private int m_next_row;        /* index of next row to fill/empty in strip */

        /// <summary>
        /// Initialize postprocessing controller.
        /// </summary>
        public jpeg_d_post_controller(jpeg_decompress_struct cinfo, bool need_full_buffer)
        {
            m_cinfo = cinfo;

            /* Create the quantization buffer, if needed */
            if (cinfo.m_quantize_colors)
            {
                /* The buffer strip height is max_v_samp_factor, which is typically
                * an efficient number of rows for upsampling to return.
                * (In the presence of output rescaling, we might want to be smarter?)
                */
                m_strip_height = cinfo.m_max_v_samp_factor;

                if (need_full_buffer)
                {
                    /* Two-pass color quantization: need full-image storage. */
                    /* We round up the number of rows to a multiple of the strip height. */
                    m_whole_image = jpeg_common_struct.CreateSamplesArray(
                        cinfo.m_output_width * cinfo.m_out_color_components,
                        JpegUtils.jround_up(cinfo.m_output_height, m_strip_height));
                    m_whole_image.ErrorProcessor = cinfo;
                }
                else
                {
                    /* One-pass color quantization: just make a strip buffer. */
                    m_buffer = jpeg_common_struct.AllocJpegSamples(
                        cinfo.m_output_width * cinfo.m_out_color_components, m_strip_height);
                }
            }
        }
        private int m_next_row;                   /* index of next row to fill/empty in strip */

        /// <summary>
        /// Initialize postprocessing controller.
        /// </summary>
        public jpeg_d_post_controller(jpeg_decompress_struct cinfo, bool need_full_buffer)
        {
            m_cinfo = cinfo;

            /* Create the quantization buffer, if needed */
            if (cinfo.m_quantize_colors)
            {
                /* The buffer strip height is max_v_samp_factor, which is typically
                 * an efficient number of rows for upsampling to return.
                 * (In the presence of output rescaling, we might want to be smarter?)
                 */
                m_strip_height = cinfo.m_max_v_samp_factor;

                if (need_full_buffer)
                {
                    /* Two-pass color quantization: need full-image storage. */
                    /* We round up the number of rows to a multiple of the strip height. */
                    m_whole_image = jpeg_common_struct.CreateSamplesArray(
                        cinfo.m_output_width * cinfo.m_out_color_components,
                        JpegUtils.jround_up(cinfo.m_output_height, m_strip_height));
                    m_whole_image.ErrorProcessor = cinfo;
                }
                else
                {
                    /* One-pass color quantization: just make a strip buffer. */
                    m_buffer = jpeg_common_struct.AllocJpegSamples(
                        cinfo.m_output_width * cinfo.m_out_color_components, m_strip_height);
                }
            }
        }
Exemplo n.º 3
0
        public static JBLOCK[][] GetBuffer(jvirt_array <JBLOCK> arr)
        {
            var propertyInfo =
                typeof(jvirt_array <JBLOCK>).GetField("m_buffer", BindingFlags.Instance | BindingFlags.NonPublic);

            return((JBLOCK[][])propertyInfo?.GetValue(arr));
        }
