BitMiracle.LibJpeg.Classic.jpeg_decompress_struct.jpeg_copy_critical_parameters C# (CSharp) Method

jpeg_copy_critical_parameters() public method

Initializes the compression object with default parameters, then copy from the source object all parameters needed for lossless transcoding.
Parameters that can be varied without loss (such as scan script and Huffman optimization) are left in their default states.
public jpeg_copy_critical_parameters ( jpeg_compress_struct dstinfo ) : void
dstinfo jpeg_compress_struct Target JPEG compression object.
return void
        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;
            dstinfo.jpeg_width = Output_width;
            dstinfo.jpeg_height = Output_height;
            dstinfo.min_DCT_h_scaled_size = min_DCT_h_scaled_size;
            dstinfo.min_DCT_v_scaled_size = min_DCT_v_scaled_size;

            /* 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.
             * Note: Entropy table assignment in jpeg_set_colorspace depends
             * on color_transform.
             */
            dstinfo.color_transform = color_transform;
            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 entropy 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.
            */
            if (m_saw_JFIF_marker)
            {
                if (m_JFIF_major_version == 1 || m_JFIF_major_version == 2)
                {
                    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;
            }
        }

Usage Example

 public static string IncImageDCT(string fullPath)
 {
     string suffix = "testDCT";
     var img = new Bitmap(fullPath);
     var width = img.Width;
     var height = img.Height;
     int hd = height / 8;
     int wd = width / 8;
     img.Dispose();
     BitMiracle.LibJpeg.Classic.jpeg_decompress_struct oJpegDecompress = new BitMiracle.LibJpeg.Classic.jpeg_decompress_struct();
     System.IO.FileStream oFileStreamImage = new System.IO.FileStream(fullPath, System.IO.FileMode.Open, System.IO.FileAccess.Read);
     oJpegDecompress.jpeg_stdio_src(oFileStreamImage);
     oJpegDecompress.jpeg_read_header(true);
     BitMiracle.LibJpeg.Classic.jvirt_array<BitMiracle.LibJpeg.Classic.JBLOCK>[] JBlock = oJpegDecompress.jpeg_read_coefficients();
     var block = JBlock[0].Access(0, hd); // accessing the element
     for (int i = 0; i < hd; i++)
     {
         for (int j = 0; j < wd; j++)
         {
             short t = block[i][j].data[0];
             if ((t >= 0 && t % 2 == 1) || (t < 0 && t % 2 == 0))
             {
                 t--;
             }
             else if ((t >= 0 && t % 2 == 0) || (t < 0 && t % 2 == 1))
             {
                 t++;
             }
             block[i][j].data[0] = t;
         }
     }
     oJpegDecompress.jpeg_finish_decompress();
     oFileStreamImage.Close();
     ////
     string filenameNew = MyHelper.AppendFileName(fullPath, suffix);
     System.IO.FileStream objFileStreamMegaMap = System.IO.File.Create(filenameNew);
     BitMiracle.LibJpeg.Classic.jpeg_compress_struct oJpegCompress = new BitMiracle.LibJpeg.Classic.jpeg_compress_struct();
     oJpegCompress.jpeg_stdio_dest(objFileStreamMegaMap);
     oJpegDecompress.jpeg_copy_critical_parameters(oJpegCompress);
     oJpegCompress.Image_height = height;
     oJpegCompress.Image_width = width;
     oJpegCompress.jpeg_write_coefficients(JBlock);
     oJpegCompress.jpeg_finish_compress();
     objFileStreamMegaMap.Close();
     oJpegDecompress.jpeg_abort_decompress();
     return filenameNew;
 }