Changeset 27600
- Timestamp:
- Apr 5, 2010, 12:49:31 PM (16 years ago)
- File:
-
- 1 edited
-
trunk/Ohana/src/kapa2/src/PNGit.c (modified) (5 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/Ohana/src/kapa2/src/PNGit.c
r27560 r27600 1 1 # include "Ximage.h" 2 3 /* I almost have PNG images + plots working. I need to do a few more things: 4 1) decide on the dimensions of the output image/plot file -- like the jpeg covering the pixels? adjust the box to include the full plot box? 5 2) update bDraw to either write palette values (as now) or RGB values into the buffer 6 3) decision on output based on the existence of images or not 7 */ 8 9 bDrawBuffer *bDrawBufferCreate8bitRGB (int Nx, int Ny); 10 int bDrawImage (bDrawBuffer *buffer, Graphic *graphic); 2 11 3 12 int PNGit (int sock) { … … 6 15 png_structp png_ptr; 7 16 png_infop info_ptr; 8 png_color *palette;9 17 int Npalette; 10 18 char filename[1024]; 11 bDrawBuffer *buffer; 12 Graphic *graphic; 19 bDrawBuffer *buffer = NULL; 20 Graphic *graphic = NULL; 21 png_color *palette = NULL; 13 22 14 23 graphic = GetGraphic(); … … 48 57 49 58 /* see docs for write-row-callback to provide progress info */ 50 51 png_set_IHDR (png_ptr, info_ptr, graphic->dx, graphic->dy, 8, PNG_COLOR_TYPE_PALETTE, PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT); 52 53 /* png_set_IHDR (png_ptr, info_ptr, graphic->dx, graphic->dy, 8, PNG_COLOR_TYPE_RGB, PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT); */ 54 /* png_set_IHDR (png_ptr, info_ptr, graphic->dx, graphic->dy, 16, PNG_COLOR_TYPE_RGB, PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT); */ 55 56 /* For color images, I need to define the relationship between the drawing colors and the RGB values? */ 57 /* instead of calling png_set_PLTE, call png_set_sRGB (png_ptr, info_ptr, PNG_sRGB_INTENT_ABSOLUTE); */ 59 # define PALETTE 1 60 if (PALETTE) { 61 png_set_IHDR (png_ptr, info_ptr, graphic->dx, graphic->dy, 8, PNG_COLOR_TYPE_PALETTE, PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT); 62 palette = KapaPNGPalette (&Npalette); 63 png_set_PLTE (png_ptr, info_ptr, palette, Npalette); 64 } else { 65 // png_set_IHDR (png_ptr, info_ptr, graphic->dx, graphic->dy, 16, PNG_COLOR_TYPE_RGB, PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT); 66 png_set_IHDR (png_ptr, info_ptr, graphic->dx, graphic->dy, 8, PNG_COLOR_TYPE_RGB, PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT); 67 png_set_sRGB (png_ptr, info_ptr, PNG_sRGB_INTENT_ABSOLUTE); 68 } 58 69 59 70 /* other useful calls here: … … 61 72 png_set_text(png_ptr, info_ptr, text_ptr, num_text); 62 73 */ 63 64 palette = KapaPNGPalette (&Npalette);65 png_set_PLTE (png_ptr, info_ptr, palette, Npalette);66 74 67 75 png_write_info (png_ptr, info_ptr); 68 76 69 buffer = bDrawIt (); 77 if (PALETTE) { 78 buffer = bDrawIt (); 79 } else { 80 buffer = bDrawBufferCreate8bitRGB(graphic->dx, graphic->dy); 81 bDrawImage (buffer, graphic); 82 } 70 83 71 84 png_write_image (png_ptr, buffer[0].pixels); … … 79 92 80 93 } 94 95 /* For color images, I need to define the relationship between the drawing colors and the RGB values? */ 96 97 98 bDrawBuffer *bDrawBufferCreate8bitRGB (int Nx, int Ny) { 99 100 int i, j; 101 bDrawBuffer *buffer; 102 103 ALLOCATE (buffer, bDrawBuffer, 1); 104 buffer[0].Nx = Nx; 105 buffer[0].Ny = Ny; 106 107 ALLOCATE (buffer[0].pixels, bDrawColor *, Ny); 108 for (i = 0; i < Ny; i++) { 109 ALLOCATE (buffer[0].pixels[i], bDrawColor, 3*Nx); 110 for (j = 0; j < 3*Nx; j+=3) { 111 buffer[0].pixels[i][j+0] = 0xff; 112 buffer[0].pixels[i][j+1] = 0xff; 113 buffer[0].pixels[i][j+2] = 0xff; 114 } 115 } 116 return (buffer); 117 } 118 119 # define WHITE_R 255 120 # define WHITE_G 255 121 # define WHITE_B 255 122 # define MY_SWAP_INT(A,B) { int tmp; tmp = A; A = B; B = tmp; } 123 124 int bDrawImage (bDrawBuffer *buffer, Graphic *graphic) { 125 126 Section *section; 127 KapaImageWidget *image; 128 129 int ii, i, j; 130 int i_start, i_end, j_start, j_end; 131 int I_start, J_start; 132 int dropback; /* this is a bit of a kludge... */ 133 int dx, dy, DX, DY, inDX, inDY; 134 int expand_in, expand_out; 135 double expand, Ix, Iy; 136 unsigned char *out_pix; 137 unsigned short *in_pix, *in_pix_ref; 138 unsigned char *pixel1, *pixel2, *pixel3; 139 140 bDrawColor *line_buffer; 141 142 section = GetActiveSection(); 143 image = section->image; 144 if (image == NULL) return (TRUE); 145 146 ALLOCATE (pixel1, unsigned char, graphic[0].Npixels); 147 ALLOCATE (pixel2, unsigned char, graphic[0].Npixels); 148 ALLOCATE (pixel3, unsigned char, graphic[0].Npixels); 149 150 /** cmap[i].pixel must be defined even if X is not used **/ 151 for (i = 0; i < graphic[0].Npixels; i++) { /* set up pixel array */ 152 pixel1[i] = graphic[0].cmap[i].red >> 8; 153 pixel2[i] = graphic[0].cmap[i].green >> 8; 154 pixel3[i] = graphic[0].cmap[i].blue >> 8; 155 } 156 157 assert ((image[0].picture.expand >= 1) || (image[0].picture.expand <= -2)); 158 expand = expand_in = expand_out = 1.0; 159 if (image[0].picture.expand > 0) { 160 expand = 1 / (1.0*image[0].picture.expand); 161 expand_out = image[0].picture.expand; 162 expand_in = 1; 163 } 164 if (image[0].picture.expand < 0) { 165 expand = fabs((double)image[0].picture.expand); 166 expand_out = 1; 167 expand_in = -image[0].picture.expand; 168 } 169 170 dx = image[0].picture.dx; 171 dy = image[0].picture.dy; 172 DX = image[0].image[0].matrix.Naxis[0]; 173 DY = image[0].image[0].matrix.Naxis[1]; 174 175 // i_start, j_start are the closest lit screen pixel to 0,0 176 // I_start, J_start are the image pixel corresponding to i_start, j_start 177 Picture_Lower (&i_start, &j_start, &I_start, &J_start, &image[0].image[0].matrix, &image[0].picture); 178 179 // i_end, j_end are the closest lit screen pixel to dx, dy 180 // I_end, J_end are the image pixel corresponding to i_end, j_end 181 Picture_Upper (&i_end, &j_end, i_start, j_start, &image[0].image[0].matrix, &image[0].picture); 182 183 assert (i_start <= i_end); 184 assert (j_start <= j_end); 185 186 Ix = image[0].picture.flipx ? I_start - 1 : I_start; 187 Iy = image[0].picture.flipy ? J_start - 1 : J_start; 188 189 inDX = image[0].picture.flipx ? -1 : +1; 190 inDY = image[0].picture.flipy ? -1 : +1; 191 192 dropback = expand_out - (i_end - i_start) % expand_out; 193 if ((i_end - i_start) % expand_out == 0) dropback = 0; 194 195 ALLOCATE (line_buffer, bDrawColor, 3*dx); 196 197 in_pix_ref = &image[0].pixmap[DX*(int)MAX(Iy,0) + (int)MAX(Ix,0)]; 198 199 /********** below we do the mapping from buffer pixels (in) to picture pixels (out) **********/ 200 201 /**** fill in bottom area ****/ 202 out_pix = line_buffer; 203 for (i = 0; i < dx; i++, out_pix+=3) { 204 out_pix[0] = WHITE_R; 205 out_pix[1] = WHITE_G; 206 out_pix[2] = WHITE_B; 207 } 208 for (j = 0; j < j_start; j++) { 209 memcpy (buffer[0].pixels[j], line_buffer, 3*dx); 210 } 211 212 /*** fill in the image data region ***/ 213 for (j = j_start; j < j_end; j+= expand_out, in_pix_ref += inDY*expand_in*DX) { 214 215 /* create one output image line */ 216 in_pix = in_pix_ref; 217 out_pix = line_buffer; 218 219 /**** fill in area to the left of the picture ****/ 220 for (i = 0; i < i_start; i++, out_pix+=3) { 221 out_pix[0] = WHITE_R; 222 out_pix[1] = WHITE_G; 223 out_pix[2] = WHITE_B; 224 } 225 226 /*** fill in the picture region ***/ 227 for (i = i_start; i < i_end; i+=expand_out, in_pix += inDX*expand_in) { 228 for (ii = 0; ii < expand_out; ii++, out_pix+=3) { 229 out_pix[0] = pixel1[*in_pix]; 230 out_pix[1] = pixel2[*in_pix]; 231 out_pix[2] = pixel3[*in_pix]; 232 } 233 } 234 235 /**** fill in area to the right of the picture ****/ 236 for (i = i_end; i < dx; i++, out_pix+=3) { 237 out_pix[0] = WHITE_R; 238 out_pix[1] = WHITE_G; 239 out_pix[2] = WHITE_B; 240 } 241 242 /* write out the image line expand_out times */ 243 for (i = 0; i < expand_out; i++) { 244 memcpy (buffer[0].pixels[j + i], line_buffer, 3*dx); 245 } 246 } 247 248 /**** fill in top area ****/ 249 out_pix = line_buffer; 250 for (i = 0; i < dx; i++, out_pix+=3) { 251 out_pix[0] = WHITE_R; 252 out_pix[1] = WHITE_G; 253 out_pix[2] = WHITE_B; 254 } 255 for (j = j_end; j < dy; j++) { 256 memcpy (buffer[0].pixels[j], line_buffer, 3*dx); 257 } 258 259 free (pixel1); 260 free (pixel2); 261 free (pixel3); 262 free (line_buffer); 263 264 return (TRUE); 265 } 266 267 # if (0) 268 269 /* I need to write the overlay objects on the jpeg image. 270 if i can write / overwrite data in jpeg buffer, then do it here, 271 otherwise i need to create a temporary image buffer, then write the 272 scanlines to that buffer */ 273 274 { 275 int Npalette; 276 png_color *palette; 277 bDrawColor white, color; 278 bDrawBuffer *buffer; 279 280 palette = KapaPNGPalette (&Npalette); 281 282 buffer = bDrawBufferCreate (dx, dy); 283 bDrawSetBuffer (buffer); 284 for (i = 0; i < NOVERLAYS; i++) { 285 if (image[i].overlay[i].active) bDrawOverlay (image, i); 286 } 287 288 white = KapaColorByName ("white"); 289 for (j = 0; j < dy; j++) { 290 for (i = 0; i < dx; i++) { 291 color = buffer[0].pixels[j][i]; 292 if (color == white) continue; 293 image_buffer[j*3*dx + 3*i + 0] = palette[color].red; 294 image_buffer[j*3*dx + 3*i + 1] = palette[color].green; 295 image_buffer[j*3*dx + 3*i + 2] = palette[color].blue; 296 } 297 } 298 bDrawBufferFree (buffer); 299 } 300 301 # endif
Note:
See TracChangeset
for help on using the changeset viewer.