Exemplo n.º 4
0
        private int cur_output_row;             /* next row# to write to virtual array */

        public bmp_dest_struct(jpeg_decompress_struct cinfo, bool is_os2)
        {
            this.cinfo  = cinfo;
            this.is_os2 = is_os2;

            if (cinfo.Out_color_space == J_COLOR_SPACE.JCS_GRAYSCALE)
            {
                m_putGrayRows = true;
            }
            else if (cinfo.Out_color_space == J_COLOR_SPACE.JCS_RGB)
            {
                if (cinfo.Quantize_colors)
                {
                    m_putGrayRows = true;
                }
                else
                {
                    m_putGrayRows = false;
                }
            }
            else
            {
                cinfo.ERREXIT((int)ADDON_MESSAGE_CODE.JERR_BMP_COLORSPACE);
            }

            /* Calculate output image dimensions so we can allocate space */
            cinfo.jpeg_calc_output_dimensions();

            /* Determine width of rows in the BMP file (padded to 4-byte boundary). */
            row_width  = cinfo.Output_width * cinfo.Output_components;
            data_width = row_width;
            while ((row_width & 3) != 0)
            {
                row_width++;
            }

            pad_bytes = row_width - data_width;

            /* Allocate space for inversion array, prepare for write pass */
            whole_image = jpeg_common_struct.CreateSamplesArray(row_width, cinfo.Output_height);
            whole_image.ErrorProcessor = cinfo;

            cur_output_row = 0;
            if (cinfo.Progress != null)
            {
                cdjpeg_progress_mgr progress = cinfo.Progress as cdjpeg_progress_mgr;
                if (progress != null)
                {
                    /* count file input as separate pass */
                    progress.total_extra_passes++;
                }
            }

            /* Create decompressor output buffer. */
            buffer        = jpeg_common_struct.AllocJpegSamples(row_width, 1);
            buffer_height = 1;
        }
        /// <summary>
        /// Initialize coefficient buffer controller.
        /// 
        /// Each passed coefficient array must be the right size for that
        /// coefficient: width_in_blocks wide and height_in_blocks high,
        /// with unit height at least v_samp_factor.
        /// </summary>
        public my_trans_c_coef_controller(jpeg_compress_struct cinfo, jvirt_array<JBLOCK>[] coef_arrays)
        {
            m_cinfo = cinfo;

            /* Save pointer to virtual arrays */
            m_whole_image = coef_arrays;

            /* Allocate and pre-zero space for dummy DCT blocks. */
            JBLOCK[] buffer = new JBLOCK[JpegConstants.C_MAX_BLOCKS_IN_MCU];
            for (int i = 0; i < JpegConstants.C_MAX_BLOCKS_IN_MCU; i++)
                buffer[i] = new JBLOCK();

            for (int i = 0; i < JpegConstants.C_MAX_BLOCKS_IN_MCU; i++)
            {
                m_dummy_buffer[i] = new JBLOCK[JpegConstants.C_MAX_BLOCKS_IN_MCU - i];
                for (int j = i; j < JpegConstants.C_MAX_BLOCKS_IN_MCU; j++)
                    m_dummy_buffer[i][j - i] = buffer[j];
            }
        }
Exemplo n.º 6
0
        public jpeg_d_coef_controller(jpeg_decompress_struct cinfo, bool need_full_buffer)
        {
            m_cinfo = cinfo;

            /* Create the coefficient buffer. */
            if (need_full_buffer)
            {
                /* Allocate a full-image virtual array for each component, */
                /* padded to a multiple of samp_factor DCT blocks in each direction. */
                /* Note we ask for a pre-zeroed array. */
                for (int ci = 0; ci < cinfo.m_num_components; ci++)
                {
                    m_whole_image[ci] = jpeg_common_struct.CreateBlocksArray(
                        JpegUtils.jround_up(cinfo.Comp_info[ci].Width_in_blocks, cinfo.Comp_info[ci].H_samp_factor),
                        JpegUtils.jround_up(cinfo.Comp_info[ci].height_in_blocks, cinfo.Comp_info[ci].V_samp_factor));
                    m_whole_image[ci].ErrorProcessor = cinfo;
                }

                m_useDummyConsumeData = false;
                m_decompressor = DecompressorType.Ordinary;
                m_coef_arrays = m_whole_image; /* link to virtual arrays */
            }
            else
            {
                /* We only need a single-MCU buffer. */
                JBLOCK[] buffer = new JBLOCK[JpegConstants.D_MAX_BLOCKS_IN_MCU];
                for (int i = 0; i < JpegConstants.D_MAX_BLOCKS_IN_MCU; i++)
                {
                    buffer[i] = new JBLOCK();
                    for (int ii = 0; ii < buffer[i].data.Length; ii++)
                        buffer[i].data[ii] = -12851;

                    m_MCU_buffer[i] = buffer[i];
                }

                m_useDummyConsumeData = true;
                m_decompressor = DecompressorType.OnePass;
                m_coef_arrays = null; /* flag for no virtual arrays */
            }
        }
