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

jpeg_read_coefficients() public method

Read or write the raw DCT coefficient arrays from a JPEG file (useful for lossless transcoding).
jpeg_read_header must be completed before calling this.
The entire image is read into a set of virtual coefficient-block arrays, one per component. The return value is an array of virtual-array descriptors.
An alternative usage is to simply obtain access to the coefficient arrays during a buffered-image mode decompression operation. This is allowed after any jpeg_finish_output call. The arrays can be accessed until jpeg_finish_decompress is called. Note that any call to the library may reposition the arrays, so don't rely on jvirt_array{T}.Access results to stay valid across library calls.
public jpeg_read_coefficients ( ) : jvirt_array[]
return jvirt_array[]
        public jvirt_array<JBLOCK>[] jpeg_read_coefficients()
        {
            if (m_global_state == JpegState.DSTATE_READY)
            {
                /* First call: initialize active modules */
                transdecode_master_selection();
                m_global_state = JpegState.DSTATE_RDCOEFS;
            }

            if (m_global_state == JpegState.DSTATE_RDCOEFS)
            {
                /* Absorb whole file into the coef buffer */
                for ( ; ; )
                {
                    ReadResult retcode;
                    /* Call progress monitor hook if present */
                    if (m_progress != null)
                        m_progress.Updated();

                    /* Absorb some more input */
                    retcode = m_inputctl.consume_input();
                    if (retcode == ReadResult.JPEG_SUSPENDED)
                        return null;

                    if (retcode == ReadResult.JPEG_REACHED_EOI)
                        break;

                    /* Advance progress counter if appropriate */
                    if (m_progress != null && (retcode == ReadResult.JPEG_ROW_COMPLETED || retcode == ReadResult.JPEG_REACHED_SOS))
                    {
                        m_progress.Pass_counter++;
                        if (m_progress.Pass_counter >= m_progress.Pass_limit)
                        {
                            /* startup underestimated number of scans; ratchet up one scan */
                            m_progress.Pass_limit += m_total_iMCU_rows;
                        }
                    }
                }

                /* Set state so that jpeg_finish_decompress does the right thing */
                m_global_state = JpegState.DSTATE_STOPPING;
            }

            /* At this point we should be in state DSTATE_STOPPING if being used
            * standalone, or in state DSTATE_BUFIMAGE if being invoked to get access
            * to the coefficients during a full buffered-image-mode decompression.
            */
            if ((m_global_state == JpegState.DSTATE_STOPPING || m_global_state == JpegState.DSTATE_BUFIMAGE) && m_buffered_image)
                return m_coef.GetCoefArrays();

            /* Oops, improper usage */
            ERREXIT(J_MESSAGE_CODE.JERR_BAD_STATE, (int)m_global_state);
            /* keep compiler happy */
            return null;
        }

Usage Example

Example #1
0
 static int[][] getDct(string filename)
 {
     jpeg_decompress_struct cinfo = new jpeg_decompress_struct();
     FileStream objFileStreamHeaderImage = new FileStream(filename, FileMode.Open, FileAccess.Read);
     cinfo.jpeg_stdio_src(objFileStreamHeaderImage);
     cinfo.jpeg_read_header(true);
     var coeffs = cinfo.jpeg_read_coefficients();
     const int size = 64;
     int height = cinfo.Image_height / size;
     int width = cinfo.Image_width / size;
     int[][] result = new int[height * width][];
     var dct = coeffs[0].Access(0, height);
     for (int i = 0; i < height * width; i++)
     {
         result[i] = new int[size];
     }
     for (int i = 0; i < height; i++)
     {
         for (int j = 0; j < width; j++)
         {
             for (int k = 0; k < 64; k++)
             {
                 result[i * width + j][k] = dct[i][j][k];
             }
         }
     }
     return result;
 }
All Usage Examples Of BitMiracle.LibJpeg.Classic.jpeg_decompress_struct::jpeg_read_coefficients