123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689 |
- #include "cmyk.h"
- #include "cdjpeg.h"
- #ifdef BMP_SUPPORTED
- #ifdef HAVE_UNSIGNED_CHAR
- typedef unsigned char U_CHAR;
- #define UCH(x) ((int)(x))
- #else
- #ifdef __CHAR_UNSIGNED__
- typedef char U_CHAR;
- #define UCH(x) ((int)(x))
- #else
- typedef char U_CHAR;
- #define UCH(x) ((int)(x) & 0xFF)
- #endif
- #endif
- #define ReadOK(file, buffer, len) \
- (JFREAD(file, buffer, len) == ((size_t)(len)))
- static int alpha_index[JPEG_NUMCS] = {
- -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 3, 3, 0, 0, -1
- };
- typedef struct _bmp_source_struct *bmp_source_ptr;
- typedef struct _bmp_source_struct {
- struct cjpeg_source_struct pub;
- j_compress_ptr cinfo;
- JSAMPARRAY colormap;
- jvirt_sarray_ptr whole_image;
- JDIMENSION source_row;
- JDIMENSION row_width;
- int bits_per_pixel;
- int cmap_length;
- boolean use_inversion_array;
- U_CHAR *iobuffer;
- } bmp_source_struct;
- LOCAL(int)
- read_byte(bmp_source_ptr sinfo)
- {
- register FILE *infile = sinfo->pub.input_file;
- register int c;
- if ((c = getc(infile)) == EOF)
- ERREXIT(sinfo->cinfo, JERR_INPUT_EOF);
- return c;
- }
- LOCAL(void)
- read_colormap(bmp_source_ptr sinfo, int cmaplen, int mapentrysize)
- {
- int i, gray = 1;
- switch (mapentrysize) {
- case 3:
-
- for (i = 0; i < cmaplen; i++) {
- sinfo->colormap[2][i] = (JSAMPLE)read_byte(sinfo);
- sinfo->colormap[1][i] = (JSAMPLE)read_byte(sinfo);
- sinfo->colormap[0][i] = (JSAMPLE)read_byte(sinfo);
- if (sinfo->colormap[2][i] != sinfo->colormap[1][i] ||
- sinfo->colormap[1][i] != sinfo->colormap[0][i])
- gray = 0;
- }
- break;
- case 4:
-
- for (i = 0; i < cmaplen; i++) {
- sinfo->colormap[2][i] = (JSAMPLE)read_byte(sinfo);
- sinfo->colormap[1][i] = (JSAMPLE)read_byte(sinfo);
- sinfo->colormap[0][i] = (JSAMPLE)read_byte(sinfo);
- (void)read_byte(sinfo);
- if (sinfo->colormap[2][i] != sinfo->colormap[1][i] ||
- sinfo->colormap[1][i] != sinfo->colormap[0][i])
- gray = 0;
- }
- break;
- default:
- ERREXIT(sinfo->cinfo, JERR_BMP_BADCMAP);
- break;
- }
- if (sinfo->cinfo->in_color_space == JCS_UNKNOWN && gray)
- sinfo->cinfo->in_color_space = JCS_GRAYSCALE;
- if (sinfo->cinfo->in_color_space == JCS_GRAYSCALE && !gray)
- ERREXIT(sinfo->cinfo, JERR_BAD_IN_COLORSPACE);
- }
- METHODDEF(JDIMENSION)
- get_8bit_row(j_compress_ptr cinfo, cjpeg_source_ptr sinfo)
- {
- bmp_source_ptr source = (bmp_source_ptr)sinfo;
- register JSAMPARRAY colormap = source->colormap;
- int cmaplen = source->cmap_length;
- JSAMPARRAY image_ptr;
- register int t;
- register JSAMPROW inptr, outptr;
- register JDIMENSION col;
- if (source->use_inversion_array) {
-
- source->source_row--;
- image_ptr = (*cinfo->mem->access_virt_sarray)
- ((j_common_ptr)cinfo, source->whole_image,
- source->source_row, (JDIMENSION)1, FALSE);
- inptr = image_ptr[0];
- } else {
- if (!ReadOK(source->pub.input_file, source->iobuffer, source->row_width))
- ERREXIT(cinfo, JERR_INPUT_EOF);
- inptr = source->iobuffer;
- }
-
- outptr = source->pub.buffer[0];
- if (cinfo->in_color_space == JCS_GRAYSCALE) {
- for (col = cinfo->image_width; col > 0; col--) {
- t = GETJSAMPLE(*inptr++);
- if (t >= cmaplen)
- ERREXIT(cinfo, JERR_BMP_OUTOFRANGE);
- *outptr++ = colormap[0][t];
- }
- } else if (cinfo->in_color_space == JCS_CMYK) {
- for (col = cinfo->image_width; col > 0; col--) {
- t = GETJSAMPLE(*inptr++);
- if (t >= cmaplen)
- ERREXIT(cinfo, JERR_BMP_OUTOFRANGE);
- rgb_to_cmyk(colormap[0][t], colormap[1][t], colormap[2][t], outptr,
- outptr + 1, outptr + 2, outptr + 3);
- outptr += 4;
- }
- } else {
- register int rindex = rgb_red[cinfo->in_color_space];
- register int gindex = rgb_green[cinfo->in_color_space];
- register int bindex = rgb_blue[cinfo->in_color_space];
- register int aindex = alpha_index[cinfo->in_color_space];
- register int ps = rgb_pixelsize[cinfo->in_color_space];
- if (aindex >= 0) {
- for (col = cinfo->image_width; col > 0; col--) {
- t = GETJSAMPLE(*inptr++);
- if (t >= cmaplen)
- ERREXIT(cinfo, JERR_BMP_OUTOFRANGE);
- outptr[rindex] = colormap[0][t];
- outptr[gindex] = colormap[1][t];
- outptr[bindex] = colormap[2][t];
- outptr[aindex] = 0xFF;
- outptr += ps;
- }
- } else {
- for (col = cinfo->image_width; col > 0; col--) {
- t = GETJSAMPLE(*inptr++);
- if (t >= cmaplen)
- ERREXIT(cinfo, JERR_BMP_OUTOFRANGE);
- outptr[rindex] = colormap[0][t];
- outptr[gindex] = colormap[1][t];
- outptr[bindex] = colormap[2][t];
- outptr += ps;
- }
- }
- }
- return 1;
- }
- METHODDEF(JDIMENSION)
- get_24bit_row(j_compress_ptr cinfo, cjpeg_source_ptr sinfo)
- {
- bmp_source_ptr source = (bmp_source_ptr)sinfo;
- JSAMPARRAY image_ptr;
- register JSAMPROW inptr, outptr;
- register JDIMENSION col;
- if (source->use_inversion_array) {
-
- source->source_row--;
- image_ptr = (*cinfo->mem->access_virt_sarray)
- ((j_common_ptr)cinfo, source->whole_image,
- source->source_row, (JDIMENSION)1, FALSE);
- inptr = image_ptr[0];
- } else {
- if (!ReadOK(source->pub.input_file, source->iobuffer, source->row_width))
- ERREXIT(cinfo, JERR_INPUT_EOF);
- inptr = source->iobuffer;
- }
-
- outptr = source->pub.buffer[0];
- if (cinfo->in_color_space == JCS_EXT_BGR) {
- MEMCOPY(outptr, inptr, source->row_width);
- } else if (cinfo->in_color_space == JCS_CMYK) {
- for (col = cinfo->image_width; col > 0; col--) {
-
- JSAMPLE b = *inptr++, g = *inptr++, r = *inptr++;
- rgb_to_cmyk(r, g, b, outptr, outptr + 1, outptr + 2, outptr + 3);
- outptr += 4;
- }
- } else {
- register int rindex = rgb_red[cinfo->in_color_space];
- register int gindex = rgb_green[cinfo->in_color_space];
- register int bindex = rgb_blue[cinfo->in_color_space];
- register int aindex = alpha_index[cinfo->in_color_space];
- register int ps = rgb_pixelsize[cinfo->in_color_space];
- if (aindex >= 0) {
- for (col = cinfo->image_width; col > 0; col--) {
- outptr[bindex] = *inptr++;
- outptr[gindex] = *inptr++;
- outptr[rindex] = *inptr++;
- outptr[aindex] = 0xFF;
- outptr += ps;
- }
- } else {
- for (col = cinfo->image_width; col > 0; col--) {
- outptr[bindex] = *inptr++;
- outptr[gindex] = *inptr++;
- outptr[rindex] = *inptr++;
- outptr += ps;
- }
- }
- }
- return 1;
- }
- METHODDEF(JDIMENSION)
- get_32bit_row(j_compress_ptr cinfo, cjpeg_source_ptr sinfo)
- {
- bmp_source_ptr source = (bmp_source_ptr)sinfo;
- JSAMPARRAY image_ptr;
- register JSAMPROW inptr, outptr;
- register JDIMENSION col;
- if (source->use_inversion_array) {
-
- source->source_row--;
- image_ptr = (*cinfo->mem->access_virt_sarray)
- ((j_common_ptr)cinfo, source->whole_image,
- source->source_row, (JDIMENSION)1, FALSE);
- inptr = image_ptr[0];
- } else {
- if (!ReadOK(source->pub.input_file, source->iobuffer, source->row_width))
- ERREXIT(cinfo, JERR_INPUT_EOF);
- inptr = source->iobuffer;
- }
-
- outptr = source->pub.buffer[0];
- if (cinfo->in_color_space == JCS_EXT_BGRX ||
- cinfo->in_color_space == JCS_EXT_BGRA) {
- MEMCOPY(outptr, inptr, source->row_width);
- } else if (cinfo->in_color_space == JCS_CMYK) {
- for (col = cinfo->image_width; col > 0; col--) {
-
- JSAMPLE b = *inptr++, g = *inptr++, r = *inptr++;
- rgb_to_cmyk(r, g, b, outptr, outptr + 1, outptr + 2, outptr + 3);
- inptr++;
- outptr += 4;
- }
- } else {
- register int rindex = rgb_red[cinfo->in_color_space];
- register int gindex = rgb_green[cinfo->in_color_space];
- register int bindex = rgb_blue[cinfo->in_color_space];
- register int aindex = alpha_index[cinfo->in_color_space];
- register int ps = rgb_pixelsize[cinfo->in_color_space];
- if (aindex >= 0) {
- for (col = cinfo->image_width; col > 0; col--) {
- outptr[bindex] = *inptr++;
- outptr[gindex] = *inptr++;
- outptr[rindex] = *inptr++;
- outptr[aindex] = *inptr++;
- outptr += ps;
- }
- } else {
- for (col = cinfo->image_width; col > 0; col--) {
- outptr[bindex] = *inptr++;
- outptr[gindex] = *inptr++;
- outptr[rindex] = *inptr++;
- inptr++;
- outptr += ps;
- }
- }
- }
- return 1;
- }
- METHODDEF(JDIMENSION)
- preload_image(j_compress_ptr cinfo, cjpeg_source_ptr sinfo)
- {
- bmp_source_ptr source = (bmp_source_ptr)sinfo;
- register FILE *infile = source->pub.input_file;
- register JSAMPROW out_ptr;
- JSAMPARRAY image_ptr;
- JDIMENSION row;
- cd_progress_ptr progress = (cd_progress_ptr)cinfo->progress;
-
- for (row = 0; row < cinfo->image_height; row++) {
- if (progress != NULL) {
- progress->pub.pass_counter = (long)row;
- progress->pub.pass_limit = (long)cinfo->image_height;
- (*progress->pub.progress_monitor) ((j_common_ptr)cinfo);
- }
- image_ptr = (*cinfo->mem->access_virt_sarray)
- ((j_common_ptr)cinfo, source->whole_image, row, (JDIMENSION)1, TRUE);
- out_ptr = image_ptr[0];
- if (fread(out_ptr, 1, source->row_width, infile) != source->row_width) {
- if (feof(infile))
- ERREXIT(cinfo, JERR_INPUT_EOF);
- else
- ERREXIT(cinfo, JERR_FILE_READ);
- }
- }
- if (progress != NULL)
- progress->completed_extra_passes++;
-
- switch (source->bits_per_pixel) {
- case 8:
- source->pub.get_pixel_rows = get_8bit_row;
- break;
- case 24:
- source->pub.get_pixel_rows = get_24bit_row;
- break;
- case 32:
- source->pub.get_pixel_rows = get_32bit_row;
- break;
- default:
- ERREXIT(cinfo, JERR_BMP_BADDEPTH);
- }
- source->source_row = cinfo->image_height;
-
- return (*source->pub.get_pixel_rows) (cinfo, sinfo);
- }
- METHODDEF(void)
- start_input_bmp(j_compress_ptr cinfo, cjpeg_source_ptr sinfo)
- {
- bmp_source_ptr source = (bmp_source_ptr)sinfo;
- U_CHAR bmpfileheader[14];
- U_CHAR bmpinfoheader[64];
- #define GET_2B(array, offset) \
- ((unsigned short)UCH(array[offset]) + \
- (((unsigned short)UCH(array[offset + 1])) << 8))
- #define GET_4B(array, offset) \
- ((unsigned int)UCH(array[offset]) + \
- (((unsigned int)UCH(array[offset + 1])) << 8) + \
- (((unsigned int)UCH(array[offset + 2])) << 16) + \
- (((unsigned int)UCH(array[offset + 3])) << 24))
- unsigned int bfOffBits;
- unsigned int headerSize;
- int biWidth;
- int biHeight;
- unsigned short biPlanes;
- unsigned int biCompression;
- int biXPelsPerMeter, biYPelsPerMeter;
- unsigned int biClrUsed = 0;
- int mapentrysize = 0;
- int bPad;
- JDIMENSION row_width = 0;
-
- if (!ReadOK(source->pub.input_file, bmpfileheader, 14))
- ERREXIT(cinfo, JERR_INPUT_EOF);
- if (GET_2B(bmpfileheader, 0) != 0x4D42)
- ERREXIT(cinfo, JERR_BMP_NOT);
- bfOffBits = GET_4B(bmpfileheader, 10);
-
-
- if (!ReadOK(source->pub.input_file, bmpinfoheader, 4))
- ERREXIT(cinfo, JERR_INPUT_EOF);
- headerSize = GET_4B(bmpinfoheader, 0);
- if (headerSize < 12 || headerSize > 64)
- ERREXIT(cinfo, JERR_BMP_BADHEADER);
- if (!ReadOK(source->pub.input_file, bmpinfoheader + 4, headerSize - 4))
- ERREXIT(cinfo, JERR_INPUT_EOF);
- switch (headerSize) {
- case 12:
-
- biWidth = (int)GET_2B(bmpinfoheader, 4);
- biHeight = (int)GET_2B(bmpinfoheader, 6);
- biPlanes = GET_2B(bmpinfoheader, 8);
- source->bits_per_pixel = (int)GET_2B(bmpinfoheader, 10);
- switch (source->bits_per_pixel) {
- case 8:
- mapentrysize = 3;
- TRACEMS2(cinfo, 1, JTRC_BMP_OS2_MAPPED, biWidth, biHeight);
- break;
- case 24:
- TRACEMS2(cinfo, 1, JTRC_BMP_OS2, biWidth, biHeight);
- break;
- default:
- ERREXIT(cinfo, JERR_BMP_BADDEPTH);
- break;
- }
- break;
- case 40:
- case 64:
-
-
- biWidth = (int)GET_4B(bmpinfoheader, 4);
- biHeight = (int)GET_4B(bmpinfoheader, 8);
- biPlanes = GET_2B(bmpinfoheader, 12);
- source->bits_per_pixel = (int)GET_2B(bmpinfoheader, 14);
- biCompression = GET_4B(bmpinfoheader, 16);
- biXPelsPerMeter = (int)GET_4B(bmpinfoheader, 24);
- biYPelsPerMeter = (int)GET_4B(bmpinfoheader, 28);
- biClrUsed = GET_4B(bmpinfoheader, 32);
-
- switch (source->bits_per_pixel) {
- case 8:
- mapentrysize = 4;
- TRACEMS2(cinfo, 1, JTRC_BMP_MAPPED, biWidth, biHeight);
- break;
- case 24:
- TRACEMS2(cinfo, 1, JTRC_BMP, biWidth, biHeight);
- break;
- case 32:
- TRACEMS2(cinfo, 1, JTRC_BMP, biWidth, biHeight);
- break;
- default:
- ERREXIT(cinfo, JERR_BMP_BADDEPTH);
- break;
- }
- if (biCompression != 0)
- ERREXIT(cinfo, JERR_BMP_COMPRESSED);
- if (biXPelsPerMeter > 0 && biYPelsPerMeter > 0) {
-
- cinfo->X_density = (UINT16)(biXPelsPerMeter / 100);
- cinfo->Y_density = (UINT16)(biYPelsPerMeter / 100);
- cinfo->density_unit = 2;
- }
- break;
- default:
- ERREXIT(cinfo, JERR_BMP_BADHEADER);
- return;
- }
- if (biWidth <= 0 || biHeight <= 0)
- ERREXIT(cinfo, JERR_BMP_EMPTY);
- if (biPlanes != 1)
- ERREXIT(cinfo, JERR_BMP_BADPLANES);
-
- bPad = bfOffBits - (headerSize + 14);
-
- if (mapentrysize > 0) {
- if (biClrUsed <= 0)
- biClrUsed = 256;
- else if (biClrUsed > 256)
- ERREXIT(cinfo, JERR_BMP_BADCMAP);
-
- source->colormap = (*cinfo->mem->alloc_sarray)
- ((j_common_ptr)cinfo, JPOOL_IMAGE, (JDIMENSION)biClrUsed, (JDIMENSION)3);
- source->cmap_length = (int)biClrUsed;
-
- read_colormap(source, (int)biClrUsed, mapentrysize);
-
- bPad -= biClrUsed * mapentrysize;
- }
-
- if (bPad < 0)
- ERREXIT(cinfo, JERR_BMP_BADHEADER);
- while (--bPad >= 0) {
- (void)read_byte(source);
- }
-
- switch (source->bits_per_pixel) {
- case 8:
- if (cinfo->in_color_space == JCS_UNKNOWN)
- cinfo->in_color_space = JCS_EXT_RGB;
- if (IsExtRGB(cinfo->in_color_space))
- cinfo->input_components = rgb_pixelsize[cinfo->in_color_space];
- else if (cinfo->in_color_space == JCS_GRAYSCALE)
- cinfo->input_components = 1;
- else if (cinfo->in_color_space == JCS_CMYK)
- cinfo->input_components = 4;
- else
- ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
- row_width = (JDIMENSION)biWidth;
- break;
- case 24:
- if (cinfo->in_color_space == JCS_UNKNOWN)
- cinfo->in_color_space = JCS_EXT_BGR;
- if (IsExtRGB(cinfo->in_color_space))
- cinfo->input_components = rgb_pixelsize[cinfo->in_color_space];
- else if (cinfo->in_color_space == JCS_CMYK)
- cinfo->input_components = 4;
- else
- ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
- row_width = (JDIMENSION)(biWidth * 3);
- break;
- case 32:
- if (cinfo->in_color_space == JCS_UNKNOWN)
- cinfo->in_color_space = JCS_EXT_BGRA;
- if (IsExtRGB(cinfo->in_color_space))
- cinfo->input_components = rgb_pixelsize[cinfo->in_color_space];
- else if (cinfo->in_color_space == JCS_CMYK)
- cinfo->input_components = 4;
- else
- ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE);
- row_width = (JDIMENSION)(biWidth * 4);
- break;
- default:
- ERREXIT(cinfo, JERR_BMP_BADDEPTH);
- }
- while ((row_width & 3) != 0) row_width++;
- source->row_width = row_width;
- if (source->use_inversion_array) {
-
- source->whole_image = (*cinfo->mem->request_virt_sarray)
- ((j_common_ptr)cinfo, JPOOL_IMAGE, FALSE,
- row_width, (JDIMENSION)biHeight, (JDIMENSION)1);
- source->pub.get_pixel_rows = preload_image;
- if (cinfo->progress != NULL) {
- cd_progress_ptr progress = (cd_progress_ptr)cinfo->progress;
- progress->total_extra_passes++;
- }
- } else {
- source->iobuffer = (U_CHAR *)
- (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE, row_width);
- switch (source->bits_per_pixel) {
- case 8:
- source->pub.get_pixel_rows = get_8bit_row;
- break;
- case 24:
- source->pub.get_pixel_rows = get_24bit_row;
- break;
- case 32:
- source->pub.get_pixel_rows = get_32bit_row;
- break;
- default:
- ERREXIT(cinfo, JERR_BMP_BADDEPTH);
- }
- }
-
- if ((unsigned long long)biWidth *
- (unsigned long long)cinfo->input_components > 0xFFFFFFFFULL)
- ERREXIT(cinfo, JERR_WIDTH_OVERFLOW);
-
- source->pub.buffer = (*cinfo->mem->alloc_sarray)
- ((j_common_ptr)cinfo, JPOOL_IMAGE,
- (JDIMENSION)(biWidth * cinfo->input_components), (JDIMENSION)1);
- source->pub.buffer_height = 1;
- cinfo->data_precision = 8;
- cinfo->image_width = (JDIMENSION)biWidth;
- cinfo->image_height = (JDIMENSION)biHeight;
- }
- METHODDEF(void)
- finish_input_bmp(j_compress_ptr cinfo, cjpeg_source_ptr sinfo)
- {
-
- }
- GLOBAL(cjpeg_source_ptr)
- jinit_read_bmp(j_compress_ptr cinfo, boolean use_inversion_array)
- {
- bmp_source_ptr source;
-
- source = (bmp_source_ptr)
- (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,
- sizeof(bmp_source_struct));
- source->cinfo = cinfo;
-
- source->pub.start_input = start_input_bmp;
- source->pub.finish_input = finish_input_bmp;
- source->use_inversion_array = use_inversion_array;
- return (cjpeg_source_ptr)source;
- }
- #endif
|