Exemplo n.º 7
0
        private int cur_output_row;  /* next row# to write to virtual array */

        public bmp_dest_struct(jpeg_decompress_struct cinfo, bool is_os2)
        {
            this.cinfo = cinfo;
            this.is_os2 = is_os2;

            if (cinfo.Out_color_space == J_COLOR_SPACE.JCS_GRAYSCALE)
            {
                m_putGrayRows = true;
            }
            else if (cinfo.Out_color_space == J_COLOR_SPACE.JCS_RGB)
            {
                if (cinfo.Quantize_colors)
                    m_putGrayRows = true;
                else
                    m_putGrayRows = false;
            }
            else
            {
                cinfo.ERREXIT((int)ADDON_MESSAGE_CODE.JERR_BMP_COLORSPACE);
            }

            /* Calculate output image dimensions so we can allocate space */
            cinfo.jpeg_calc_output_dimensions();

            /* Determine width of rows in the BMP file (padded to 4-byte boundary). */
            row_width = cinfo.Output_width * cinfo.Output_components;
            data_width = row_width;
            while ((row_width & 3) != 0)
                row_width++;

            pad_bytes = row_width - data_width;

            /* Allocate space for inversion array, prepare for write pass */
            whole_image = jpeg_common_struct.CreateSamplesArray(row_width, cinfo.Output_height);
            whole_image.ErrorProcessor = cinfo;

            cur_output_row = 0;
            if (cinfo.Progress != null)
            {
                cdjpeg_progress_mgr progress = cinfo.Progress as cdjpeg_progress_mgr;
                if (progress != null)
                {
                    /* count file input as separate pass */
                    progress.total_extra_passes++;
                }
            }

            /* Create decompressor output buffer. */
            buffer = jpeg_common_struct.AllocJpegSamples(row_width, 1);
            buffer_height = 1;
        }
        /// <summary>
        /// Master selection of compression modules for transcoding.
        /// </summary>
        private void transencode_master_selection(jvirt_array<JBLOCK>[] coef_arrays)
        {
            /* Although we don't actually use input_components for transcoding, 
             * jcmaster.c's initial_setup will complain if input_components is 0.
             */
            m_input_components = 1;

            /* Initialize master control (includes parameter checking/processing) */
            jinit_c_master_control(true /* transcode only */);

            /* Entropy encoding: only Huffman coding supported. */
            if (m_progressive_mode)
                m_entropy = new phuff_entropy_encoder(this);
            else
                m_entropy = new huff_entropy_encoder(this);

            /* We need a special coefficient buffer controller. */
            m_coef = new my_trans_c_coef_controller(this, coef_arrays);
            m_marker = new jpeg_marker_writer(this);

            /* Write the datastream header (SOI, JFIF) immediately.
            * Frame and scan headers are postponed till later.
            * This lets application insert special markers after the SOI.
            */
            m_marker.write_file_header();
        }
        /// <summary>
        /// Compression initialization for writing raw-coefficient data. Useful for lossless transcoding.
        /// </summary>
        /// <param name="coef_arrays">The virtual arrays need not be filled or even realized at the time 
        /// <c>jpeg_write_coefficients</c> is called; indeed, the virtual arrays typically will be realized 
        /// during this routine and filled afterwards.
        /// </param>
        /// <remarks>Before calling this, all parameters and a data destination must be set up.
        /// Call <see cref="jpeg_finish_compress"/> to actually write the data.
        /// </remarks>
        public void jpeg_write_coefficients(jvirt_array<JBLOCK>[] coef_arrays)
        {
            if (m_global_state != JpegState.CSTATE_START)
                ERREXIT(J_MESSAGE_CODE.JERR_BAD_STATE, (int)m_global_state);

            /* Mark all tables to be written */
            jpeg_suppress_tables(false);

            /* (Re)initialize error mgr and destination modules */
            m_err.reset_error_mgr();
            m_dest.init_destination();

            /* Perform master selection of active modules */
            transencode_master_selection(coef_arrays);

            /* Wait for jpeg_finish_compress() call */
            m_next_scanline = 0;   /* so jpeg_write_marker works */
            m_global_state = JpegState.CSTATE_WRCOEFS;
        }
