IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Ignore:
Timestamp:
Dec 5, 2005, 3:55:45 PM (21 years ago)
Author:
eugene
Message:

adding no-X version of JPEG, other features

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/Ohana/src/kii/picture/JPEGit24.c

    r5637 r5700  
    11# include "Ximage.h"
    22# include "jpeglib.h"
    3 
    4 void JPEGit24 (Graphic *graphic, Layout *layout) {
     3# include <png.h>
     4# include "bDraw.h"
     5
     6# define WHITE_R 255
     7# define WHITE_G 255
     8# define WHITE_B 255
     9
     10int JPEGit24 (Graphic *graphic, Layout *layout) {
    511
    612  struct jpeg_compress_struct cinfo;
     
    814  JSAMPROW row_pointer[1];      /* pointer to JSAMPLE row[s] */
    915  JSAMPLE *image_buffer;        /* Points to data for current line */
    10 
    11   int i, j, ii, jj;
     16  JSAMPLE *line_buffer;         /* Points to data for current line */
     17
     18  int ii, i, j;
    1219  int i_start, i_end, j_start, j_end;
    1320  int dropback, extra;  /* this is a bit of a kludge... */
    1421  int dx, dy, DX, DY;
     22  int status, Nbytes, quality;
     23  int expand_in, expand_out;
    1524  double expand, Rx, Ry, X, Y;
    16   int expand_in, expand_out;
    17   unsigned char *out_pix, *out_pix2, *data;
    18   unsigned char *in_pix,  *in_pix2;
     25  unsigned char *out_pix, *in_pix, *in_pix_ref;
    1926  unsigned char pixel1[256], pixel2[256], pixel3[256];
    20   unsigned char pixvalue1, pixvalue2, pixvalue3;
    21   unsigned long white;
    22   unsigned char white1, white2, white3;
     27  char filename[1024];
    2328  FILE *f;
    2429
     
    3641  f = fopen (filename, "w");
    3742  if (f == (FILE *) NULL) {
    38     fprintf (stderr, "failed to open %s for output\n", filename);
     43    fprintf (stderr, "Kii: failed to open %s for output\n", filename);
    3944    return (TRUE);
    4045  }
     
    5560  jpeg_start_compress (&cinfo, TRUE);
    5661
    57   /* output line buffer */
    58   ALLOCATE (image_buffer, JSAMPLE, 3*layout[0].picture.dx);
    59 
    6062  /** cmap[i].pixel must be defined even if X is not used **/
    6163  for (i = 0; i < 256; i++) { /* set up pixel array */
    62     pixel1[i] = 0x0000ff & (layout[0].cmap[i].pixel >> 0);
    63     pixel2[i] = 0x0000ff & (layout[0].cmap[i].pixel >> 8);
    64     pixel3[i] = 0x0000ff & (layout[0].cmap[i].pixel >> 16);
    65   }
    66   white = layout[0].white;
    67   white1 = 0x0000ff & (white >> 0);
    68   white2 = 0x0000ff & (white >> 8);
    69   white3 = 0x0000ff & (white >> 16);
     64    pixel1[i] = layout[0].cmap[i].red >> 8;
     65    pixel2[i] = layout[0].cmap[i].green >> 8;
     66    pixel3[i] = layout[0].cmap[i].blue >> 8;
     67  }
    7068
    7169  expand = expand_in = expand_out = 1.0;
     
    8583  dx = layout[0].picture.dx;
    8684  dy = layout[0].picture.dy;
    87   DX = layout[0].matrix[0].Naxis[0];
    88   DY = layout[0].matrix[0].Naxis[1];
    89   extra = 4 - (dx * 3) % 4;  /* why is this needed?? */
     85  DX = layout[0].matrix.Naxis[0];
     86  DY = layout[0].matrix.Naxis[1];
    9087
    9188  /* X,Y are the image coordinates of the first image pixel */
     
    119116  if ((i_end - i_start) % expand_out == 0) dropback = 0;
    120117
    121   out_pix = image_buffer;
    122   in_pix  = (unsigned char *) (layout[0].matrix[0].buffer) + DX*(int)MAX(Y,0) + (int)MAX(X,0);
     118  /* output line buffer */
     119  ALLOCATE (image_buffer, JSAMPLE, 3*dx*dy);
     120  ALLOCATE (line_buffer, JSAMPLE, 3*dx);
     121
     122  in_pix_ref  = (unsigned char *) (layout[0].matrix.buffer) + DX*(int)MAX(Y,0) + (int)MAX(X,0);
    123123
    124124  /********** below we do the mapping from buffer pixels (in) to picture pixels (out) **********/
    125125
    126   /* we write the jpeg image data one row at a time */
    127 
    128126  /**** fill in bottom area ****/
    129   out_pix = image_buffer;
     127  out_pix = line_buffer;
    130128  for (i = 0; i < dx; i++, out_pix+=3) {
    131129    out_pix[0] = WHITE_R;
     
    134132  }
    135133  for (j = 0; j < j_start; j++) {
    136     row_pointer[0] = image_buffer;
    137     (void) jpeg_write_scanlines (&cinfo, row_pointer, 1);
     134    memcpy (&image_buffer[j*3*dx], line_buffer, 3*dx);
    138135  }
    139136 
    140137  /*** fill in the image data region ***/
    141   for (j = j_start; j < j_end; j+= expand_out, in_pix += expand_in*DX) {
     138  for (j = j_start; j < j_end; j+= expand_out, in_pix_ref += expand_in*DX) {
    142139   
    143140    /* create one output image line */
    144     out_pix = image_buffer;
     141    in_pix = in_pix_ref;
     142    out_pix = line_buffer;
    145143
    146144    /**** fill in area to the left of the picture ****/
     
    152150   
    153151    /*** fill in the picture region ***/
    154     for (i = i_start; i < i_end; i++, in_pix+=expand_in, out_pix+=3) {
    155       out_pix[0] = pixel1[*in_pix];
    156       out_pix[1] = pixel2[*in_pix];
    157       out_pix[2] = pixel3[*in_pix];
     152    for (i = i_start; i < i_end; i+=expand_out, in_pix+=expand_in) {
     153      for (ii = 0; ii < expand_out; ii++, out_pix+=3) {
     154        out_pix[0] = pixel1[*in_pix];
     155        out_pix[1] = pixel2[*in_pix];
     156        out_pix[2] = pixel3[*in_pix];
     157      }
    158158    }
    159159   
     
    167167    /* write out the image line expand_out times */
    168168    for (i = 0; i < expand_out; i++) {
    169       row_pointer[0] = image_buffer;
    170       (void) jpeg_write_scanlines (&cinfo, row_pointer, 1);
     169      memcpy (&image_buffer[(j + i)*3*dx], line_buffer, 3*dx);
    171170    }
    172171  }
    173172
    174173  /**** fill in top area ****/
    175   out_pix = image_buffer;
     174  out_pix = line_buffer;
    176175  for (i = 0; i < dx; i++, out_pix+=3) {
    177176    out_pix[0] = WHITE_R;
     
    179178    out_pix[2] = WHITE_B;
    180179  }
    181   for (j = 0; j < (dy - j_end); j++) {
    182     row_pointer[0] = image_buffer;
     180  for (j = j_end; j < dy; j++) {
     181    memcpy (&image_buffer[j*3*dx], line_buffer, 3*dx);
     182  }
     183
     184
     185  /* I need to write the overlay objects on the jpeg image.
     186     if i can write / overwrite data in jpeg buffer, then do it here,
     187     otherwise i need to create a temporary image buffer, then write the
     188     scanlines to that buffer */
     189
     190  {
     191    int Npalette;
     192    png_color *palette;
     193    bDrawColor white, color;
     194    bDrawBuffer *buffer;
     195
     196    palette = MakePNGPalette (&Npalette);
     197
     198    buffer = bDrawBufferCreate (dx, dy);
     199    bDrawSetBuffer (buffer);
     200    for (i = 0; i < NOVERLAYS; i++) {
     201      if (OVERLAY[i]) bDrawOverlay (layout, i);
     202    }
     203
     204    white = GetColorByName ("white");
     205    for (j = 0; j < dy; j++) {
     206      for (i = 0; i < dx; i++) {
     207        color = buffer[0].pixels[j][i];
     208        if (color == white) continue;
     209        image_buffer[j*3*dx + 3*i + 0] = palette[color].red;
     210        image_buffer[j*3*dx + 3*i + 1] = palette[color].green;
     211        image_buffer[j*3*dx + 3*i + 2] = palette[color].blue;
     212      }
     213    }
     214    bDrawBufferFree (buffer);
     215  }
     216
     217  for (i = 0; i < dy; i++) {
     218    row_pointer[0] = &image_buffer[i*3*dx];
    183219    (void) jpeg_write_scanlines (&cinfo, row_pointer, 1);
    184220  }
     
    190226  return (TRUE);
    191227}
    192 
Note: See TracChangeset for help on using the changeset viewer.