/// <summary> /// Set default decompression parameters. /// </summary> private void default_decompress_parms() { /* Guess the input colorspace, and set output colorspace accordingly. */ /* (Wish JPEG committee had provided a real way to specify this...) */ /* Note application may override our guesses. */ switch (m_num_components) { case 1: m_jpeg_color_space = J_COLOR_SPACE.JCS_GRAYSCALE; m_out_color_space = J_COLOR_SPACE.JCS_GRAYSCALE; break; case 3: if (m_saw_JFIF_marker) { /* JFIF implies YCbCr */ m_jpeg_color_space = J_COLOR_SPACE.JCS_YCbCr; } else if (m_saw_Adobe_marker) { switch (m_Adobe_transform) { case 0: m_jpeg_color_space = J_COLOR_SPACE.JCS_RGB; break; case 1: m_jpeg_color_space = J_COLOR_SPACE.JCS_YCbCr; break; default: WARNMS(J_MESSAGE_CODE.JWRN_ADOBE_XFORM, m_Adobe_transform); m_jpeg_color_space = J_COLOR_SPACE.JCS_YCbCr; /* assume it's YCbCr */ break; } } else { /* Saw no special markers, try to guess from the component IDs */ int cid0 = m_comp_info[0].Component_id; int cid1 = m_comp_info[1].Component_id; int cid2 = m_comp_info[2].Component_id; if (cid0 == 1 && cid1 == 2 && cid2 == 3) { /* assume JFIF w/out marker */ m_jpeg_color_space = J_COLOR_SPACE.JCS_YCbCr; } else if (cid0 == 82 && cid1 == 71 && cid2 == 66) { /* ASCII 'R', 'G', 'B' */ m_jpeg_color_space = J_COLOR_SPACE.JCS_RGB; } else { TRACEMS(1, J_MESSAGE_CODE.JTRC_UNKNOWN_IDS, cid0, cid1, cid2); /* assume it's YCbCr */ m_jpeg_color_space = J_COLOR_SPACE.JCS_YCbCr; } } /* Always guess RGB is proper output colorspace. */ m_out_color_space = J_COLOR_SPACE.JCS_RGB; break; case 4: if (m_saw_Adobe_marker) { switch (m_Adobe_transform) { case 0: m_jpeg_color_space = J_COLOR_SPACE.JCS_CMYK; break; case 2: m_jpeg_color_space = J_COLOR_SPACE.JCS_YCCK; break; default: WARNMS(J_MESSAGE_CODE.JWRN_ADOBE_XFORM, m_Adobe_transform); /* assume it's YCCK */ m_jpeg_color_space = J_COLOR_SPACE.JCS_YCCK; break; } } else { /* No special markers, assume straight CMYK. */ m_jpeg_color_space = J_COLOR_SPACE.JCS_CMYK; } m_out_color_space = J_COLOR_SPACE.JCS_CMYK; break; default: m_jpeg_color_space = J_COLOR_SPACE.JCS_UNKNOWN; m_out_color_space = J_COLOR_SPACE.JCS_UNKNOWN; break; } /* Set defaults for other decompression parameters. */ m_scale_num = 1; /* 1:1 scaling */ m_scale_denom = 1; m_buffered_image = false; m_raw_data_out = false; m_dct_method = JpegConstants.JDCT_DEFAULT; m_do_fancy_upsampling = true; m_do_block_smoothing = true; m_quantize_colors = false; /* We set these in case application only sets quantize_colors. */ m_dither_mode = J_DITHER_MODE.JDITHER_FS; m_two_pass_quantize = true; m_desired_number_of_colors = 256; m_colormap = null; /* Initialize for no mode change in buffered-image mode. */ m_enable_1pass_quant = false; m_enable_external_quant = false; m_enable_2pass_quant = false; }
// Compression parameter setup aids /// <summary> /// Set the JPEG colorspace (property <see cref="jpeg_compress_struct.Jpeg_color_space"/>, /// and choose colorspace-dependent parameters appropriately. /// </summary> /// <param name="colorspace">The required colorspace.</param> /// <remarks>See <see href="c90654b9-f3f4-4319-80d1-979c73d84e76.htm" target="_self">Special color spaces</see>, /// below, before using this. A large number of parameters, including all per-component parameters, /// are set by this routine; if you want to twiddle individual parameters you should call /// <c>jpeg_set_colorspace</c> before rather than after.</remarks> /// <seealso href="ce3f6712-3633-4a58-af07-626a4fba9ae4.htm" target="_self">Compression parameter selection</seealso> /// <seealso href="c90654b9-f3f4-4319-80d1-979c73d84e76.htm" target="_self">Special color spaces</seealso> public void jpeg_set_colorspace(J_COLOR_SPACE colorspace) { int ci; /* Safety check to ensure start_compress not called yet. */ if (m_global_state != JpegState.CSTATE_START) ERREXIT(J_MESSAGE_CODE.JERR_BAD_STATE, (int)m_global_state); /* For all colorspaces, we use Q and Huff tables 0 for luminance components, * tables 1 for chrominance components. */ m_jpeg_color_space = colorspace; m_write_JFIF_header = false; /* No marker for non-JFIF colorspaces */ m_write_Adobe_marker = false; /* write no Adobe marker by default */ switch (colorspace) { case J_COLOR_SPACE.JCS_GRAYSCALE: m_write_JFIF_header = true; /* Write a JFIF marker */ m_num_components = 1; /* JFIF specifies component ID 1 */ jpeg_set_colorspace_SET_COMP(0, 1, 1, 1, 0, 0, 0); break; case J_COLOR_SPACE.JCS_RGB: m_write_Adobe_marker = true; /* write Adobe marker to flag RGB */ m_num_components = 3; jpeg_set_colorspace_SET_COMP(0, 0x52 /* 'R' */, 1, 1, 0, 0, 0); jpeg_set_colorspace_SET_COMP(1, 0x47 /* 'G' */, 1, 1, 0, 0, 0); jpeg_set_colorspace_SET_COMP(2, 0x42 /* 'B' */, 1, 1, 0, 0, 0); break; case J_COLOR_SPACE.JCS_YCbCr: m_write_JFIF_header = true; /* Write a JFIF marker */ m_num_components = 3; /* JFIF specifies component IDs 1,2,3 */ /* We default to 2x2 subsamples of chrominance */ jpeg_set_colorspace_SET_COMP(0, 1, 2, 2, 0, 0, 0); jpeg_set_colorspace_SET_COMP(1, 2, 1, 1, 1, 1, 1); jpeg_set_colorspace_SET_COMP(2, 3, 1, 1, 1, 1, 1); break; case J_COLOR_SPACE.JCS_CMYK: m_write_Adobe_marker = true; /* write Adobe marker to flag CMYK */ m_num_components = 4; jpeg_set_colorspace_SET_COMP(0, 0x43 /* 'C' */, 1, 1, 0, 0, 0); jpeg_set_colorspace_SET_COMP(1, 0x4D /* 'M' */, 1, 1, 0, 0, 0); jpeg_set_colorspace_SET_COMP(2, 0x59 /* 'Y' */, 1, 1, 0, 0, 0); jpeg_set_colorspace_SET_COMP(3, 0x4B /* 'K' */, 1, 1, 0, 0, 0); break; case J_COLOR_SPACE.JCS_YCCK: m_write_Adobe_marker = true; /* write Adobe marker to flag YCCK */ m_num_components = 4; jpeg_set_colorspace_SET_COMP(0, 1, 2, 2, 0, 0, 0); jpeg_set_colorspace_SET_COMP(1, 2, 1, 1, 1, 1, 1); jpeg_set_colorspace_SET_COMP(2, 3, 1, 1, 1, 1, 1); jpeg_set_colorspace_SET_COMP(3, 4, 2, 2, 0, 0, 0); break; case J_COLOR_SPACE.JCS_UNKNOWN: m_num_components = m_input_components; if (m_num_components < 1 || m_num_components > JpegConstants.MAX_COMPONENTS) ERREXIT(J_MESSAGE_CODE.JERR_COMPONENT_COUNT, m_num_components, JpegConstants.MAX_COMPONENTS); for (ci = 0; ci < m_num_components; ci++) { jpeg_set_colorspace_SET_COMP(ci, ci, 1, 1, 0, 0, 0); } break; default: ERREXIT(J_MESSAGE_CODE.JERR_BAD_J_COLORSPACE); break; } }
private bool TIFFjpeg_set_colorspace(J_COLOR_SPACE colorspace) { try { m_compression.jpeg_set_colorspace(colorspace); } catch (Exception) { return false; } return true; }
/// <summary> /// Maps input color spaces to best related color space for JPEG encoding. /// </summary> /// <param name="in_color_space"></param> /// <returns></returns> public J_COLOR_SPACE map_colorspace(J_COLOR_SPACE in_color_space) { switch(in_color_space) { case J_COLOR_SPACE.JCS_GRAYSCALE: case J_COLOR_SPACE.JCS_CMYK: case J_COLOR_SPACE.JCS_YCCK: case J_COLOR_SPACE.JCS_UNKNOWN: return in_color_space; case J_COLOR_SPACE.JCS_RGB: case J_COLOR_SPACE.JCS_YCbCr: return J_COLOR_SPACE.JCS_YCbCr; } ERREXIT(J_MESSAGE_CODE.JERR_BAD_IN_COLORSPACE); return J_COLOR_SPACE.JCS_UNKNOWN; // Unreachable code }
extern public static void jpeg_set_colorspace (ref jpeg_compress_struct cinfo, J_COLOR_SPACE colorspace);
static bool TIFFjpeg_set_colorspace(JPEGState sp, J_COLOR_SPACE colorspace) { try { libjpeg.jpeg_set_colorspace((jpeg_compress)sp.comm, colorspace); } catch { return false; } return true; }
// Set the JPEG colorspace, and choose colorspace-dependent default values. public static void jpeg_set_colorspace(jpeg_compress cinfo, J_COLOR_SPACE colorspace, J_SUBSAMPLING subsampling) { // Safety check to ensure start_compress not called yet. if(cinfo.global_state!=STATE.CSTART) ERREXIT1(cinfo, J_MESSAGE_CODE.JERR_BAD_STATE, cinfo.global_state); // For all colorspaces, we use Q and Huff tables 0 for luminance components, // tables 1 for chrominance components. cinfo.jpeg_color_space=colorspace; cinfo.write_JFIF_header=false; // No marker for non-JFIF colorspaces cinfo.write_Adobe_marker=false; // write no Adobe marker by default switch(colorspace) { case J_COLOR_SPACE.JCS_GRAYSCALE: cinfo.write_JFIF_header=true; // Write a JFIF marker cinfo.num_components=1; // JFIF specifies component ID 1 SET_COMP(cinfo, 0, 1, 1, 1, 0, 0, 0); break; case J_COLOR_SPACE.JCS_RGB: cinfo.write_Adobe_marker=true; // write Adobe marker to flag RGB cinfo.num_components=3; SET_COMP(cinfo, 0, 0x52, 1, 1, 0, 0, 0); // 0x52 = 'R' SET_COMP(cinfo, 1, 0x47, 1, 1, 0, 0, 0); // 0x47 = 'G' SET_COMP(cinfo, 2, 0x42, 1, 1, 0, 0, 0); // 0x42 = 'B' break; case J_COLOR_SPACE.JCS_YCbCr: cinfo.write_JFIF_header=true; // Write a JFIF marker cinfo.num_components=3; // JFIF specifies component IDs 1,2,3 if(cinfo.lossless||subsampling==J_SUBSAMPLING.JPEG444) { SET_COMP(cinfo, 0, 1, 1, 1, 0, 0, 0); SET_COMP(cinfo, 1, 2, 1, 1, 1, 1, 1); SET_COMP(cinfo, 2, 3, 1, 1, 1, 1, 1); } else if(subsampling==J_SUBSAMPLING.JPEG422) { // We default to 2x1 subsamples of chrominance SET_COMP(cinfo, 0, 1, 2, 1, 0, 0, 0); SET_COMP(cinfo, 1, 2, 1, 1, 1, 1, 1); SET_COMP(cinfo, 2, 3, 1, 1, 1, 1, 1); } else { // We default to 2x2 subsamples of chrominance SET_COMP(cinfo, 0, 1, 2, 2, 0, 0, 0); SET_COMP(cinfo, 1, 2, 1, 1, 1, 1, 1); SET_COMP(cinfo, 2, 3, 1, 1, 1, 1, 1); } break; case J_COLOR_SPACE.JCS_CMYK: cinfo.write_Adobe_marker=true; // write Adobe marker to flag CMYK cinfo.num_components=4; SET_COMP(cinfo, 0, 0x43, 1, 1, 0, 0, 0); // 0x43 = 'C' SET_COMP(cinfo, 1, 0x4D, 1, 1, 0, 0, 0); // 0x4D = 'M' SET_COMP(cinfo, 2, 0x59, 1, 1, 0, 0, 0); // 0x59 = 'Y' SET_COMP(cinfo, 3, 0x4B, 1, 1, 0, 0, 0); // 0x4B = 'K' break; case J_COLOR_SPACE.JCS_YCCK: cinfo.write_Adobe_marker=true; // write Adobe marker to flag YCCK cinfo.num_components=4; if(cinfo.lossless) { SET_COMP(cinfo, 0, 1, 1, 1, 0, 0, 0); SET_COMP(cinfo, 1, 2, 1, 1, 1, 1, 1); SET_COMP(cinfo, 2, 3, 1, 1, 1, 1, 1); SET_COMP(cinfo, 3, 4, 1, 1, 0, 0, 0); } else { SET_COMP(cinfo, 0, 1, 2, 2, 0, 0, 0); SET_COMP(cinfo, 1, 2, 1, 1, 1, 1, 1); SET_COMP(cinfo, 2, 3, 1, 1, 1, 1, 1); SET_COMP(cinfo, 3, 4, 2, 2, 0, 0, 0); } break; case J_COLOR_SPACE.JCS_UNKNOWN: cinfo.num_components=cinfo.input_components; if(cinfo.num_components<1||cinfo.num_components>MAX_COMPONENTS) ERREXIT2(cinfo, J_MESSAGE_CODE.JERR_COMPONENT_COUNT, cinfo.num_components, MAX_COMPONENTS); for(int ci=0; ci<cinfo.num_components; ci++) SET_COMP(cinfo, ci, ci, 1, 1, 0, 0, 0); break; default: ERREXIT(cinfo, J_MESSAGE_CODE.JERR_BAD_J_COLORSPACE); break; } }
// Set the JPEG colorspace, and choose colorspace-dependent default values. public static void jpeg_set_colorspace(jpeg_compress cinfo, J_COLOR_SPACE colorspace) { jpeg_set_colorspace(cinfo, colorspace, J_SUBSAMPLING.JPEG420); }
// Set the JPEG colorspace, and choose colorspace-dependent default values. public static void jpeg_set_colorspace(jpeg_compress cinfo, J_COLOR_SPACE colorspace, J_SUBSAMPLING subsampling) { // Safety check to ensure start_compress not called yet. if (cinfo.global_state != STATE.CSTART) { ERREXIT1(cinfo, J_MESSAGE_CODE.JERR_BAD_STATE, cinfo.global_state); } // For all colorspaces, we use Q and Huff tables 0 for luminance components, // tables 1 for chrominance components. cinfo.jpeg_color_space = colorspace; cinfo.write_JFIF_header = false; // No marker for non-JFIF colorspaces cinfo.write_Adobe_marker = false; // write no Adobe marker by default switch (colorspace) { case J_COLOR_SPACE.JCS_GRAYSCALE: cinfo.write_JFIF_header = true; // Write a JFIF marker cinfo.num_components = 1; // JFIF specifies component ID 1 SET_COMP(cinfo, 0, 1, 1, 1, 0, 0, 0); break; case J_COLOR_SPACE.JCS_RGB: cinfo.write_Adobe_marker = true; // write Adobe marker to flag RGB cinfo.num_components = 3; SET_COMP(cinfo, 0, 0x52, 1, 1, 0, 0, 0); // 0x52 = 'R' SET_COMP(cinfo, 1, 0x47, 1, 1, 0, 0, 0); // 0x47 = 'G' SET_COMP(cinfo, 2, 0x42, 1, 1, 0, 0, 0); // 0x42 = 'B' break; case J_COLOR_SPACE.JCS_YCbCr: cinfo.write_JFIF_header = true; // Write a JFIF marker cinfo.num_components = 3; // JFIF specifies component IDs 1,2,3 if (cinfo.lossless || subsampling == J_SUBSAMPLING.JPEG444) { SET_COMP(cinfo, 0, 1, 1, 1, 0, 0, 0); SET_COMP(cinfo, 1, 2, 1, 1, 1, 1, 1); SET_COMP(cinfo, 2, 3, 1, 1, 1, 1, 1); } else if (subsampling == J_SUBSAMPLING.JPEG422) { // We default to 2x1 subsamples of chrominance SET_COMP(cinfo, 0, 1, 2, 1, 0, 0, 0); SET_COMP(cinfo, 1, 2, 1, 1, 1, 1, 1); SET_COMP(cinfo, 2, 3, 1, 1, 1, 1, 1); } else { // We default to 2x2 subsamples of chrominance SET_COMP(cinfo, 0, 1, 2, 2, 0, 0, 0); SET_COMP(cinfo, 1, 2, 1, 1, 1, 1, 1); SET_COMP(cinfo, 2, 3, 1, 1, 1, 1, 1); } break; case J_COLOR_SPACE.JCS_CMYK: cinfo.write_Adobe_marker = true; // write Adobe marker to flag CMYK cinfo.num_components = 4; SET_COMP(cinfo, 0, 0x43, 1, 1, 0, 0, 0); // 0x43 = 'C' SET_COMP(cinfo, 1, 0x4D, 1, 1, 0, 0, 0); // 0x4D = 'M' SET_COMP(cinfo, 2, 0x59, 1, 1, 0, 0, 0); // 0x59 = 'Y' SET_COMP(cinfo, 3, 0x4B, 1, 1, 0, 0, 0); // 0x4B = 'K' break; case J_COLOR_SPACE.JCS_YCCK: cinfo.write_Adobe_marker = true; // write Adobe marker to flag YCCK cinfo.num_components = 4; if (cinfo.lossless) { SET_COMP(cinfo, 0, 1, 1, 1, 0, 0, 0); SET_COMP(cinfo, 1, 2, 1, 1, 1, 1, 1); SET_COMP(cinfo, 2, 3, 1, 1, 1, 1, 1); SET_COMP(cinfo, 3, 4, 1, 1, 0, 0, 0); } else { SET_COMP(cinfo, 0, 1, 2, 2, 0, 0, 0); SET_COMP(cinfo, 1, 2, 1, 1, 1, 1, 1); SET_COMP(cinfo, 2, 3, 1, 1, 1, 1, 1); SET_COMP(cinfo, 3, 4, 2, 2, 0, 0, 0); } break; case J_COLOR_SPACE.JCS_UNKNOWN: cinfo.num_components = cinfo.input_components; if (cinfo.num_components < 1 || cinfo.num_components > MAX_COMPONENTS) { ERREXIT2(cinfo, J_MESSAGE_CODE.JERR_COMPONENT_COUNT, cinfo.num_components, MAX_COMPONENTS); } for (int ci = 0; ci < cinfo.num_components; ci++) { SET_COMP(cinfo, ci, ci, 1, 1, 0, 0, 0); } break; default: ERREXIT(cinfo, J_MESSAGE_CODE.JERR_BAD_J_COLORSPACE); break; } }