Exemplo n.º 10
0
        /// <summary>
        /// Master selection of compression modules for transcoding.
        /// </summary>
        private void transencode_master_selection(jvirt_array<JBLOCK>[] coef_arrays)
        {
            /* Initialize master control (includes parameter checking/processing) */
            jinit_c_master_control(true /* transcode only */);

            /* Entropy encoding: only Huffman or arithmetic coding. */
            if (arith_code)
                m_entropy = new arith_entropy_encoder(this);
            else
                m_entropy = new huff_entropy_encoder(this);

            /* We need a special coefficient buffer controller. */
            m_coef = new my_trans_c_coef_controller(this, coef_arrays);
            m_marker = new jpeg_marker_writer(this);

            /* Write the datastream header (SOI, JFIF) immediately.
            * Frame and scan headers are postponed till later.
            * This lets application insert special markers after the SOI.
            */
            m_marker.write_file_header();
        }
Exemplo n.º 11
0
        /// <summary>
        /// Read the file header; detects image size and component count.
        /// </summary>
        public override void start_input()
        {
            byte[] bmpfileheader = new byte[14];
            /* Read and verify the bitmap file header */
            if (!ReadOK(input_file, bmpfileheader, 0, 14))
                cinfo.ERREXIT(J_MESSAGE_CODE.JERR_INPUT_EOF);

            if (GET_2B(bmpfileheader, 0) != 0x4D42) /* 'BM' */
                cinfo.ERREXIT((int)ADDON_MESSAGE_CODE.JERR_BMP_NOT);
            
            int bfOffBits = GET_4B(bmpfileheader, 10);
            /* We ignore the remaining fileheader fields */

            /* The infoheader might be 12 bytes (OS/2 1.x), 40 bytes (Windows),
             * or 64 bytes (OS/2 2.x).  Check the first 4 bytes to find out which.
             */
            byte[] bmpinfoheader = new byte[64];
            if (!ReadOK(input_file, bmpinfoheader, 0, 4))
                cinfo.ERREXIT(J_MESSAGE_CODE.JERR_INPUT_EOF);

            int headerSize = GET_4B(bmpinfoheader, 0);
            if (headerSize < 12 || headerSize> 64)
                cinfo.ERREXIT((int)ADDON_MESSAGE_CODE.JERR_BMP_BADHEADER);

            if (!ReadOK(input_file, bmpinfoheader, 4, headerSize - 4))
                cinfo.ERREXIT(J_MESSAGE_CODE.JERR_INPUT_EOF);

            int biWidth = 0;      /* initialize to avoid compiler warning */
            int biHeight = 0;
            int biPlanes;
            int biCompression;
            int biXPelsPerMeter;
            int biYPelsPerMeter;
            int biClrUsed = 0;
            int mapentrysize = 0;       /* 0 indicates no colormap */
            switch (headerSize)
            {
                case 12:
                    /* Decode OS/2 1.x header (Microsoft calls this a BITMAPCOREHEADER) */
                    biWidth = GET_2B(bmpinfoheader, 4);
                    biHeight = GET_2B(bmpinfoheader, 6);
                    biPlanes = GET_2B(bmpinfoheader, 8);
                    bits_per_pixel = GET_2B(bmpinfoheader, 10);

                    switch (bits_per_pixel)
                    {
                        case 8:
                            /* colormapped image */
                            mapentrysize = 3;       /* OS/2 uses RGBTRIPLE colormap */
                            cinfo.TRACEMS(1, (int)ADDON_MESSAGE_CODE.JTRC_BMP_OS2_MAPPED, biWidth, biHeight);
                            break;
                        case 24:
                            /* RGB image */
                            cinfo.TRACEMS(1, (int)ADDON_MESSAGE_CODE.JTRC_BMP_OS2, biWidth, biHeight);
                            break;
                        default:
                            cinfo.ERREXIT((int)ADDON_MESSAGE_CODE.JERR_BMP_BADDEPTH);
                            break;
                    }

                    if (biPlanes != 1)
                        cinfo.ERREXIT((int)ADDON_MESSAGE_CODE.JERR_BMP_BADPLANES);
                    break;
                case 40:
                case 64:
                    /* Decode Windows 3.x header (Microsoft calls this a BITMAPINFOHEADER) */
                    /* or OS/2 2.x header, which has additional fields that we ignore */
                    biWidth = GET_4B(bmpinfoheader, 4);
                    biHeight = GET_4B(bmpinfoheader, 8);
                    biPlanes = GET_2B(bmpinfoheader, 12);
                    bits_per_pixel = GET_2B(bmpinfoheader, 14);
                    biCompression = GET_4B(bmpinfoheader, 16);
                    biXPelsPerMeter = GET_4B(bmpinfoheader, 24);
                    biYPelsPerMeter = GET_4B(bmpinfoheader, 28);
                    biClrUsed = GET_4B(bmpinfoheader, 32);
                    /* biSizeImage, biClrImportant fields are ignored */

                    switch (bits_per_pixel)
                    {
                        case 8:
                            /* colormapped image */
                            mapentrysize = 4;       /* Windows uses RGBQUAD colormap */
                            cinfo.TRACEMS(1, (int)ADDON_MESSAGE_CODE.JTRC_BMP_MAPPED, biWidth, biHeight);
                            break;
                        case 24:
                            /* RGB image */
                            cinfo.TRACEMS(1, (int)ADDON_MESSAGE_CODE.JTRC_BMP, biWidth, biHeight);
                            break;
                        default:
                            cinfo.ERREXIT((int)ADDON_MESSAGE_CODE.JERR_BMP_BADDEPTH);
                            break;
                    }

                    if (biPlanes != 1)
                        cinfo.ERREXIT((int)ADDON_MESSAGE_CODE.JERR_BMP_BADPLANES);
                    
                    if (biCompression != 0)
                        cinfo.ERREXIT((int)ADDON_MESSAGE_CODE.JERR_BMP_COMPRESSED);

                    if (biXPelsPerMeter > 0 && biYPelsPerMeter > 0)
                    {
                        /* Set JFIF density parameters from the BMP data */
                        cinfo.X_density = (short)(biXPelsPerMeter / 100); /* 100 cm per meter */
                        cinfo.Y_density = (short)(biYPelsPerMeter / 100);
                        cinfo.Density_unit = DensityUnit.DotsCm;
                    }
                    break;
                default:
                    cinfo.ERREXIT((int)ADDON_MESSAGE_CODE.JERR_BMP_BADHEADER);
                    break;
            }

            /* Compute distance to bitmap data --- will adjust for colormap below */
            int bPad = bfOffBits - (headerSize + 14);

            /* Read the colormap, if any */
            if (mapentrysize > 0)
            {
                if (biClrUsed <= 0)
                    biClrUsed = 256;        /* assume it's 256 */
                else if (biClrUsed > 256)
                    cinfo.ERREXIT((int)ADDON_MESSAGE_CODE.JERR_BMP_BADCMAP);

                /* Allocate space to store the colormap */
                colormap = jpeg_common_struct.AllocJpegSamples(biClrUsed, 3);
                /* and read it from the file */
                read_colormap(biClrUsed, mapentrysize);
                /* account for size of colormap */
                bPad -= biClrUsed * mapentrysize;
            }

            /* Skip any remaining pad bytes */
            if (bPad < 0)           /* incorrect bfOffBits value? */
                cinfo.ERREXIT((int)ADDON_MESSAGE_CODE.JERR_BMP_BADHEADER);

            while (--bPad >= 0)
                read_byte();

            /* Compute row width in file, including padding to 4-byte boundary */
            if (bits_per_pixel == 24)
                row_width = biWidth * 3;
            else
                row_width = biWidth;

            while ((row_width & 3) != 0)
                row_width++;

            /* Allocate space for inversion array, prepare for preload pass */
            whole_image = jpeg_common_struct.CreateSamplesArray(row_width, biHeight);
            whole_image.ErrorProcessor = cinfo;
            m_pixelRowsMethod = PixelRowsMethod.preload;
            if (cinfo.Progress != null)
            {
                cdjpeg_progress_mgr progress = cinfo.Progress as cdjpeg_progress_mgr;
                if (progress != null)
                {
                    /* count file input as separate pass */
                    progress.total_extra_passes++;
                }
            }

            /* Allocate one-row buffer for returned data */
            buffer = jpeg_common_struct.AllocJpegSamples(biWidth * 3, 1);
            buffer_height = 1;

            cinfo.In_color_space = J_COLOR_SPACE.JCS_RGB;
            cinfo.Input_components = 3;
            cinfo.Data_precision = 8;
            cinfo.Image_width = biWidth;
            cinfo.Image_height = biHeight;
        }
