Example #1
0
 private void WriteBuffer(int dataCount)
 {
     try
     {
         m_outfile.Write(m_buffer, 0, dataCount);
     }
     catch (IOException e)
     {
         m_cinfo.TraceMS(0, JMessageCode.JERR_FILE_WRITE, e.Message);
         m_cinfo.ErrExit(JMessageCode.JERR_FILE_WRITE);
     }
     catch (NotSupportedException e)
     {
         m_cinfo.TraceMS(0, JMessageCode.JERR_FILE_WRITE, e.Message);
         m_cinfo.ErrExit(JMessageCode.JERR_FILE_WRITE);
     }
     catch (ObjectDisposedException e)
     {
         m_cinfo.TraceMS(0, JMessageCode.JERR_FILE_WRITE, e.Message);
         m_cinfo.ErrExit(JMessageCode.JERR_FILE_WRITE);
     }
 }
Example #2
0
        private readonly bool m_need_context_rows; /* true if need rows above & below */

        public JpegDownsampler(JpegCompressStruct cinfo)
        {
            m_cinfo             = cinfo;
            m_need_context_rows = false;

            if (cinfo.CIR601sampling)
            {
                cinfo.ErrExit(JMessageCode.JERR_CCIR601_NOTIMPL);
            }

            /* Verify we can handle the sampling factors, and set up method pointers */
            var smoothok = true;

            for (var ci = 0; ci < cinfo.m_num_components; ci++)
            {
                var componentInfo = cinfo.Component_info[ci];

                /* Compute size of an "output group" for DCT scaling.  This many samples
                 * are to be converted from max_h_samp_factor * max_v_samp_factor pixels.
                 */
                var h_out_group = (componentInfo.H_samp_factor * componentInfo.DCT_h_scaled_size) /
                                  m_cinfo.min_DCT_h_scaled_size;
                var v_out_group = (componentInfo.V_samp_factor * componentInfo.DCT_v_scaled_size) /
                                  m_cinfo.min_DCT_v_scaled_size;
                var h_in_group = m_cinfo.m_max_h_samp_factor;
                var v_in_group = m_cinfo.m_max_v_samp_factor;
                rowgroup_height[ci] = v_out_group; /* save for use later */
                if (h_in_group == h_out_group && v_in_group == v_out_group)
                {
                    if (cinfo.m_input_smoothing != 0)
                    {
                        m_downSamplers[ci]  = DownSampleMethod.fullsize_smooth_downsampler;
                        m_need_context_rows = true;
                    }
                    else
                    {
                        m_downSamplers[ci] = DownSampleMethod.fullsize_downsampler;
                    }
                }
                else if (h_in_group == h_out_group * 2 && v_in_group == v_out_group)
                {
                    smoothok           = false;
                    m_downSamplers[ci] = DownSampleMethod.h2v1_downsampler;
                }
                else if (h_in_group == h_out_group * 2 && v_in_group == v_out_group * 2)
                {
                    if (cinfo.m_input_smoothing != 0)
                    {
                        m_downSamplers[ci]  = DownSampleMethod.h2v2_smooth_downsampler;
                        m_need_context_rows = true;
                    }
                    else
                    {
                        m_downSamplers[ci] = DownSampleMethod.h2v2_downsampler;
                    }
                }
                else if ((h_in_group % h_out_group) == 0 && (v_in_group % v_out_group) == 0)
                {
                    smoothok           = false;
                    m_downSamplers[ci] = DownSampleMethod.int_downsampler;
                    h_expand[ci]       = (byte)(h_in_group / h_out_group);
                    v_expand[ci]       = (byte)(v_in_group / v_out_group);
                }
                else
                {
                    cinfo.ErrExit(JMessageCode.JERR_FRACT_SAMPLE_NOTIMPL);
                }
            }

            if (cinfo.m_input_smoothing != 0 && !smoothok)
            {
                cinfo.TraceMS(0, JMessageCode.JTRC_SMOOTH_NOTIMPL);
            }
        }
Example #3
0
        /// <summary>
        /// Write frame header.
        /// This consists of DQT and SOFn markers,
        /// a conditional LSE marker and a conditional pseudo SOS marker.
        /// Note that we do not emit the SOF until we have emitted the DQT(s).
        /// This avoids compatibility problems with incorrect implementations that
        /// try to error-check the quant table numbers as soon as they see the SOF.
        /// </summary>
        public void WriteFrameHeader()
        {
            /* Emit DQT for each quantization table.
             * Note that emit_dqt() suppresses any duplicate tables.
             */
            var prec = 0;

            for (var ci = 0; ci < m_cinfo.m_num_components; ci++)
            {
                prec += EmitDQT(m_cinfo.Component_info[ci].Quant_tbl_no);
            }

            /* now prec is nonzero iff there are any 16-bit quant tables. */

            /* Check for a non-baseline specification.
             * Note we assume that Huffman table numbers won't be changed later.
             */
            bool is_baseline;

            if (m_cinfo.arith_code ||
                m_cinfo.m_progressive_mode ||
                m_cinfo.m_data_precision != 8 ||
                m_cinfo.BlockSize != JpegConstants.DCTSIZE)
            {
                is_baseline = false;
            }
            else
            {
                is_baseline = true;
                for (var ci = 0; ci < m_cinfo.m_num_components; ci++)
                {
                    if (m_cinfo.Component_info[ci].Dc_tbl_no > 1 || m_cinfo.Component_info[ci].Ac_tbl_no > 1)
                    {
                        is_baseline = false;
                    }
                }

                if (prec != 0 && is_baseline)
                {
                    is_baseline = false;
                    /* If it's baseline except for quantizer size, warn the user */
                    m_cinfo.TraceMS(0, JMessageCode.JTRC_16BIT_TABLES);
                }
            }

            /* Emit the proper SOF marker */
            if (m_cinfo.arith_code)
            {
                if (m_cinfo.m_progressive_mode)
                {
                    EmitSOF(JpegMarker.SOF10); /* SOF code for progressive arithmetic */
                }
                else
                {
                    EmitSOF(JpegMarker.SOF9);  /* SOF code for sequential arithmetic */
                }
            }
            else if (m_cinfo.m_progressive_mode)
            {
                EmitSOF(JpegMarker.SOF2);    /* SOF code for progressive Huffman */
            }
            else if (is_baseline)
            {
                EmitSOF(JpegMarker.SOF0);    /* SOF code for baseline implementation */
            }
            else
            {
                EmitSOF(JpegMarker.SOF1);    /* SOF code for non-baseline Huffman file */
            }

            /* Check to emit LSE inverse color transform specification marker */
            if (m_cinfo.ColorTransform != JColorTransform.JCT_NONE)
            {
                EmitLseIct();
            }

            /* Check to emit pseudo SOS marker */
            if (m_cinfo.m_progressive_mode && m_cinfo.BlockSize != JpegConstants.DCTSIZE)
            {
                EmitPseudoSOS();
            }
        }