48 #if PNG_LIBPNG_VER < 10400
50 #define png_set_expand_gray_1_2_4_to_8 png_set_gray_1_2_4_to_8
62 #define FILE_BUFFER_CAPACITY (1024*4)
64 #define ROUNDCLAMPF(x) ((x < 0.0f) ? 0 : \
65 ((x > 1.0f) ? 255 : (uint8_t)(255.0f*(x) + 0.5f)))
66 #define ROUNDCLAMP(x) ((x < 0.0) ? 0 : \
67 ((x > 1.0) ? 255 : (uint8_t)(255.0*(x) + 0.5)))
73 unsigned string_length = strlen(
string), suffix_length = strlen(suffix);
76 if (string_length < suffix_length)
79 string += string_length - suffix_length;
81 for (i = 0; i < suffix_length; ++i)
82 if (tolower(
string[i]) != tolower(suffix[i]))
90 static void fill_image(uint32_t *image,
int width,
int height, uint32_t color)
95 for (y = 0; y < height; ++y, image += width)
96 for (x = 0; x < width; ++x)
116 int *use_alpha,
const uint32_t *image,
int width,
int height)
118 const int max_colors = 256;
119 uint32_t *palette = NULL;
121 int x, y, i, red, green, blue, alpha;
124 if (!use_color || !num_colors || !use_alpha)
127 || !(palette = (uint32_t *)malloc(
sizeof(uint32_t)*max_colors)))
130 *use_color = *use_alpha = 1;
134 *num_colors = *use_color = *use_alpha = 0;
136 for (y = 0; y < height; ++y)
138 for (x = 0; x < width; ++x)
141 red = ((uint8_t *)&pixel)[0];
142 green = ((uint8_t *)&pixel)[1];
143 blue = ((uint8_t *)&pixel)[2];
144 alpha = ((uint8_t *)&pixel)[3];
146 if (red != green || red != blue)
153 for (i = 0; i < *num_colors; ++i)
154 if (pixel == palette[i])
157 if (i == *num_colors)
182 w = (uint16_t) getc(file);
183 w |= ((uint16_t) getc(file) << 8);
192 dw = (uint32_t) getc(file);
193 dw |= ((uint32_t) getc(file) << 8);
194 dw |= ((uint32_t) getc(file) << 16);
195 dw |= ((uint32_t) getc(file) << 24);
203 putc(w & 0xFF, file);
204 putc((w & 0xFF00) >> 8, file);
211 putc(dw & 0xFF, file);
212 putc((dw & 0xFF00) >> 8, file);
213 putc((dw & 0xFF0000) >> 16, file);
214 putc((dw & 0xFF000000) >> 24, file);
220 FILE *file,
const uint32_t *palette)
222 int row_padding = (-(width + 7) / 8) & 3;
226 image += ((
long int)width) * ((
long int)height - 1);
228 for (y = height; y; --y, image -= width)
233 for (x = 0; x < width;)
237 for (bit = 7; bit >= 0 && x < width; --bit, code <<= 1)
238 image[x++] = palette[(code & 0x80) ? 1:0];
241 for (x = row_padding; x; --x)
251 FILE *file,
const uint32_t *palette)
253 int row_padding = (-(width + 1) / 2) & 3;
257 image += ((
long int)width) * ((
long int)height - 1);
259 for (y = height; y; --y, image -= width)
264 for (x = 0; x < width;)
267 image[x++] = palette[(code & 0xF0) >> 4];
270 image[x++] = palette[code & 0x0F];
273 for (x = row_padding; x; --x)
283 FILE *file,
const uint32_t *palette)
286 unsigned count, value;
287 uint32_t color_high, color_low;
290 image += ((
long int)width) * ((
long int)height - 1);
292 for (x = 0, y = height; y;)
317 if (x >= width || y < 0)
330 image[x++] = palette[(value & 0xF0) >> 4];
337 image[x++] = palette[value & 0x0F];
345 if (((count + 1)/2) & 1)
351 color_high = palette[(value & 0xF0) >> 4];
352 color_low = palette[value & 0xF];
359 image[x++] = color_high;
367 image[x++] = color_low;
383 FILE *file,
const uint32_t *palette)
385 int row_padding = (-width) & 3;
388 image += ((
long int)width) * ((
long int)height - 1);
390 for (y = height; y; --y, image -= width)
395 for (x = 0; x < width; ++x)
396 image[x] = palette[getc(file) & 0xFF];
398 for (x = row_padding; x; --x)
408 FILE *file,
const uint32_t *palette)
411 unsigned count, value;
415 image += ((
long int)width) * ((
long int)height - 1);
417 for (x = 0, y = height; y;)
442 if (x >= width || y < 0)
454 image[x++] = palette[getc(file) & 0xFF];
463 color = palette[value & 0xFF];
482 uint8_t *image_ptr = (uint8_t *)image;
483 int row_padding = (-3*width) & 3;
487 image_ptr += ((
long int)width)*((
long int)height - 1);
489 for (y = height; y; --y, image_ptr -= width)
494 for (x = 0; x < width; x += 4)
496 image_ptr[x+3] = 255;
497 image_ptr[x+2] = getc(file);
498 image_ptr[x+1] = getc(file);
499 image_ptr[x+0] = getc(file);
502 for (x = row_padding; x; --x)
512 int shift = 0, bitcount = 0;
535 shift += bitcount - 8;
540 *right_shift = shift;
544 *left_shift = -shift;
551 uint32_t redmask, uint32_t greenmask,
552 uint32_t bluemask, uint32_t alphamask)
554 uint8_t *image_ptr = (uint8_t *)image;
556 int row_padding = (-2 * width) & 3;
557 int redleft_shift, greenleft_shift, blueleft_shift, alphaleft_shift;
558 int redright_shift, greenright_shift, blueright_shift, alpharight_shift;
566 image_ptr += ((
long int)width) * ((
long int)height - 1);
568 for (y = height; y; --y, image_ptr -= width)
573 for (x = 0; x < width; x += 4)
579 image_ptr[x + 3] = ((code & alphamask) >> alpharight_shift)
581 image_ptr[x + 2] = ((code & bluemask ) >> blueright_shift )
583 image_ptr[x + 1] = ((code & greenmask) >> greenright_shift)
585 image_ptr[x + 0] = ((code & redmask ) >> redright_shift )
589 for (x = row_padding; x; --x)
599 uint32_t redmask, uint32_t greenmask,
600 uint32_t bluemask, uint32_t alphamask)
604 int redleft_shift, greenleft_shift, blueleft_shift, alphaleft_shift;
605 int redright_shift, greenright_shift, blueright_shift, alpharight_shift;
613 image_ptr = (uint8_t *)image + ((
long int)width)*((
long int)height - 1);
615 for (y = height; y; --y, image_ptr -= width)
620 for (x = 0; x < width; x += 4)
623 image_ptr[x + 3] = ((code & alphamask) >> alpharight_shift)
625 image_ptr[x + 2] = ((code & bluemask ) >> blueright_shift )
627 image_ptr[x + 1] = ((code & greenmask) >> greenright_shift)
629 image_ptr[x + 0] = ((code & redmask ) >> redright_shift )
650 static int read_bmp(uint32_t **image,
int *width,
int *height, FILE *file)
652 uint32_t *palette = NULL;
653 uint8_t *palette_ptr;
654 long int image_data_offset, info_size;
655 unsigned i, num_planes, bits_per_pixel, compression, num_colors;
656 uint32_t redmask, greenmask, bluemask, alphamask;
657 int success = 0, os2bmp;
661 *width = *height = 0;
662 fseek(file, 0, SEEK_SET);
664 magic[0] = getc(file);
665 magic[1] = getc(file);
667 if (!(magic[0] == 0x42 && magic[1] == 0x4D)
668 || fseek(file, 8, SEEK_CUR))
670 fprintf(stderr,
"Invalid BMP header.\n");
680 fprintf(stderr,
"Invalid BMP info header.\n");
684 if ((os2bmp = (info_size == 12)))
692 redmask = 0x00FF0000;
693 greenmask = 0x0000FF00;
694 bluemask = 0x000000FF;
695 alphamask = 0xFF000000;
704 fseek(file, 12, SEEK_CUR);
706 fseek(file, 4, SEEK_CUR);
716 fprintf(stderr,
"image dimensions exceed MAX_IMAGE_SIZE.\n");
720 if (feof(file) || num_planes != 1 || compression > 3)
724 if (!(*image = (uint32_t *)malloc(
725 sizeof(uint32_t)*((
long int)*width)*((
long int)*height))))
729 if (bits_per_pixel <= 8)
731 fseek(file, 14 + info_size, SEEK_SET);
734 num_colors = 1 << bits_per_pixel;
736 if (!(palette = (uint32_t *)malloc(
sizeof(uint32_t)*256)))
739 for (i = 0, palette_ptr = (uint8_t *)palette; i < num_colors; ++i)
741 palette_ptr[3] = 255;
742 palette_ptr[2] = getc(file);
743 palette_ptr[1] = getc(file);
744 palette_ptr[0] = getc(file);
753 palette[i] = palette[0];
756 if (fseek(file, image_data_offset, SEEK_SET) || feof(file))
758 fprintf(stderr,
"file error.\n");
766 switch (bits_per_pixel)
770 *image, *width, *height, file, palette);
774 *image, *width, *height, file, palette);
778 *image, *width, *height, file, palette);
785 0x001F << 10, 0x001F << 5, 0x0001F, 0);
789 0x00FF0000, 0x0000FF00, 0x000000FF, 0xFF000000);
794 if (bits_per_pixel == 8)
796 *image, *width, *height, file, palette);
799 if (bits_per_pixel == 4)
801 *image, *width, *height, file, palette);
804 switch (bits_per_pixel)
808 redmask, greenmask, bluemask, alphamask);
812 redmask, greenmask, bluemask, alphamask);
819 fprintf(stderr,
"Error reading BMP data.\n");
825 if (!success && *image)
853 static int write_bmp(
const uint32_t *image,
int width,
int height, FILE *file)
855 const uint8_t *image_ptr = (uint8_t *)image;
856 uint32_t *palette = NULL;
859 int use_palette, num_colors, use_color, use_alpha;
860 int x, y, i, row_padding, success = 0;
867 image, width, height);
870 if (palette && 2*num_colors < width*height)
873 use_palette = num_colors = 0;
880 row_padding = (-width)&3;
881 imageSize = (width + row_padding)*((
long int)height);
885 row_padding = (-3*width)&3;
886 imageSize = (3*width + row_padding)*((
long int)height);
913 write_u32_le((!use_palette || num_colors == 256) ? 0:num_colors, file);
919 fprintf(stderr,
"Error during write to file.\n");
925 for (i = 0; i < num_colors; ++i)
928 putc(((uint8_t *)&pixel)[2], file);
929 putc(((uint8_t *)&pixel)[1], file);
930 putc(((uint8_t *)&pixel)[0], file);
937 image_ptr += ((
long int)width)*((
long int)height - 1);
939 for (y = height; y; --y, image_ptr -= width)
943 for (x = 0; x < width; x += 4)
945 pixel = *((uint32_t *)(image_ptr + x));
947 for (i = 0; i < num_colors; ++i)
948 if (pixel == palette[i])
956 for (x = 0; x < width; x += 4)
958 putc(image_ptr[x+2], file);
959 putc(image_ptr[x+1], file);
960 putc(image_ptr[x+0], file);
964 for (x = row_padding; x; --x)
970 fprintf(stderr,
"Error during write to file.\n");
990 struct jpeg_error_mgr pub;
996 METHODDEF(
void) jerr_exit(j_common_ptr cinfo)
998 hooked_jerr *jerr = (hooked_jerr *) cinfo->err;
999 (*cinfo->err->output_message)(cinfo);
1000 longjmp(jerr->jmpbuf, 1);
1018 static int read_jpeg(uint32_t **image,
int *width,
int *height, FILE *file)
1020 struct jpeg_decompress_struct cinfo;
1024 unsigned i, row_size;
1027 *width = *height = 0;
1028 cinfo.err = jpeg_std_error(&jerr.pub);
1029 jerr.pub.error_exit = jerr_exit;
1031 if (setjmp(jerr.jmpbuf))
1036 jpeg_create_decompress(&cinfo);
1037 jpeg_stdio_src(&cinfo, file);
1038 jpeg_read_header(&cinfo, 1);
1039 cinfo.out_color_space = JCS_RGB;
1040 jpeg_start_decompress(&cinfo);
1041 *width = (int)cinfo.output_width;
1042 *height = (
int)cinfo.output_height;
1046 fprintf(stderr,
"image dimensions exceed MAX_IMAGE_SIZE.\n");
1047 jpeg_abort_decompress(&cinfo);
1052 if (!(*image = (uint32_t *)malloc(
sizeof(uint32_t)
1053 * ((
size_t)*width) * ((
size_t)*height))))
1055 jpeg_abort_decompress(&cinfo);
1060 row_size = cinfo.output_width * cinfo.output_components;
1061 buffer = (*cinfo.mem->alloc_sarray) ((j_common_ptr) &cinfo,
1062 JPOOL_IMAGE, row_size, 1);
1063 image_ptr = (uint8_t *)*image;
1065 while (cinfo.output_scanline < cinfo.output_height)
1066 for (jpeg_read_scanlines(&cinfo, buffer, 1), i = 0;
1067 i < row_size; i += 3)
1069 *(image_ptr++) = buffer[0][i];
1070 *(image_ptr++) = buffer[0][i + 1];
1071 *(image_ptr++) = buffer[0][i + 2];
1072 *(image_ptr++) = 0xFF;
1075 jpeg_finish_decompress(&cinfo);
1076 jpeg_destroy_decompress(&cinfo);
1083 *width = *height = 0;
1084 jpeg_destroy_decompress(&cinfo);
1107 static int write_jpeg(
const uint32_t *
const image,
1108 int width,
int height, FILE *file,
int quality)
1110 struct jpeg_compress_struct cinfo;
1112 uint8_t *buffer = NULL, *image_ptr;
1113 unsigned i, row_size;
1118 cinfo.err = jpeg_std_error(&jerr.pub);
1119 jerr.pub.error_exit = jerr_exit;
1121 if (setjmp(jerr.jmpbuf))
1124 jpeg_create_compress(&cinfo);
1125 jpeg_stdio_dest(&cinfo, file);
1126 cinfo.image_width = width;
1127 cinfo.image_height = height;
1128 cinfo.input_components = 3;
1129 cinfo.in_color_space = JCS_RGB;
1130 jpeg_set_defaults(&cinfo);
1131 jpeg_set_quality(&cinfo, (quality < 100) ? quality : 100, 1);
1132 jpeg_start_compress(&cinfo, 1);
1134 row_size = 3 * width;
1135 image_ptr = (uint8_t *)image;
1137 if (!(buffer = (uint8_t *)malloc(row_size)))
1140 while (cinfo.next_scanline < cinfo.image_height)
1142 for (i = 0; i < row_size; i += 3)
1144 buffer[i] = image_ptr[0];
1145 buffer[i + 1] = image_ptr[1];
1146 buffer[i + 2] = image_ptr[2];
1150 jpeg_write_scanlines(&cinfo, &buffer, 1);
1156 jpeg_finish_compress(&cinfo);
1157 jpeg_destroy_compress(&cinfo);
1163 jpeg_destroy_compress(&cinfo);
1183 static int read_png(uint32_t **image,
int *width,
int *height, FILE *file)
1185 png_bytep *row_pointers;
1189 png_uint_32 png_width, png_height;
1190 int bit_depth, color_type, interlace_type;
1194 *width = *height = 0;
1197 if (fread(header, 1, 8, file) != 8 || png_sig_cmp(header, 0, 8))
1201 if (!(png = png_create_read_struct(
1202 PNG_LIBPNG_VER_STRING, NULL, NULL, NULL))
1203 || !(info = png_create_info_struct(png)))
1206 png_destroy_read_struct(&png, (png_infopp)NULL, (png_infopp)NULL);
1211 if (setjmp(png_jmpbuf(png)))
1214 png_init_io(png, file);
1215 png_set_sig_bytes(png, 8);
1217 png_read_info(png, info);
1218 png_get_IHDR(png, info, &png_width, &png_height, &bit_depth, &color_type,
1219 &interlace_type, (
int*)NULL, (
int*)NULL);
1220 *width = (int)png_width;
1221 *height = (int)png_height;
1224 if (color_type == PNG_COLOR_TYPE_PALETTE)
1225 png_set_palette_to_rgb(png);
1226 if (color_type == PNG_COLOR_TYPE_GRAY && bit_depth < 8)
1227 png_set_expand_gray_1_2_4_to_8(png);
1228 if (color_type == PNG_COLOR_TYPE_GRAY
1229 || color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
1230 png_set_gray_to_rgb(png);
1231 if (png_get_valid(png, info, PNG_INFO_tRNS))
1232 png_set_tRNS_to_alpha(png);
1234 png_set_strip_16(png);
1235 png_set_filler(png, 0xFF, PNG_FILLER_AFTER);
1237 png_set_interlace_handling(png);
1238 png_read_update_info(png, info);
1241 if (!(*image = (uint32_t *)malloc(
sizeof(uint32_t)
1242 *((
size_t)*width)*((
size_t)*height)))
1243 || !(row_pointers = (png_bytep *)malloc(
sizeof(png_bytep)
1247 for (row = 0; row < png_height; ++row)
1248 row_pointers[row] = (png_bytep)(*image + png_width*row);
1251 png_read_image(png, row_pointers);
1253 png_destroy_read_struct(&png, &info, (png_infopp)NULL);
1260 *width = *height = 0;
1261 png_destroy_read_struct(&png, &info, (png_infopp)NULL);
1286 static int write_png(
const uint32_t *
const image,
1287 int width,
int height, FILE *file)
1289 const uint32_t *image_ptr;
1290 uint32_t *palette = NULL;
1291 uint8_t *row_buffer;
1294 png_color png_palette[256];
1295 png_byte png_trans[256];
1297 int png_color_type, num_colors, use_color, use_alpha;
1298 int x, y, i, success = 0;
1304 if (!(row_buffer = (uint8_t *)malloc(4*width)))
1307 if (!(png = png_create_write_struct(PNG_LIBPNG_VER_STRING,
1309 || !(info = png_create_info_struct(png)))
1312 png_destroy_write_struct(&png, (png_infopp)NULL);
1318 if (setjmp(png_jmpbuf(png)))
1324 png_init_io(png, file);
1325 png_set_compression_level(png, 9);
1328 image, width, height);
1331 if (palette && use_color)
1332 png_color_type = PNG_COLOR_TYPE_PALETTE;
1334 png_color_type = PNG_COLOR_TYPE_RGB_ALPHA;
1336 png_color_type = PNG_COLOR_TYPE_RGB;
1338 png_color_type = PNG_COLOR_TYPE_GRAY;
1340 png_set_IHDR(png, info, width, height, 8, png_color_type,
1341 PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE);
1343 if (png_color_type == PNG_COLOR_TYPE_PALETTE)
1345 for (i = 0; i < num_colors; ++i)
1348 png_palette[i].red = ((uint8_t *)&pixel)[0];
1349 png_palette[i].green = ((uint8_t *)&pixel)[1];
1350 png_palette[i].blue = ((uint8_t *)&pixel)[2];
1351 png_trans[i] = ((uint8_t *)&pixel)[3];
1354 png_set_PLTE(png, info, png_palette, num_colors);
1357 png_set_tRNS(png, info, png_trans, num_colors, NULL);
1360 png_write_info(png, info);
1362 for (y = 0, image_ptr = image; y < height; ++y, image_ptr += width)
1364 switch (png_color_type)
1366 case PNG_COLOR_TYPE_RGB_ALPHA:
1367 png_write_row(png, (png_bytep)image_ptr);
1369 case PNG_COLOR_TYPE_RGB:
1370 for (x = 0; x < width; ++x)
1372 pixel = image_ptr[x];
1373 row_buffer[3 * x + 0] = ((uint8_t *)&pixel)[0];
1374 row_buffer[3 * x + 1] = ((uint8_t *)&pixel)[1];
1375 row_buffer[3 * x + 2] = ((uint8_t *)&pixel)[2];
1378 png_write_row(png, (png_bytep)row_buffer);
1380 case PNG_COLOR_TYPE_GRAY:
1381 for (x = 0; x < width; ++x)
1383 pixel = image_ptr[x];
1384 row_buffer[x] = ((uint8_t *)&pixel)[0];
1387 png_write_row(png, (png_bytep)row_buffer);
1389 case PNG_COLOR_TYPE_PALETTE:
1390 for (x = 0; x < width; ++x)
1392 pixel = image_ptr[x];
1394 for (i = 0; i < num_colors; ++i)
1395 if (pixel == palette[i])
1401 png_write_row(png, (png_bytep)row_buffer);
1406 png_write_end(png, info);
1411 png_destroy_write_struct(&png, &info);
1432 static int read_tiff(uint32_t **image,
int *width,
int *height,
1433 const char *filename,
unsigned directory)
1436 uint32 image_width, image_height;
1439 *width = *height = 0;
1441 if (!(tiff = TIFFOpen(filename,
"r")))
1443 fprintf(stderr,
"TIFFOpen failed to open file.\n");
1447 TIFFSetDirectory(tiff, directory);
1448 TIFFGetField(tiff, TIFFTAG_IMAGEWIDTH, &image_width);
1449 TIFFGetField(tiff, TIFFTAG_IMAGELENGTH, &image_height);
1450 *width = (int)image_width;
1451 *height = (int)image_height;
1455 fprintf(stderr,
"Image dimensions exceed MAX_IMAGE_SIZE.\n");
1459 if (!(*image = (uint32_t *)malloc(
1460 sizeof(uint32_t)*image_width*image_height)))
1463 if (!TIFFReadRGBAImageOriented(tiff, image_width, image_height,
1464 (uint32 *)*image, ORIENTATION_TOPLEFT, 1))
1474 *width = *height = 0;
1493 static int write_tiff(
const uint32_t *image,
int width,
int height,
1494 const char *filename)
1497 uint16 alpha = EXTRASAMPLE_ASSOCALPHA;
1502 if (!(tiff = TIFFOpen(filename,
"w")))
1504 fprintf(stderr,
"TIFFOpen failed to open file.\n");
1508 if (TIFFSetField(tiff, TIFFTAG_IMAGEWIDTH, width) != 1
1509 || TIFFSetField(tiff, TIFFTAG_IMAGELENGTH, height) != 1
1510 || TIFFSetField(tiff, TIFFTAG_SAMPLESPERPIXEL, 4) != 1
1511 || TIFFSetField(tiff, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_RGB) != 1
1512 || TIFFSetField(tiff, TIFFTAG_EXTRASAMPLES, 1, &alpha) != 1
1513 || TIFFSetField(tiff, TIFFTAG_BITSPERSAMPLE, 8) != 1
1514 || TIFFSetField(tiff, TIFFTAG_ORIENTATION, ORIENTATION_TOPLEFT) != 1
1515 || TIFFSetField(tiff, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG) != 1
1518 || TIFFSetField(tiff, TIFFTAG_COMPRESSION, COMPRESSION_LZW) != 1)
1520 fprintf(stderr,
"TIFFSetField failed.\n");
1525 if (TIFFWriteEncodedStrip(tiff, 0, (tdata_t)image,
1526 4 * ((
size_t)width) * ((
size_t)height)) < 0)
1528 fprintf(stderr,
"Error writing data to file.\n");
1543 const int num_pixels = width * height;
1544 const int num_channels = (format & IMAGEIO_GRAYSCALE) ?
1545 1 : ((format & IMAGEIO_STRIP_ALPHA) ? 3 : 4);
1546 const int channel_stride = (format & IMAGEIO_PLANAR) ? num_pixels : 1;
1547 const int channel_stride2 = 2 * channel_stride;
1548 const int channel_stride3 = 3 * channel_stride;
1553 int order[4] = {0, 1, 2, 3};
1554 int i, x, y, pixel_stride, row_stride;
1557 pixel_stride = (format & IMAGEIO_PLANAR) ? 1 : num_channels;
1559 if (format & IMAGEIO_COLUMNMAJOR)
1561 row_stride = pixel_stride;
1562 pixel_stride *= height;
1565 row_stride = width * pixel_stride;
1567 if (format & IMAGEIO_BGRFLIP)
1573 if ((format & IMAGEIO_AFLIP) && !(format & IMAGEIO_STRIP_ALPHA))
1575 order[3] = order[2];
1576 order[2] = order[1];
1577 order[1] = order[0];
1581 switch (format & (IMAGEIO_U8 | IMAGEIO_SINGLE | IMAGEIO_DOUBLE))
1584 if (!(dest_u8 = (uint8_t *)malloc(
1585 sizeof(uint8_t)*num_channels*num_pixels)))
1588 switch (num_channels)
1591 for (y = 0; y < height; ++y, src += width)
1592 for (x = 0, i = row_stride * y;
1594 ++x, i += pixel_stride)
1597 dest_u8[i] = (uint8_t)(0.299f * ((uint8_t *)&pixel)[0]
1598 + 0.587f * ((uint8_t *)&pixel)[1]
1599 + 0.114f * ((uint8_t *)&pixel)[2] + 0.5f);
1603 for (y = 0; y < height; ++y, src += width)
1604 for (x = 0, i = row_stride * y;
1606 ++x, i += pixel_stride)
1610 ((uint8_t *)&pixel)[order[0]];
1611 dest_u8[i + channel_stride] =
1612 ((uint8_t *)&pixel)[order[1]];
1613 dest_u8[i + channel_stride2] =
1614 ((uint8_t *)&pixel)[order[2]];
1618 for (y = 0; y < height; ++y, src += width)
1619 for (x = 0, i = row_stride * y;
1621 ++x, i += pixel_stride)
1625 ((uint8_t *)&pixel)[order[0]];
1626 dest_u8[i + channel_stride] =
1627 ((uint8_t *)&pixel)[order[1]];
1628 dest_u8[i + channel_stride2] =
1629 ((uint8_t *)&pixel)[order[2]];
1630 dest_u8[i + channel_stride3] =
1631 ((uint8_t *)&pixel)[order[3]];
1636 case IMAGEIO_SINGLE:
1637 if (!(dest_f32 = (
float *)malloc(
1638 sizeof(
float) * num_channels*num_pixels)))
1641 switch (num_channels)
1644 for (y = 0; y < height; ++y, src += width)
1645 for (x = 0, i = row_stride * y;
1650 dest_f32[i] = 1.172549019607843070675535e-3f
1651 * ((uint8_t *)&pixel)[0]
1652 + 2.301960784313725357840079e-3f
1653 * ((uint8_t *)&pixel)[1]
1654 + 4.470588235294117808150007e-4f
1655 * ((uint8_t *)&pixel)[2];
1659 for (y = 0; y < height; ++y, src += width)
1660 for (x = 0, i = row_stride * y;
1662 ++x, i += pixel_stride)
1666 ((uint8_t *)&pixel)[order[0]] / 255.0f;
1667 dest_f32[i + channel_stride] =
1668 ((uint8_t *)&pixel)[order[1]] / 255.0f;
1669 dest_f32[i + channel_stride2] =
1670 ((uint8_t *)&pixel)[order[2]] / 255.0f;
1674 for (y = 0; y < height; ++y, src += width)
1675 for (x = 0, i = row_stride * y;
1677 ++x, i += pixel_stride)
1681 ((uint8_t *)&pixel)[order[0]] / 255.0f;
1682 dest_f32[i + channel_stride] =
1683 ((uint8_t *)&pixel)[order[1]] / 255.0f;
1684 dest_f32[i + channel_stride2] =
1685 ((uint8_t *)&pixel)[order[2]] / 255.0f;
1686 dest_f32[i + channel_stride3] =
1687 ((uint8_t *)&pixel)[order[3]] / 255.0f;
1692 case IMAGEIO_DOUBLE:
1693 if (!(dest_f64 = (
double *)
1694 malloc(
sizeof(
double)*num_channels*num_pixels)))
1697 switch (num_channels)
1700 for (y = 0; y < height; ++y, src += width)
1701 for (x = 0, i = row_stride * y;
1703 ++x, i += pixel_stride)
1706 dest_f64[i] = 1.172549019607843070675535e-3
1707 * ((uint8_t *)&pixel)[0]
1708 + 2.301960784313725357840079e-3
1709 * ((uint8_t *)&pixel)[1]
1710 + 4.470588235294117808150007e-4
1711 * ((uint8_t *)&pixel)[2];
1715 for (y = 0; y < height; ++y, src += width)
1716 for (x = 0, i = row_stride * y;
1718 ++x, i += pixel_stride)
1722 ((uint8_t *)&pixel)[order[0]] / 255.0;
1723 dest_f64[i + channel_stride] =
1724 ((uint8_t *)&pixel)[order[1]] / 255.0;
1725 dest_f64[i + channel_stride2] =
1726 ((uint8_t *)&pixel)[order[2]] / 255.0;
1730 for (y = 0; y < height; ++y, src += width)
1731 for (x = 0, i = row_stride * y;
1733 ++x, i += pixel_stride)
1737 ((uint8_t *)&pixel)[order[0]] / 255.0;
1738 dest_f64[i + channel_stride] =
1739 ((uint8_t *)&pixel)[order[1]] / 255.0;
1740 dest_f64[i + channel_stride2] =
1741 ((uint8_t *)&pixel)[order[2]] / 255.0;
1742 dest_f64[i + channel_stride3] =
1743 ((uint8_t *)&pixel)[order[3]] / 255.0;
1758 const int num_pixels = width * height;
1759 const int num_channels = (format & IMAGEIO_GRAYSCALE) ?
1760 1 : ((format & IMAGEIO_STRIP_ALPHA) ? 3 : 4);
1761 const int channel_stride = (format & IMAGEIO_PLANAR) ? num_pixels : 1;
1762 const int channel_stride2 = 2 * channel_stride;
1763 const int channel_stride3 = 3 * channel_stride;
1764 double *src_f64 = (
double *)src;
1765 float *src_f32 = (
float *)src;
1766 uint8_t *src_u8 = (uint8_t *)src;
1767 uint8_t *dest, *dest_ptr;
1768 int order[4] = {0, 1, 2, 3};
1769 int i, x, y, pixel_stride, row_stride;
1771 if (!(dest = (uint8_t *)malloc(
sizeof(uint32_t)*num_pixels)))
1775 pixel_stride = (format & IMAGEIO_PLANAR) ? 1 : num_channels;
1777 if (format & IMAGEIO_COLUMNMAJOR)
1779 row_stride = pixel_stride;
1780 pixel_stride *= height;
1783 row_stride = width*pixel_stride;
1785 if (format & IMAGEIO_BGRFLIP)
1791 if ((format & IMAGEIO_AFLIP) && !(format & IMAGEIO_STRIP_ALPHA))
1793 order[3] = order[2];
1794 order[2] = order[1];
1795 order[1] = order[0];
1799 switch (format & (IMAGEIO_U8 | IMAGEIO_SINGLE | IMAGEIO_DOUBLE))
1802 switch (num_channels)
1805 for (y = 0; y < height; ++y, dest_ptr += 4 * width)
1806 for (x = 0, i = row_stride * y;
1808 ++x, i += pixel_stride)
1811 dest_ptr[4 * x + 1] =
1812 dest_ptr[4 * x + 2] = src_u8[i];
1813 dest_ptr[4 * x + 3] = 255;
1817 for (y = 0; y < height; ++y, dest_ptr += 4 * width)
1818 for (x = 0, i = row_stride * y;
1820 ++x, i += pixel_stride)
1822 dest_ptr[4 * x + order[0]] = src_u8[i];
1823 dest_ptr[4 * x + order[1]] = src_u8[i + channel_stride];
1824 dest_ptr[4 * x + order[2]] = src_u8[i + channel_stride2];
1825 dest_ptr[4 * x + 3] = 255;
1829 for (y = 0; y < height; ++y, dest_ptr += 4 * width)
1830 for (x = 0, i = row_stride * y;
1832 ++x, i += pixel_stride)
1834 dest_ptr[4 * x + order[0]] = src_u8[i];
1835 dest_ptr[4 * x + order[1]] = src_u8[i + channel_stride];
1836 dest_ptr[4 * x + order[2]] = src_u8[i + channel_stride2];
1837 dest_ptr[4 * x + order[3]] = src_u8[i + channel_stride3];
1842 case IMAGEIO_SINGLE:
1843 switch (num_channels)
1846 for (y = 0; y < height; ++y, dest_ptr += 4 * width)
1847 for (x = 0, i = row_stride * y;
1849 ++x, i += pixel_stride)
1852 dest_ptr[4 * x + 1] =
1853 dest_ptr[4 * x + 2] = ROUNDCLAMPF(src_f32[i]);
1854 dest_ptr[4 * x + 3] = 255;
1858 for (y = 0; y < height; ++y, dest_ptr += 4 * width)
1859 for (x = 0, i = row_stride * y;
1861 ++x, i += pixel_stride)
1863 dest_ptr[4 * x + order[0]] =
1864 ROUNDCLAMPF(src_f32[i]);
1865 dest_ptr[4 * x + order[1]] =
1866 ROUNDCLAMPF(src_f32[i + channel_stride]);
1867 dest_ptr[4 * x + order[2]] =
1868 ROUNDCLAMPF(src_f32[i + channel_stride2]);
1869 dest_ptr[4 * x + 3] = 255;
1873 for (y = 0; y < height; ++y, dest_ptr += 4 * width)
1874 for (x = 0, i = row_stride * y;
1876 ++x, i += pixel_stride)
1878 dest_ptr[4 * x + order[0]] =
1879 ROUNDCLAMPF(src_f32[i]);
1880 dest_ptr[4 * x + order[1]] =
1881 ROUNDCLAMPF(src_f32[i + channel_stride]);
1882 dest_ptr[4 * x + order[2]] =
1883 ROUNDCLAMPF(src_f32[i + channel_stride2]);
1884 dest_ptr[4 * x + order[3]] =
1885 ROUNDCLAMPF(src_f32[i + channel_stride3]);
1890 case IMAGEIO_DOUBLE:
1891 switch (num_channels)
1894 for (y = 0; y < height; ++y, dest_ptr += 4 * width)
1895 for (x = 0, i = row_stride * y;
1897 ++x, i += pixel_stride)
1900 dest_ptr[4 * x + 1] =
1901 dest_ptr[4 * x + 2] = ROUNDCLAMP(src_f64[i]);
1902 dest_ptr[4 * x + 3] = 255;
1906 for (y = 0; y < height; ++y, dest_ptr += 4 * width)
1907 for (x = 0, i = row_stride * y;
1909 ++x, i += pixel_stride)
1911 dest_ptr[4 * x + order[0]] =
1912 ROUNDCLAMP(src_f64[i]);
1913 dest_ptr[4 * x + order[1]] =
1914 ROUNDCLAMP(src_f64[i + channel_stride]);
1915 dest_ptr[4 * x + order[2]] =
1916 ROUNDCLAMP(src_f64[i + channel_stride2]);
1917 dest_ptr[4 * x + 3] = 255;;
1921 for (y = 0; y < height; ++y, dest_ptr += 4 * width)
1922 for (x = 0, i = row_stride * y;
1924 ++x, i += pixel_stride)
1926 dest_ptr[4 * x + order[0]] =
1927 ROUNDCLAMP(src_f64[i]);
1928 dest_ptr[4 * x + order[1]] =
1929 ROUNDCLAMP(src_f64[i + channel_stride]);
1930 dest_ptr[4 * x + order[2]] =
1931 ROUNDCLAMP(src_f64[i + channel_stride2]);
1932 dest_ptr[4 * x + order[3]] =
1933 ROUNDCLAMP(src_f64[i + channel_stride3]);
1942 return (uint32_t *)dest;
1962 if (!(file = fopen(filename,
"rb")))
1966 magic = ((uint32_t)getc(file));
1967 magic |= ((uint32_t)getc(file)) << 8;
1968 magic |= ((uint32_t)getc(file)) << 16;
1969 magic |= ((uint32_t)getc(file)) << 24;
1980 if ((magic & 0x0000FFFFL) == 0x00004D42L)
1981 strcpy(type,
"BMP");
1982 else if ((magic & 0x00FFFFFFL) == 0x00FFD8FFL)
1983 strcpy(type,
"JPEG");
1984 else if (magic == 0x474E5089L)
1985 strcpy(type,
"PNG");
1986 else if (magic == 0x002A4949L || magic == 0x2A004D4DL)
1987 strcpy(type,
"TIFF");
1988 else if (magic == 0x38464947L)
1989 strcpy(type,
"GIF");
1990 else if (magic == 0x474E4D8AL)
1991 strcpy(type,
"MNG");
1992 else if ((magic & 0xF0FF00FFL) == 0x0001000AL
1993 && ((magic >> 8) & 0xFF) < 6)
1994 strcpy(type,
"PCX");
2084 const char *filename,
unsigned format)
2087 uint32_t *image_u8 = NULL;
2094 if (!(file = fopen(filename,
"rb")))
2096 fprintf(stderr,
"Unable to open file \"%s\".\n", filename);
2100 if (!strcmp(type,
"BMP"))
2102 if (!
read_bmp(&image_u8, width, height, file))
2103 fprintf(stderr,
"Failed to read \"%s\".\n", filename);
2105 else if (!strcmp(type,
"JPEG"))
2108 if (!(read_jpeg(&image_u8, width, height, file)))
2109 fprintf(stderr,
"Failed to read \"%s\".\n", filename);
2111 fprintf(stderr,
"file \"%s\" is a JPEG image.\n"
2112 "Compile with USE_LIBJPEG to enable JPEG reading.\n",
2116 else if (!strcmp(type,
"PNG"))
2119 if (!(read_png(&image_u8, width, height, file)))
2120 fprintf(stderr,
"Failed to read \"%s\".\n", filename);
2122 fprintf(stderr,
"file \"%s\" is a PNG image.\n"
2123 "Compile with USE_LIBPNG to enable PNG reading.\n",
2127 else if (!strcmp(type,
"TIFF"))
2132 if (!(read_tiff(&image_u8, width, height, filename, 0)))
2133 fprintf(stderr,
"Failed to read \"%s\".\n", filename);
2137 fprintf(stderr,
"file \"%s\" is a TIFF image.\n"
2138 "Compile with USE_LIBTIFF to enable TIFF reading.\n",
2146 fprintf(stderr,
"file \"%s\" is a %s image.", filename, type);
2149 "file \"%s\" is an unrecognized format.", filename);
2150 fprintf(stderr,
"\nSorry, only "
2157 if (image_u8 && format)
2189 const char *filename,
unsigned format,
int quality)
2193 enum {BMP_FORMAT, JPEG_FORMAT, PNG_FORMAT, TIFF_FORMAT} fileformat;
2196 if (!image || width <= 0 || height <= 0)
2198 fprintf(stderr,
"Null image.\n");
2199 fprintf(stderr,
"Failed to write \"%s\".\n", filename);
2204 fileformat = BMP_FORMAT;
2208 fileformat = JPEG_FORMAT;
2210 fprintf(stderr,
"Failed to write \"%s\".\n", filename);
2211 fprintf(stderr,
"Compile with USE_LIBJPEG to enable JPEG writing.\n");
2217 fileformat = PNG_FORMAT;
2219 fprintf(stderr,
"Failed to write \"%s\".\n", filename);
2220 fprintf(stderr,
"Compile with USE_LIBPNG to enable PNG writing.\n");
2227 fileformat = TIFF_FORMAT;
2229 fprintf(stderr,
"Failed to write \"%s\".\n", filename);
2230 fprintf(stderr,
"Compile with USE_LIBTIFF to enable TIFF writing.\n");
2236 fprintf(stderr,
"Failed to write \"%s\".\n", filename);
2239 fprintf(stderr,
"GIF is not supported. ");
2241 fprintf(stderr,
"MNG is not supported. ");
2243 fprintf(stderr,
"PCX is not supported. ");
2245 fprintf(stderr,
"Unable to determine format from extension.\n");
2247 fprintf(stderr,
"Sorry, only "
2252 if (!(file = fopen(filename,
"wb")))
2254 fprintf(stderr,
"Unable to write to file \"%s\".\n", filename);
2264 success =
write_bmp(image_u8, width, height, file);
2268 success = write_jpeg(image_u8, width, height, file, quality);
2278 success = write_png(image_u8, width, height, file);
2284 success = write_tiff(image_u8, width, height, filename);
2291 fprintf(stderr,
"Failed to write \"%s\".\n", filename);