Exemplo n.º 12
0
        /// <summary>
        /// Read the file header; detects image size and component count.
        /// </summary>
        public override void start_input()
        {
            byte[] bmpfileheader = new byte[14];
            /* Read and verify the bitmap file header */
            if (!ReadOK(input_file, bmpfileheader, 0, 14))
            {
                cinfo.ERREXIT(J_MESSAGE_CODE.JERR_INPUT_EOF);
            }

            if (GET_2B(bmpfileheader, 0) != 0x4D42) /* 'BM' */
            {
                cinfo.ERREXIT((int)ADDON_MESSAGE_CODE.JERR_BMP_NOT);
            }

            int bfOffBits = GET_4B(bmpfileheader, 10);

            /* We ignore the remaining fileheader fields */

            /* The infoheader might be 12 bytes (OS/2 1.x), 40 bytes (Windows),
             * or 64 bytes (OS/2 2.x).  Check the first 4 bytes to find out which.
             */
            byte[] bmpinfoheader = new byte[64];
            if (!ReadOK(input_file, bmpinfoheader, 0, 4))
            {
                cinfo.ERREXIT(J_MESSAGE_CODE.JERR_INPUT_EOF);
            }

            int headerSize = GET_4B(bmpinfoheader, 0);

            if (headerSize < 12 || headerSize > 64)
            {
                cinfo.ERREXIT((int)ADDON_MESSAGE_CODE.JERR_BMP_BADHEADER);
            }

            if (!ReadOK(input_file, bmpinfoheader, 4, headerSize - 4))
            {
                cinfo.ERREXIT(J_MESSAGE_CODE.JERR_INPUT_EOF);
            }

            int biWidth  = 0;     /* initialize to avoid compiler warning */
            int biHeight = 0;
            int biPlanes = 0;
            int biCompression;
            int biXPelsPerMeter;
            int biYPelsPerMeter;
            int biClrUsed    = 0;
            int mapentrysize = 0;       /* 0 indicates no colormap */

            switch (headerSize)
            {
            case 12:
                /* Decode OS/2 1.x header (Microsoft calls this a BITMAPCOREHEADER) */
                biWidth        = GET_2B(bmpinfoheader, 4);
                biHeight       = GET_2B(bmpinfoheader, 6);
                biPlanes       = GET_2B(bmpinfoheader, 8);
                bits_per_pixel = GET_2B(bmpinfoheader, 10);

                switch (bits_per_pixel)
                {
                case 8:
                    /* colormapped image */
                    mapentrysize = 3;               /* OS/2 uses RGBTRIPLE colormap */
                    cinfo.TRACEMS(1, (int)ADDON_MESSAGE_CODE.JTRC_BMP_OS2_MAPPED, biWidth, biHeight);
                    break;

                case 24:
                    /* RGB image */
                    cinfo.TRACEMS(1, (int)ADDON_MESSAGE_CODE.JTRC_BMP_OS2, biWidth, biHeight);
                    break;

                default:
                    cinfo.ERREXIT((int)ADDON_MESSAGE_CODE.JERR_BMP_BADDEPTH);
                    break;
                }

                break;

            case 40:
            case 64:
                /* Decode Windows 3.x header (Microsoft calls this a BITMAPINFOHEADER) */
                /* or OS/2 2.x header, which has additional fields that we ignore */
                biWidth         = GET_4B(bmpinfoheader, 4);
                biHeight        = GET_4B(bmpinfoheader, 8);
                biPlanes        = GET_2B(bmpinfoheader, 12);
                bits_per_pixel  = GET_2B(bmpinfoheader, 14);
                biCompression   = GET_4B(bmpinfoheader, 16);
                biXPelsPerMeter = GET_4B(bmpinfoheader, 24);
                biYPelsPerMeter = GET_4B(bmpinfoheader, 28);
                biClrUsed       = GET_4B(bmpinfoheader, 32);
                /* biSizeImage, biClrImportant fields are ignored */

                switch (bits_per_pixel)
                {
                case 8:
                    /* colormapped image */
                    mapentrysize = 4;               /* Windows uses RGBQUAD colormap */
                    cinfo.TRACEMS(1, (int)ADDON_MESSAGE_CODE.JTRC_BMP_MAPPED, biWidth, biHeight);
                    break;

                case 24:
                    /* RGB image */
                    cinfo.TRACEMS(1, (int)ADDON_MESSAGE_CODE.JTRC_BMP, biWidth, biHeight);
                    break;

                case 32:
                    /* RGB image + Alpha channel */
                    cinfo.TRACEMS(1, (int)ADDON_MESSAGE_CODE.JTRC_BMP, biWidth, biHeight);
                    break;

                default:
                    cinfo.ERREXIT((int)ADDON_MESSAGE_CODE.JERR_BMP_BADDEPTH);
                    break;
                }

                if (biCompression != 0)
                {
                    cinfo.ERREXIT((int)ADDON_MESSAGE_CODE.JERR_BMP_COMPRESSED);
                }

                if (biXPelsPerMeter > 0 && biYPelsPerMeter > 0)
                {
                    /* Set JFIF density parameters from the BMP data */
                    cinfo.X_density    = (short)(biXPelsPerMeter / 100);  /* 100 cm per meter */
                    cinfo.Y_density    = (short)(biYPelsPerMeter / 100);
                    cinfo.Density_unit = DensityUnit.DotsCm;
                }
                break;

            default:
                cinfo.ERREXIT((int)ADDON_MESSAGE_CODE.JERR_BMP_BADHEADER);
                break;
            }

            if (biWidth <= 0 || biHeight <= 0)
            {
                cinfo.ERREXIT((int)ADDON_MESSAGE_CODE.JERR_BMP_EMPTY);
            }

            if (biPlanes != 1)
            {
                cinfo.ERREXIT((int)ADDON_MESSAGE_CODE.JERR_BMP_BADPLANES);
            }

            /* Compute distance to bitmap data --- will adjust for colormap below */
            int bPad = bfOffBits - (headerSize + 14);

            /* Read the colormap, if any */
            if (mapentrysize > 0)
            {
                if (biClrUsed <= 0)
                {
                    biClrUsed = 256;        /* assume it's 256 */
                }
                else if (biClrUsed > 256)
                {
                    cinfo.ERREXIT((int)ADDON_MESSAGE_CODE.JERR_BMP_BADCMAP);
                }

                /* Allocate space to store the colormap */
                colormap = jpeg_common_struct.AllocJpegSamples(biClrUsed, 3);
                /* and read it from the file */
                read_colormap(biClrUsed, mapentrysize);
                /* account for size of colormap */
                bPad -= biClrUsed * mapentrysize;
            }

            /* Skip any remaining pad bytes */
            if (bPad < 0)           /* incorrect bfOffBits value? */
            {
                cinfo.ERREXIT((int)ADDON_MESSAGE_CODE.JERR_BMP_BADHEADER);
            }

            while (--bPad >= 0)
            {
                read_byte();
            }

            /* Compute row width in file, including padding to 4-byte boundary */
            if (bits_per_pixel == 24)
            {
                row_width = biWidth * 3;
            }
            else if (bits_per_pixel == 32)
            {
                row_width = biWidth * 4;
            }
            else
            {
                row_width = biWidth;
            }

            while ((row_width & 3) != 0)
            {
                row_width++;
            }

            /* Allocate space for inversion array, prepare for preload pass */
            whole_image = jpeg_common_struct.CreateSamplesArray(row_width, biHeight);
            whole_image.ErrorProcessor = cinfo;

            m_pixelRowsMethod = PixelRowsMethod.preload;
            if (cinfo.Progress != null)
            {
                cdjpeg_progress_mgr progress = cinfo.Progress as cdjpeg_progress_mgr;
                if (progress != null)
                {
                    /* count file input as separate pass */
                    progress.total_extra_passes++;
                }
            }

            /* Allocate one-row buffer for returned data */
            buffer        = jpeg_common_struct.AllocJpegSamples(biWidth * 3, 1);
            buffer_height = 1;

            cinfo.In_color_space   = J_COLOR_SPACE.JCS_RGB;
            cinfo.Input_components = 3;
            cinfo.Data_precision   = 8;
            cinfo.Image_width      = biWidth;
            cinfo.Image_height     = biHeight;
        }
