BitMiracle.LibJpeg.Classic.jpeg_compress_struct.jpeg_set_colorspace C# (CSharp) Method

jpeg_set_colorspace() public method

Set the JPEG colorspace (property jpeg_compress_struct.Jpeg_color_space, and choose colorspace-dependent parameters appropriately.
See Special color spaces, 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 jpeg_set_colorspace before rather than after.
public jpeg_set_colorspace ( J_COLOR_SPACE colorspace ) : void
colorspace J_COLOR_SPACE The required colorspace.
return void
        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_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;

                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, 0x01, 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,
                        color_transform == J_COLOR_TRANSFORM.JCT_SUBTRACT_GREEN ? 1 : 0,
                        color_transform == J_COLOR_TRANSFORM.JCT_SUBTRACT_GREEN ? 1 : 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,
                        color_transform == J_COLOR_TRANSFORM.JCT_SUBTRACT_GREEN ? 1 : 0,
                        color_transform == J_COLOR_TRANSFORM.JCT_SUBTRACT_GREEN ? 1 : 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, 0x01, 2, 2, 0, 0, 0);
                    jpeg_set_colorspace_SET_COMP(1, 0x02, 1, 1, 1, 1, 1);
                    jpeg_set_colorspace_SET_COMP(2, 0x03, 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, 0x01, 2, 2, 0, 0, 0);
                    jpeg_set_colorspace_SET_COMP(1, 0x02, 1, 1, 1, 1, 1);
                    jpeg_set_colorspace_SET_COMP(2, 0x03, 1, 1, 1, 1, 1);
                    jpeg_set_colorspace_SET_COMP(3, 0x04, 2, 2, 0, 0, 0);
                    break;

                case J_COLOR_SPACE.JCS_BG_RGB:
                    m_write_JFIF_header = true; /* Write a JFIF marker */
                    JFIF_major_version = 2;   /* Set JFIF major version = 2 */
                    m_num_components = 3;
                    /* Add offset 0x20 to the normal R/G/B component IDs */
                    jpeg_set_colorspace_SET_COMP(0, 0x72 /* 'r' */, 1, 1, 0,
                        color_transform == J_COLOR_TRANSFORM.JCT_SUBTRACT_GREEN ? 1 : 0,
                        color_transform == J_COLOR_TRANSFORM.JCT_SUBTRACT_GREEN ? 1 : 0);
                    jpeg_set_colorspace_SET_COMP(1, 0x67 /* 'g' */, 1, 1, 0, 0, 0);
                    jpeg_set_colorspace_SET_COMP(2, 0x62 /* 'b' */, 1, 1, 0,
                        color_transform == J_COLOR_TRANSFORM.JCT_SUBTRACT_GREEN ? 1 : 0,
                        color_transform == J_COLOR_TRANSFORM.JCT_SUBTRACT_GREEN ? 1 : 0);
                    break;

                case J_COLOR_SPACE.JCS_BG_YCC:
                    m_write_JFIF_header = true; /* Write a JFIF marker */
                    JFIF_major_version = 2;   /* Set JFIF major version = 2 */
                    m_num_components = 3;
                    /* Add offset 0x20 to the normal Cb/Cr component IDs */
                    /* We default to 2x2 subsamples of chrominance */
                    jpeg_set_colorspace_SET_COMP(0, 0x01, 2, 2, 0, 0, 0);
                    jpeg_set_colorspace_SET_COMP(1, 0x22, 1, 1, 1, 1, 1);
                    jpeg_set_colorspace_SET_COMP(2, 0x23, 1, 1, 1, 1, 1);
                    break;

                default:
                    ERREXIT(J_MESSAGE_CODE.JERR_BAD_J_COLORSPACE);
                    break;
            }
        }

