qjpeg/JPEGFile.cpp
changeset 76 9cbf4c7e7986
child 78 a55bf2fa3f74
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/qjpeg/JPEGFile.cpp	Mon Jan 22 00:45:57 2007 +0100
@@ -0,0 +1,214 @@
+#include <cstring>
+#include <cstdio>
+
+extern "C"
+{
+#include <stdio.h>
+#include <jpeglib.h>
+}
+
+#include "FloatPlane.h"
+#include "Image.h"
+#include "JPEGFile.h"
+
+JPEGFile::JPEGFile(const char *_filename)
+{
+    using namespace std;
+    filename = new char[std::strlen(_filename) + 1];
+    std::strcpy(filename, _filename);
+    srcinfo = new jpeg_decompress_struct;
+    jsrcerr = new jpeg_error_mgr;
+}
+
+void JPEGFile::readHeader()
+{
+    srcinfo->err = jpeg_std_error(jsrcerr);
+
+    jpeg_create_decompress(srcinfo);
+
+    inputh = fopen(filename, "rb");
+
+    jpeg_stdio_src(srcinfo, inputh);
+    jpeg_read_header(srcinfo, TRUE);
+}
+
+Image *JPEGFile::getiDCTImage()
+{
+    readHeader();
+
+    /* Set our JPEG decompression parameters */
+    srcinfo->dct_method = JDCT_FLOAT;
+
+    /* jpeglib don't do any postprocessing (scaling/colorq). */
+    srcinfo->raw_data_out = TRUE;
+    
+    /* Start decompression of pixels */
+    jpeg_start_decompress(srcinfo);
+
+    unsigned int nplanes = srcinfo->num_components;
+
+    /* Create the planes */
+    FloatPlane *planes = new FloatPlane[nplanes];
+    unsigned int width[nplanes];
+    unsigned int height[nplanes];
+    unsigned int max_width_in_blocks = 0;
+
+    /* Get the pointers to the planes */
+    float *bmp[srcinfo->out_color_components];
+    for (unsigned int i=0; i < nplanes; i++)
+    {
+        /* get the dimensions for every plane*/
+        width[i] = srcinfo->comp_info[i].downsampled_width;
+        height[i] = srcinfo->comp_info[i].downsampled_height;
+
+        if (srcinfo->comp_info[i].width_in_blocks > max_width_in_blocks)
+            max_width_in_blocks = srcinfo->comp_info[i].width_in_blocks;
+        /* Allocate and prepare bmp[] */
+        planes[i].allocate(width[i], height[i]);
+        bmp[i] = planes[i].ptr;
+    }
+
+    /* The function jpeg_read_raw_data will return an MCU per call */
+    int nscanlines = srcinfo->max_v_samp_factor*DCTSIZE;
+
+    /* Get memory for the buffer scanline pointers */
+    JSAMPARRAY *scanplanes = new JSAMPARRAY[nplanes];
+
+    /* Get memory for each scanline in the buffer */
+    for (unsigned int plane = 0; plane < nplanes; ++plane)
+    {
+        scanplanes[plane] = new JSAMPROW[nscanlines];
+        for (int isl = 0; isl < nscanlines; ++isl)
+            scanplanes[plane][isl] = new JSAMPLE[max_width_in_blocks * DCTSIZE];
+    }
+
+    unsigned int row[nplanes];
+    unsigned int div_v_factor[nplanes];
+    for (unsigned int plane = 0; plane < nplanes; ++plane)
+    {
+        row[plane] = 0;
+        div_v_factor[plane] = srcinfo->max_v_samp_factor /
+            srcinfo->comp_info[plane].v_samp_factor;
+    }
+
+    while (srcinfo->output_scanline < srcinfo->output_height) {
+        int read_scanlines =
+            jpeg_read_raw_data(srcinfo, scanplanes, nscanlines);
+        for (unsigned int plane = 0; plane < nplanes; ++plane)
+        {
+            if (row[plane] >= height[plane])
+                continue;
+            for (int isl = 0; isl < (read_scanlines / (int) div_v_factor[plane]); ++isl)
+            {
+                for (unsigned int column = 0; column < width[plane];
+                    ++column) {
+                    bmp[plane][row[plane] * width[plane] + column] =
+                        scanplanes[plane][isl][column];
+                }
+            ++row[plane];
+            }
+        }
+    }
+
+    jpeg_destroy_decompress(srcinfo);
+
+    fclose(inputh);
+
+    Image *i = new Image(planes, nplanes);
+
+    delete[] planes;
+    for (unsigned int plane = 0; plane < nplanes; ++plane)
+    {
+        for (int isl = 0; isl < nscanlines; ++isl)
+            delete[] scanplanes[plane][isl];
+        delete[] scanplanes[plane];
+    }
+    delete[] scanplanes;
+
+    return i;
+}
+
+Image *JPEGFile::getUnpackedImage()
+{
+    readHeader();
+
+    /* Set our JPEG decompression parameters */
+    srcinfo->do_fancy_upsampling = 1;
+    srcinfo->dct_method = JDCT_FLOAT;
+
+    /* Get the number of planes */
+    if (srcinfo->num_components == 1 &&
+            srcinfo->jpeg_color_space == JCS_GRAYSCALE) {
+        srcinfo->out_color_components = 1;
+        srcinfo->out_color_space = JCS_GRAYSCALE;
+    }
+    else {
+        srcinfo->out_color_components = 3;
+        srcinfo->out_color_space = JCS_RGB;
+    }
+
+    /* Start decompression of pixels */
+    jpeg_start_decompress(srcinfo);
+
+    unsigned int nplanes = srcinfo->out_color_components;
+
+    /* Create the planes */
+    FloatPlane *planes = new FloatPlane[nplanes];
+
+    /* Get the pointers to the planes */
+    float *bmp[srcinfo->out_color_components];
+    for (unsigned int i=0; i < nplanes; i++)
+    {
+        planes[i].allocate(srcinfo->output_width, srcinfo->output_height);
+        bmp[i] = planes[i].ptr;
+    }
+
+    /* Get the recommended number of buffer scanlines */
+    int nscanlines = srcinfo->rec_outbuf_height;
+
+    /* Get memory for the buffer scanline pointers */
+    JSAMPROW *scanlines = new JSAMPROW[nscanlines];
+
+    /* Get memory for each scanline in the buffer */
+    for (int isl = 0; isl < nscanlines; ++isl)
+    {
+        scanlines[isl] = new JSAMPLE[srcinfo->output_width * nplanes];
+    }
+
+    int row = 0;
+    while (srcinfo->output_scanline < srcinfo->output_height) {
+        int read_scanlines =
+            jpeg_read_scanlines(srcinfo, scanlines, nscanlines);
+        for (int isl = 0; isl < read_scanlines; ++isl)
+        {
+            for (unsigned int column = 0; column < srcinfo->output_width;
+                    ++column) {
+                for (unsigned int plane = 0; plane < nplanes; plane++)
+                    bmp[plane][row * srcinfo->output_width + column] =
+                        scanlines[isl][column * srcinfo->out_color_components
+                        + plane];
+            }
+            ++row;
+        }
+    }
+
+    jpeg_destroy_decompress(srcinfo);
+
+    fclose(inputh);
+
+    Image *i = new Image(planes, nplanes);
+
+    delete[] planes;
+    for (int isl = 0; isl < nscanlines; ++isl)
+        delete[] scanlines[isl];
+    delete[] scanlines;
+
+    return i;
+}
+
+JPEGFile::~JPEGFile()
+{
+    delete[] filename;
+    delete srcinfo;
+    delete jsrcerr;
+}