Exemplo n.º 13
0
        public static List<DiffDCT> CompareTwoDct(jvirt_array<JBLOCK>[] ABlock, jvirt_array<JBLOCK>[] BBlock, int h, int w,bool zero = false)
        {
            try
            {
                List<DiffDCT> diff = new List<DiffDCT>();
                if (ABlock.Length >= 3 && ABlock[0] != null)
                {
                    var a = ABlock[0].Access(0, h);
                    var b = BBlock[0].Access(0, h);
                    for (int i = 0; i < h; i++)
                    {

                        for (int j = 0; j < w; j++)
                        {
                            bool flag = false;
                            string s = "";
                            DiffDCT dif = new DiffDCT(new Point(i, j), s);
                            if (!zero)
                            {
                                for (int m = 0; m < 64; m++)
                                {

                                    if (a[i][j].data[m] != b[i][j].data[m])
                                    {
                                        //point.Add(new Point(i, j));
                                        //break;
                                        //if (m == 0 && fixDCT && a[i][j].data[m] % 2 == 0)
                                        //    tbTesting.AppendText(String.Format("{0} -> {1}", a[i][j].data[m], b[i][j].data[m])+Environment.NewLine);
                                        if (m == 0)
                                            dif.zero = 1;
                                        s += String.Format(" {0} : {1} -> {2} ", m, a[i][j].data[m], b[i][j].data[m]);
                                        flag = true;

                                    }
                                }

                                dif.s = s;
                                if (flag)
                                {
                                    diff.Add(dif);
                                }
                            }
                            else
                            {
                                if (a[i][j].data[0] != b[i][j].data[0])
                                {
                                    s += String.Format(" {0} : {1} -> {2} ", 0, a[i][j].data[0], b[i][j].data[0]);
                                    dif.s = s;
                                    diff.Add(dif);

                                }
                            }

                        }
                    }
                }
                return diff;
            }
            catch (Exception e)
            {
                MessageBox.Show(e.Message);
                return null;
            }
        }
Exemplo n.º 14
0
        public static string PrintDctTable(jvirt_array<JBLOCK>[] JBlock, int startRow, int numRow, int numOfCol = 5)
        {
            string t = "";
            try
            {
                if (JBlock.Length >= 3 && JBlock[0] != null)
                {
                    var b = JBlock[0].Access(startRow, numRow);
                    for (int i = 0; i < numRow; i++)
                    {

                        for (int j = 0; j < numOfCol; j++)
                        {
                            t += "Block " + (startRow + i).ToString() + ": " + j.ToString() + " " + Environment.NewLine;
                            for (int m = 0; m < 64; m++)
                            {
                                t += " " + b[i][j].data[m] + " ";
                            }
                            t += Environment.NewLine;
                        }
                    }
                }
            }
            catch (Exception e)
            {
                MessageBox.Show(e.Message);
            }

            return t;
        }