Usage Example

        /// <summary>
        /// Initializes the compression object with default parameters, then copy from the source object 
        /// all parameters needed for lossless transcoding.
        /// </summary>
        /// <param name="dstinfo">Target JPEG compression object.</param>
        /// <remarks>Parameters that can be varied without loss (such as scan script and 
        /// Huffman optimization) are left in their default states.</remarks>
        public void jpeg_copy_critical_parameters(jpeg_compress_struct dstinfo)
        {
            /* Safety check to ensure start_compress not called yet. */
            if (dstinfo.m_global_state != JpegState.CSTATE_START)
                ERREXIT(J_MESSAGE_CODE.JERR_BAD_STATE, (int)dstinfo.m_global_state);

            /* Copy fundamental image dimensions */
            dstinfo.m_image_width = m_image_width;
            dstinfo.m_image_height = m_image_height;
            dstinfo.m_input_components = m_num_components;
            dstinfo.m_in_color_space = m_jpeg_color_space;

            /* Initialize all parameters to default values */
            dstinfo.jpeg_set_defaults();

            /* jpeg_set_defaults may choose wrong colorspace, eg YCbCr if input is RGB.
            * Fix it to get the right header markers for the image colorspace.
            */
            dstinfo.jpeg_set_colorspace(m_jpeg_color_space);
            dstinfo.m_data_precision = m_data_precision;
            dstinfo.m_CCIR601_sampling = m_CCIR601_sampling;

            /* Copy the source's quantization tables. */
            for (int tblno = 0; tblno < JpegConstants.NUM_QUANT_TBLS; tblno++)
            {
                if (m_quant_tbl_ptrs[tblno] != null)
                {
                    if (dstinfo.m_quant_tbl_ptrs[tblno] == null)
                        dstinfo.m_quant_tbl_ptrs[tblno] = new JQUANT_TBL();

                    Buffer.BlockCopy(m_quant_tbl_ptrs[tblno].quantval, 0,
                        dstinfo.m_quant_tbl_ptrs[tblno].quantval, 0,
                        dstinfo.m_quant_tbl_ptrs[tblno].quantval.Length * sizeof(short));

                    dstinfo.m_quant_tbl_ptrs[tblno].Sent_table = false;
                }
            }

            /* Copy the source's per-component info.
            * Note we assume jpeg_set_defaults has allocated the dest comp_info array.
            */
            dstinfo.m_num_components = m_num_components;
            if (dstinfo.m_num_components < 1 || dstinfo.m_num_components> JpegConstants.MAX_COMPONENTS)
                ERREXIT(J_MESSAGE_CODE.JERR_COMPONENT_COUNT, dstinfo.m_num_components, JpegConstants.MAX_COMPONENTS);

            for (int ci = 0; ci < dstinfo.m_num_components; ci++)
            {
                dstinfo.Component_info[ci].Component_id = m_comp_info[ci].Component_id;
                dstinfo.Component_info[ci].H_samp_factor = m_comp_info[ci].H_samp_factor;
                dstinfo.Component_info[ci].V_samp_factor = m_comp_info[ci].V_samp_factor;
                dstinfo.Component_info[ci].Quant_tbl_no = m_comp_info[ci].Quant_tbl_no;

                /* Make sure saved quantization table for component matches the qtable
                * slot.  If not, the input file re-used this qtable slot.
                * IJG encoder currently cannot duplicate this.
                */
                int tblno = dstinfo.Component_info[ci].Quant_tbl_no;
                if (tblno < 0 || tblno >= JpegConstants.NUM_QUANT_TBLS || m_quant_tbl_ptrs[tblno] == null)
                    ERREXIT(J_MESSAGE_CODE.JERR_NO_QUANT_TABLE, tblno);

                JQUANT_TBL c_quant = m_comp_info[ci].quant_table;
                if (c_quant != null)
                {
                    JQUANT_TBL slot_quant = m_quant_tbl_ptrs[tblno];
                    for (int coefi = 0; coefi < JpegConstants.DCTSIZE2; coefi++)
                    {
                        if (c_quant.quantval[coefi] != slot_quant.quantval[coefi])
                            ERREXIT(J_MESSAGE_CODE.JERR_MISMATCHED_QUANT_TABLE, tblno);
                    }
                }
                /* Note: we do not copy the source's Huffman table assignments;
                * instead we rely on jpeg_set_colorspace to have made a suitable choice.
                */
            }

            /* Also copy JFIF version and resolution information, if available.
            * Strictly speaking this isn't "critical" info, but it's nearly
            * always appropriate to copy it if available.  In particular,
            * if the application chooses to copy JFIF 1.02 extension markers from
            * the source file, we need to copy the version to make sure we don't
            * emit a file that has 1.02 extensions but a claimed version of 1.01.
            * We will *not*, however, copy version info from mislabeled "2.01" files.
            */
            if (m_saw_JFIF_marker)
            {
                if (m_JFIF_major_version == 1)
                {
                    dstinfo.m_JFIF_major_version = m_JFIF_major_version;
                    dstinfo.m_JFIF_minor_version = m_JFIF_minor_version;
                }

                dstinfo.m_density_unit = m_density_unit;
                dstinfo.m_X_density = (short)m_X_density;
                dstinfo.m_Y_density = (short)m_Y_density;
            }
        }
All Usage Examples Of BitMiracle.LibJpeg.Classic.jpeg_compress_struct::jpeg_set_colorspace