// 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, 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; } }