| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| #define JPEG_INTERNALS |
| #include "jinclude.h" |
| #include "jpeglib.h" |
| |
| |
| |
| |
| typedef struct { |
| struct jpeg_color_converter pub; |
| |
| |
| INT32 * rgb_ycc_tab; |
| } my_color_converter; |
| |
| typedef my_color_converter * my_cconvert_ptr; |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| #define SCALEBITS 16 |
| #define CBCR_OFFSET ((INT32) CENTERJSAMPLE << SCALEBITS) |
| #define ONE_HALF ((INT32) 1 << (SCALEBITS-1)) |
| #define FIX(x) ((INT32) ((x) * (1L<<SCALEBITS) + 0.5)) |
| |
| |
| |
| |
| |
| |
| |
| #define R_Y_OFF 0 |
| #define G_Y_OFF (1*(MAXJSAMPLE+1)) |
| #define B_Y_OFF (2*(MAXJSAMPLE+1)) |
| #define R_CB_OFF (3*(MAXJSAMPLE+1)) |
| #define G_CB_OFF (4*(MAXJSAMPLE+1)) |
| #define B_CB_OFF (5*(MAXJSAMPLE+1)) |
| #define R_CR_OFF B_CB_OFF |
| #define G_CR_OFF (6*(MAXJSAMPLE+1)) |
| #define B_CR_OFF (7*(MAXJSAMPLE+1)) |
| #define TABLE_SIZE (8*(MAXJSAMPLE+1)) |
| |
| |
| |
| |
| |
| |
| METHODDEF(void) |
| rgb_ycc_start (j_compress_ptr cinfo) |
| { |
| my_cconvert_ptr cconvert = (my_cconvert_ptr) cinfo->cconvert; |
| INT32 * rgb_ycc_tab; |
| INT32 i; |
| |
| |
| cconvert->rgb_ycc_tab = rgb_ycc_tab = (INT32 *) |
| (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE, |
| (TABLE_SIZE * SIZEOF(INT32))); |
| |
| for (i = 0; i <= MAXJSAMPLE; i++) { |
| rgb_ycc_tab[i+R_Y_OFF] = FIX(0.29900) * i; |
| rgb_ycc_tab[i+G_Y_OFF] = FIX(0.58700) * i; |
| rgb_ycc_tab[i+B_Y_OFF] = FIX(0.11400) * i + ONE_HALF; |
| rgb_ycc_tab[i+R_CB_OFF] = (-FIX(0.16874)) * i; |
| rgb_ycc_tab[i+G_CB_OFF] = (-FIX(0.33126)) * i; |
| |
| |
| |
| |
| rgb_ycc_tab[i+B_CB_OFF] = FIX(0.50000) * i + CBCR_OFFSET + ONE_HALF-1; |
| |
| |
| |
| rgb_ycc_tab[i+G_CR_OFF] = (-FIX(0.41869)) * i; |
| rgb_ycc_tab[i+B_CR_OFF] = (-FIX(0.08131)) * i; |
| } |
| } |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| METHODDEF(void) |
| rgb_ycc_convert (j_compress_ptr cinfo, |
| JSAMPARRAY input_buf, JSAMPIMAGE output_buf, |
| JDIMENSION output_row, int num_rows) |
| { |
| my_cconvert_ptr cconvert = (my_cconvert_ptr) cinfo->cconvert; |
| register INT32 * ctab = cconvert->rgb_ycc_tab; |
| register int r, g, b; |
| register JSAMPROW inptr; |
| register JSAMPROW outptr0, outptr1, outptr2; |
| register JDIMENSION col; |
| JDIMENSION num_cols = cinfo->image_width; |
| |
| while (--num_rows >= 0) { |
| inptr = *input_buf++; |
| outptr0 = output_buf[0][output_row]; |
| outptr1 = output_buf[1][output_row]; |
| outptr2 = output_buf[2][output_row]; |
| output_row++; |
| for (col = 0; col < num_cols; col++) { |
| r = GETJSAMPLE(inptr[RGB_RED]); |
| g = GETJSAMPLE(inptr[RGB_GREEN]); |
| b = GETJSAMPLE(inptr[RGB_BLUE]); |
| |
| |
| |
| |
| |
| |
| outptr0[col] = (JSAMPLE) |
| ((ctab[r+R_Y_OFF] + ctab[g+G_Y_OFF] + ctab[b+B_Y_OFF]) |
| >> SCALEBITS); |
| |
| outptr1[col] = (JSAMPLE) |
| ((ctab[r+R_CB_OFF] + ctab[g+G_CB_OFF] + ctab[b+B_CB_OFF]) |
| >> SCALEBITS); |
| |
| outptr2[col] = (JSAMPLE) |
| ((ctab[r+R_CR_OFF] + ctab[g+G_CR_OFF] + ctab[b+B_CR_OFF]) |
| >> SCALEBITS); |
| inptr += RGB_PIXELSIZE; |
| } |
| } |
| } |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| METHODDEF(void) |
| rgb_gray_convert (j_compress_ptr cinfo, |
| JSAMPARRAY input_buf, JSAMPIMAGE output_buf, |
| JDIMENSION output_row, int num_rows) |
| { |
| my_cconvert_ptr cconvert = (my_cconvert_ptr) cinfo->cconvert; |
| register INT32 * ctab = cconvert->rgb_ycc_tab; |
| register int r, g, b; |
| register JSAMPROW inptr; |
| register JSAMPROW outptr; |
| register JDIMENSION col; |
| JDIMENSION num_cols = cinfo->image_width; |
| |
| while (--num_rows >= 0) { |
| inptr = *input_buf++; |
| outptr = output_buf[0][output_row++]; |
| for (col = 0; col < num_cols; col++) { |
| r = GETJSAMPLE(inptr[RGB_RED]); |
| g = GETJSAMPLE(inptr[RGB_GREEN]); |
| b = GETJSAMPLE(inptr[RGB_BLUE]); |
| |
| outptr[col] = (JSAMPLE) |
| ((ctab[r+R_Y_OFF] + ctab[g+G_Y_OFF] + ctab[b+B_Y_OFF]) |
| >> SCALEBITS); |
| inptr += RGB_PIXELSIZE; |
| } |
| } |
| } |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| METHODDEF(void) |
| cmyk_ycck_convert (j_compress_ptr cinfo, |
| JSAMPARRAY input_buf, JSAMPIMAGE output_buf, |
| JDIMENSION output_row, int num_rows) |
| { |
| my_cconvert_ptr cconvert = (my_cconvert_ptr) cinfo->cconvert; |
| register INT32 * ctab = cconvert->rgb_ycc_tab; |
| register int r, g, b; |
| register JSAMPROW inptr; |
| register JSAMPROW outptr0, outptr1, outptr2, outptr3; |
| register JDIMENSION col; |
| JDIMENSION num_cols = cinfo->image_width; |
| |
| while (--num_rows >= 0) { |
| inptr = *input_buf++; |
| outptr0 = output_buf[0][output_row]; |
| outptr1 = output_buf[1][output_row]; |
| outptr2 = output_buf[2][output_row]; |
| outptr3 = output_buf[3][output_row]; |
| output_row++; |
| for (col = 0; col < num_cols; col++) { |
| r = MAXJSAMPLE - GETJSAMPLE(inptr[0]); |
| g = MAXJSAMPLE - GETJSAMPLE(inptr[1]); |
| b = MAXJSAMPLE - GETJSAMPLE(inptr[2]); |
| |
| outptr3[col] = inptr[3]; |
| |
| |
| |
| |
| |
| |
| outptr0[col] = (JSAMPLE) |
| ((ctab[r+R_Y_OFF] + ctab[g+G_Y_OFF] + ctab[b+B_Y_OFF]) |
| >> SCALEBITS); |
| |
| outptr1[col] = (JSAMPLE) |
| ((ctab[r+R_CB_OFF] + ctab[g+G_CB_OFF] + ctab[b+B_CB_OFF]) |
| >> SCALEBITS); |
| |
| outptr2[col] = (JSAMPLE) |
| ((ctab[r+R_CR_OFF] + ctab[g+G_CR_OFF] + ctab[b+B_CR_OFF]) |
| >> SCALEBITS); |
| inptr += 4; |
| } |
| } |
| } |
| |
| |
| |
| |
| |
| |
| |
| |
| METHODDEF(void) |
| rgb_rgb1_convert (j_compress_ptr cinfo, |
| JSAMPARRAY input_buf, JSAMPIMAGE output_buf, |
| JDIMENSION output_row, int num_rows) |
| { |
| register int r, g, b; |
| register JSAMPROW inptr; |
| register JSAMPROW outptr0, outptr1, outptr2; |
| register JDIMENSION col; |
| JDIMENSION num_cols = cinfo->image_width; |
| |
| while (--num_rows >= 0) { |
| inptr = *input_buf++; |
| outptr0 = output_buf[0][output_row]; |
| outptr1 = output_buf[1][output_row]; |
| outptr2 = output_buf[2][output_row]; |
| output_row++; |
| for (col = 0; col < num_cols; col++) { |
| r = GETJSAMPLE(inptr[RGB_RED]); |
| g = GETJSAMPLE(inptr[RGB_GREEN]); |
| b = GETJSAMPLE(inptr[RGB_BLUE]); |
| |
| |
| |
| outptr0[col] = (JSAMPLE) ((r - g + CENTERJSAMPLE) & MAXJSAMPLE); |
| outptr1[col] = (JSAMPLE) g; |
| outptr2[col] = (JSAMPLE) ((b - g + CENTERJSAMPLE) & MAXJSAMPLE); |
| inptr += RGB_PIXELSIZE; |
| } |
| } |
| } |
| |
| |
| |
| |
| |
| |
| |
| |
| METHODDEF(void) |
| grayscale_convert (j_compress_ptr cinfo, |
| JSAMPARRAY input_buf, JSAMPIMAGE output_buf, |
| JDIMENSION output_row, int num_rows) |
| { |
| int instride = cinfo->input_components; |
| register JSAMPROW inptr; |
| register JSAMPROW outptr; |
| register JDIMENSION col; |
| JDIMENSION num_cols = cinfo->image_width; |
| |
| while (--num_rows >= 0) { |
| inptr = *input_buf++; |
| outptr = output_buf[0][output_row++]; |
| for (col = 0; col < num_cols; col++) { |
| outptr[col] = inptr[0]; |
| inptr += instride; |
| } |
| } |
| } |
| |
| |
| |
| |
| |
| |
| |
| |
| METHODDEF(void) |
| rgb_convert (j_compress_ptr cinfo, |
| JSAMPARRAY input_buf, JSAMPIMAGE output_buf, |
| JDIMENSION output_row, int num_rows) |
| { |
| register JSAMPROW inptr; |
| register JSAMPROW outptr0, outptr1, outptr2; |
| register JDIMENSION col; |
| JDIMENSION num_cols = cinfo->image_width; |
| |
| while (--num_rows >= 0) { |
| inptr = *input_buf++; |
| outptr0 = output_buf[0][output_row]; |
| outptr1 = output_buf[1][output_row]; |
| outptr2 = output_buf[2][output_row]; |
| output_row++; |
| for (col = 0; col < num_cols; col++) { |
| |
| outptr0[col] = inptr[RGB_RED]; |
| outptr1[col] = inptr[RGB_GREEN]; |
| outptr2[col] = inptr[RGB_BLUE]; |
| inptr += RGB_PIXELSIZE; |
| } |
| } |
| } |
| |
| |
| |
| |
| |
| |
| |
| |
| METHODDEF(void) |
| null_convert (j_compress_ptr cinfo, |
| JSAMPARRAY input_buf, JSAMPIMAGE output_buf, |
| JDIMENSION output_row, int num_rows) |
| { |
| int ci; |
| register int nc = cinfo->num_components; |
| register JSAMPROW inptr; |
| register JSAMPROW outptr; |
| register JDIMENSION col; |
| JDIMENSION num_cols = cinfo->image_width; |
| |
| while (--num_rows >= 0) { |
| |
| for (ci = 0; ci < nc; ci++) { |
| inptr = input_buf[0] + ci; |
| outptr = output_buf[ci][output_row]; |
| for (col = 0; col < num_cols; col++) { |
| *outptr++ = *inptr; |
| inptr += nc; |
| } |
| } |
| input_buf++; |
| output_row++; |
| } |
| } |
| |
| |
| |
| |
| |
| |
| METHODDEF(void) |
| null_method (j_compress_ptr cinfo) |
| { |
| |
| } |
| |
| |
| |
| |
| |
| |
| GLOBAL(void) |
| jinit_color_converter (j_compress_ptr cinfo) |
| { |
| my_cconvert_ptr cconvert; |
| |
| cconvert = (my_cconvert_ptr) |
| (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE, |
| SIZEOF(my_color_converter)); |
| cinfo->cconvert = &cconvert->pub; |
| |
| cconvert->pub.start_pass = null_method; |
| |
| |
| switch (cinfo->in_color_space) { |
| case JCS_GRAYSCALE: |
| if (cinfo->input_components != 1) |
| ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE); |
| break; |
| |
| case JCS_RGB: |
| if (cinfo->input_components != RGB_PIXELSIZE) |
| ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE); |
| break; |
| |
| case JCS_YCbCr: |
| if (cinfo->input_components != 3) |
| ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE); |
| break; |
| |
| case JCS_CMYK: |
| case JCS_YCCK: |
| if (cinfo->input_components != 4) |
| ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE); |
| break; |
| |
| default: |
| if (cinfo->input_components < 1) |
| ERREXIT(cinfo, JERR_BAD_IN_COLORSPACE); |
| break; |
| } |
| |
| |
| if (cinfo->color_transform && cinfo->jpeg_color_space != JCS_RGB) |
| ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL); |
| |
| |
| switch (cinfo->jpeg_color_space) { |
| case JCS_GRAYSCALE: |
| if (cinfo->num_components != 1) |
| ERREXIT(cinfo, JERR_BAD_J_COLORSPACE); |
| if (cinfo->in_color_space == JCS_GRAYSCALE || |
| cinfo->in_color_space == JCS_YCbCr) |
| cconvert->pub.color_convert = grayscale_convert; |
| else if (cinfo->in_color_space == JCS_RGB) { |
| cconvert->pub.start_pass = rgb_ycc_start; |
| cconvert->pub.color_convert = rgb_gray_convert; |
| } else |
| ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL); |
| break; |
| |
| case JCS_RGB: |
| if (cinfo->num_components != 3) |
| ERREXIT(cinfo, JERR_BAD_J_COLORSPACE); |
| if (cinfo->in_color_space == JCS_RGB) { |
| switch (cinfo->color_transform) { |
| case JCT_NONE: |
| cconvert->pub.color_convert = rgb_convert; |
| break; |
| case JCT_SUBTRACT_GREEN: |
| cconvert->pub.color_convert = rgb_rgb1_convert; |
| break; |
| default: |
| ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL); |
| break; |
| } |
| } else |
| ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL); |
| break; |
| |
| case JCS_YCbCr: |
| if (cinfo->num_components != 3) |
| ERREXIT(cinfo, JERR_BAD_J_COLORSPACE); |
| if (cinfo->in_color_space == JCS_RGB) { |
| cconvert->pub.start_pass = rgb_ycc_start; |
| cconvert->pub.color_convert = rgb_ycc_convert; |
| } else if (cinfo->in_color_space == JCS_YCbCr) |
| cconvert->pub.color_convert = null_convert; |
| else |
| ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL); |
| break; |
| |
| case JCS_CMYK: |
| if (cinfo->num_components != 4) |
| ERREXIT(cinfo, JERR_BAD_J_COLORSPACE); |
| if (cinfo->in_color_space == JCS_CMYK) |
| cconvert->pub.color_convert = null_convert; |
| else |
| ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL); |
| break; |
| |
| case JCS_YCCK: |
| if (cinfo->num_components != 4) |
| ERREXIT(cinfo, JERR_BAD_J_COLORSPACE); |
| if (cinfo->in_color_space == JCS_CMYK) { |
| cconvert->pub.start_pass = rgb_ycc_start; |
| cconvert->pub.color_convert = cmyk_ycck_convert; |
| } else if (cinfo->in_color_space == JCS_YCCK) |
| cconvert->pub.color_convert = null_convert; |
| else |
| ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL); |
| break; |
| |
| default: |
| if (cinfo->jpeg_color_space != cinfo->in_color_space || |
| cinfo->num_components != cinfo->input_components) |
| ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL); |
| cconvert->pub.color_convert = null_convert; |
| break; |
| } |
| } |