- Timestamp:
- Aug 26, 2010, 9:18:39 AM (16 years ago)
- Location:
- branches/sc_branches/trunkTest
- Files:
-
- 2 edited
-
. (modified) (1 prop)
-
psLib/src/jpeg/psImageJpeg.c (modified) (2 diffs)
Legend:
- Unmodified
- Added
- Removed
-
branches/sc_branches/trunkTest
- Property svn:mergeinfo changed
-
branches/sc_branches/trunkTest/psLib/src/jpeg/psImageJpeg.c
r27145 r29060 7 7 #include <string.h> 8 8 9 #include <kapa.h> 9 10 #include "psMemory.h" 10 11 #include "psImage.h" … … 134 135 return psImageJpegColormapSet (map, "greyscale"); 135 136 } 137 138 // XXX need to fix library references for this (psLib does not depend on libkapa) 139 # if (0) 140 // XXX Add colormap bar with scale (min -> max) 141 // XXX Add option to plot the source overlay (pass in bDrawBuffer populated with points?) 142 // XXX need to update bDraw APIs to pass in/out structure and avoid the local static 143 bool psImageJpegNew(const psImageJpegColormap *map, const psImage *image, const char *filename, 144 float min, float max) 145 { 146 PS_ASSERT_PTR_NON_NULL(map, false); 147 PS_ASSERT_IMAGE_NON_NULL(image, false); 148 PS_ASSERT_IMAGE_TYPE(image, PS_TYPE_F32, false); 149 PS_ASSERT_VECTOR_NON_NULL(map->red, false); 150 PS_ASSERT_VECTOR_NON_NULL(map->green, false); 151 PS_ASSERT_VECTOR_NON_NULL(map->blue, false); 152 PS_ASSERT_PTR_NON_NULL(filename, false); 153 PS_ASSERT_INT_POSITIVE(strlen(filename), false); 154 PS_ASSERT_FLOAT_REAL(min, false); 155 PS_ASSERT_FLOAT_REAL(max, false); 156 157 float zero, scale; 158 struct jpeg_compress_struct cinfo; 159 struct jpeg_error_mgr jerr; 160 161 long pixel; 162 JSAMPLE *jpegLine; // Points to data for current line 163 JSAMPROW jpegLineList[1]; // pointer to JSAMPLE row[s] 164 JSAMPLE *jpegImage; 165 JSAMPLE *outPix; 166 167 /* JPEG init calls */ 168 cinfo.err = jpeg_std_error (&jerr); 169 jpeg_create_compress (&cinfo); 170 171 /* open file, prep for jpeg */ 172 FILE *f = fopen(filename, "w"); 173 if (!f) { 174 psError(PS_ERR_IO, true, "failed to open %s for output\n", filename); 175 return false; 176 } 177 jpeg_stdio_dest(&cinfo, f); 178 179 /* set up color jpeg buffers */ 180 int quality = 75; 181 cinfo.image_width = image->numCols; // image width and height, in pixels 182 cinfo.image_height = image->numRows; 183 cinfo.input_components = 3; 184 cinfo.in_color_space = JCS_RGB; 185 jpeg_set_defaults (&cinfo); 186 jpeg_set_quality (&cinfo, quality, true); // limit to baseline-JPEG values 187 jpeg_start_compress (&cinfo, true); 188 189 psU8 *Rpix = map->red->data.U8; 190 psU8 *Gpix = map->green->data.U8; 191 psU8 *Bpix = map->blue->data.U8; 192 193 if (max == min) { 194 zero = min - 0.1; 195 scale = 256.0/0.2; 196 } else { 197 zero = min; 198 scale = 256.0/(max - min); 199 } 200 201 int dx = image->numCols; 202 int dy = image->numRows; 203 204 // output image buffer and line buffer 205 jpegLine = psAlloc (3*dx*sizeof(JSAMPLE)); 206 jpegImage = psAlloc (3*dx*dy*sizeof(JSAMPLE)); 207 208 // first copy the image data into the output buffer 209 for (int j = 0; j < dy; j++) { 210 psF32 *row = image->data.F32[j]; 211 212 outPix = jpegLine; 213 for (int i = 0; i < dx; i++, outPix += 3) { 214 if (isfinite(row[i])) { 215 pixel = PS_JPEG_SCALEVALUE(row[i],zero,scale); 216 outPix[0] = Rpix[pixel]; 217 outPix[1] = Gpix[pixel]; 218 outPix[2] = Bpix[pixel]; 219 } else { 220 // XXX NAN value should be set per-color map 221 outPix[0] = 0x00; 222 outPix[1] = 0xff; 223 outPix[2] = 0x00; 224 } 225 } 226 memcpy (&jpegImage[j*3*dx], jpegLine, 3*dx); 227 } 228 229 bDrawBuffer *bdbuf = bDrawBufferCreate(dx, dy); 230 bDrawSetBuffer(bdbuf); 231 bDrawColor red = KapaColorByName("red"); 232 bDrawSetStyle (red, 1, 0); 233 bDrawCircle(40.0, 20.0, 3.0); 234 235 { 236 int Npalette; 237 png_color *palette = KapaPNGPalette (&Npalette); 238 bDrawColor white = KapaColorByName ("white"); 239 for (int j = 0; j < dy; j++) { 240 for (int i = 0; i < dx; i++) { 241 bDrawColor color = bdbuf[0].pixels[j][i]; 242 if (color == white) continue; 243 jpegImage[j*3*dx + 3*i + 0] = palette[color].red; 244 jpegImage[j*3*dx + 3*i + 1] = palette[color].green; 245 jpegImage[j*3*dx + 3*i + 2] = palette[color].blue; 246 } 247 } 248 } 249 bDrawBufferFree (bdbuf); 250 251 // write out the image buffer 252 for (int j = 0; j < image->numRows; j++) { 253 jpegLineList[0] = &jpegImage[j*3*dx]; 254 if (jpeg_write_scanlines(&cinfo, jpegLineList, 1) == 0) { 255 psError(PS_ERR_IO, true, "Unable to write line %d to JPEG", j); 256 psFree(jpegLine); 257 psFree(jpegImage); 258 fclose(f); 259 return false; 260 } 261 } 262 263 jpeg_finish_compress(&cinfo); 264 if (fclose(f) == EOF) { 265 psError(PS_ERR_IO, true, "Failed to close %s", filename); 266 psFree(jpegLine); 267 psFree(jpegImage); 268 return false; 269 } 270 jpeg_destroy_compress(&cinfo); 271 272 psFree(jpegLine); 273 psFree(jpegImage); 274 return true; 275 } 276 # endif 136 277 137 278 bool psImageJpeg(const psImageJpegColormap *map, const psImage *image, const char *filename,
Note:
See TracChangeset
for help on using the changeset viewer.
