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[]
示例#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;
 }
 public static jvirt_array<BitMiracle.LibJpeg.Classic.JBLOCK>[] ReadCoefFromImage(string fullPath)
 {
     var img = new Bitmap(fullPath);
     var width = img.Width;
     var height = img.Height;
     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();
     oJpegDecompress.jpeg_finish_decompress();
     oFileStreamImage.Close();
     return JBlock;
 }
 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;
 }