Added structures for a bitmap of coefficients.
authorviric@mandarina
Mon, 22 Jan 2007 18:22:34 +0100
changeset 78 a55bf2fa3f74
parent 76 9cbf4c7e7986
child 79 290ffe01986a
Added structures for a bitmap of coefficients.
qjpeg/CoefsImage.cpp
qjpeg/CoefsImage.h
qjpeg/CoefsPlane.cpp
qjpeg/CoefsPlane.h
qjpeg/FloatPlane.cpp
qjpeg/FloatPlane.h
qjpeg/Image.h
qjpeg/JPEGFile.cpp
qjpeg/JPEGFile.h
qjpeg/Makefile
qjpeg/data.txt
qjpeg/testJPEGFile.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/qjpeg/CoefsImage.cpp	Mon Jan 22 18:22:34 2007 +0100
@@ -0,0 +1,42 @@
+#include "CoefsImage.h"
+#include "CoefsPlane.h"
+
+CoefsImage::CoefsImage()
+{
+    ready = false;
+}
+
+
+CoefsImage::CoefsImage(CoefsPlane _plane[], const unsigned int _components)
+{
+    components = _components;
+    plane = new CoefsPlane[3];
+    /* Copy CoefsPlanes */
+    for(unsigned int i=0; i<components; i++)
+        plane[i] = _plane[i];
+    ready = true;
+}
+
+void CoefsImage::setComponents(unsigned int n)
+{
+    if (ready == true)
+        delete[] plane;
+
+    components = n;
+
+    plane = new CoefsPlane[n];
+    ready = true;
+}
+
+void CoefsImage::free()
+{
+    if (!ready)
+        return;
+
+    for(unsigned int i=0; i<components; i++)
+        plane[i].free();
+
+    delete[] plane;
+
+    ready = false;
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/qjpeg/CoefsImage.h	Mon Jan 22 18:22:34 2007 +0100
@@ -0,0 +1,18 @@
+class CoefsPlane;
+
+class CoefsImage
+{
+    unsigned int components;
+    bool ready;
+
+public:
+    CoefsPlane *plane;
+
+    CoefsImage();
+    CoefsImage(CoefsPlane _plane[], const unsigned int _components);
+
+    void free();
+    void setComponents(unsigned int n);
+    void writePPM(const char *filename);
+    unsigned int getComponents();
+};
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/qjpeg/CoefsPlane.cpp	Mon Jan 22 18:22:34 2007 +0100
@@ -0,0 +1,49 @@
+#include "CoefsPlane.h"
+
+
+CoefsPlane::CoefsPlane()
+{
+    ready = false;
+}
+
+CoefsPlane::CoefsPlane(float *_ptr, const unsigned int _width_in_blocks, const unsigned int _height_in_blocks)
+{
+    ptr = _ptr;
+    width_in_blocks = _width_in_blocks;
+    height_in_blocks = _height_in_blocks;
+    ready = true;
+}
+
+CoefsPlane::CoefsPlane(const unsigned int _width_in_blocks, const unsigned int _height_in_blocks)
+{
+    allocate(_width_in_blocks, _height_in_blocks);
+
+    ready = true;
+}
+
+void CoefsPlane::allocate(const unsigned int _width_in_blocks, const unsigned int _height_in_blocks)
+{
+    width_in_blocks = _width_in_blocks;
+    height_in_blocks = _height_in_blocks;
+    ptr = new float[width_in_blocks*height_in_blocks*64];
+}
+
+unsigned int CoefsPlane::getWidthInBlocks()
+{
+    return width_in_blocks;
+}
+
+unsigned int CoefsPlane::getHeightInBlocks()
+{
+    return height_in_blocks;
+}
+
+float * CoefsPlane::getPtr()
+{
+    return ptr;
+}
+
+void CoefsPlane::free()
+{
+    delete[] ptr;
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/qjpeg/CoefsPlane.h	Mon Jan 22 18:22:34 2007 +0100
@@ -0,0 +1,37 @@
+class CoefsPlane
+{
+    unsigned int width_in_blocks;
+    unsigned int height_in_blocks;
+
+    bool ready;
+
+    /*enum { DCTSIZE = 8, DCTSIZE2 = 64 };*/
+
+
+public:
+    float *ptr;
+
+    void allocate(const unsigned int _width_in_blocks, const unsigned int _height_in_blocks);
+
+    CoefsPlane();
+    CoefsPlane(float *_ptr, const unsigned int _width_in_blocks,
+            const unsigned int _height_in_blocks);
+    CoefsPlane(const unsigned int _width_in_blocks,
+            const unsigned int _height_in_blocks);
+
+    unsigned int getWidthInBlocks();
+    unsigned int getHeightInBlocks();
+    float * getPtr();
+    void free();
+    inline void set(unsigned int brow, unsigned int bcolumn, 
+            unsigned int coef, float val)
+    {
+        ptr[((brow * width_in_blocks) + bcolumn)*64 + coef] = val;
+    }
+
+    inline float get(unsigned int brow, unsigned int bcolumn, 
+            unsigned int coef)
+    {
+        return ptr[((brow * width_in_blocks) + bcolumn)*64 + coef];
+    }
+};
--- a/qjpeg/FloatPlane.cpp	Mon Jan 22 00:45:57 2007 +0100
+++ b/qjpeg/FloatPlane.cpp	Mon Jan 22 18:22:34 2007 +0100
@@ -14,13 +14,13 @@
 
 void FloatPlane::setMaxMinValue(unsigned int max, unsigned int min)
 {
-	MAXVALUE=max;
-	MINVALUE=min;
+    MAXVALUE=max;
+    MINVALUE=min;
 }
 
 FloatPlane::FloatPlane(float *_ptr)
 {
-	ptr = _ptr;
+    ptr = _ptr;
     setMaxMinValue();
     ready = true;
 }
@@ -37,7 +37,7 @@
 {
     width = _width;
     height = _height;
-	ptr = new float[width*height];
+    ptr = new float[width*height];
     ready = true;
 }
 
--- a/qjpeg/FloatPlane.h	Mon Jan 22 00:45:57 2007 +0100
+++ b/qjpeg/FloatPlane.h	Mon Jan 22 18:22:34 2007 +0100
@@ -1,27 +1,36 @@
 class FloatPlane
 {
-	unsigned int width;
-	unsigned int height;
+    unsigned int width;
+    unsigned int height;
 
-	int MAXVALUE;
-	int MINVALUE;
+    int MAXVALUE;
+    int MINVALUE;
 
     bool ready;
 
     void setMaxMinValue(unsigned int max = 255, unsigned int min = 0);
 
 public:
-	float *ptr;
+    float *ptr;
 
-	FloatPlane();
-	FloatPlane(float *_ptr);
-	FloatPlane(const unsigned int _width, const unsigned int _height);
+    FloatPlane();
+    FloatPlane(float *_ptr);
+    FloatPlane(const unsigned int _width, const unsigned int _height);
 
-	void allocate(const unsigned int _width, const unsigned int _height);
-	unsigned int getWidth();
-	float * getPtr();
-	unsigned int getHeight();
-	unsigned int getMaxValue();
-	void writePGM(const char * filename);
-	void free();
+    void allocate(const unsigned int _width, const unsigned int _height);
+    unsigned int getWidth();
+    unsigned int getHeight();
+    unsigned int getMaxValue();
+    float * getPtr();
+    void writePGM(const char * filename);
+    void free();
+    inline void set(unsigned int row, unsigned int column, float val)
+    {
+        ptr[(row * width) + column] = val;
+    }
+
+    inline float get(unsigned int row, unsigned int column)
+    {
+        return ptr[(row * width) + column];
+    }
 };
--- a/qjpeg/Image.h	Mon Jan 22 00:45:57 2007 +0100
+++ b/qjpeg/Image.h	Mon Jan 22 18:22:34 2007 +0100
@@ -2,19 +2,19 @@
 
 class Image
 {
-	unsigned int components;
+    unsigned int components;
     bool ready;
 
     bool sameDimensions();
 
 public:
-	FloatPlane *plane;
+    FloatPlane *plane;
 
     Image();
-	Image(FloatPlane _plane[], const unsigned int _components);
+    Image(FloatPlane _plane[], const unsigned int _components);
 
     void free();
-	void setComponents(unsigned int n);
+    void setComponents(unsigned int n);
     void writePPM(const char *filename);
-	unsigned int getComponents();
+    unsigned int getComponents();
 };
--- a/qjpeg/JPEGFile.cpp	Mon Jan 22 00:45:57 2007 +0100
+++ b/qjpeg/JPEGFile.cpp	Mon Jan 22 18:22:34 2007 +0100
@@ -8,7 +8,9 @@
 }
 
 #include "FloatPlane.h"
+#include "CoefsPlane.h"
 #include "Image.h"
+#include "CoefsImage.h"
 #include "JPEGFile.h"
 
 JPEGFile::JPEGFile(const char *_filename)
@@ -206,6 +208,66 @@
     return i;
 }
 
+CoefsImage *JPEGFile::getCoefs()
+{
+    readHeader();
+
+    unsigned int nplanes = srcinfo->num_components;
+
+    /* Create the planes */
+    CoefsPlane *planes = new CoefsPlane[nplanes];
+    unsigned int width_in_blocks[nplanes];
+    unsigned int height_in_blocks[nplanes];
+    unsigned int max_width_in_blocks = 0;
+
+    /* Pointers to the planes of Image (FloatPlanes) */
+    float *bmp[srcinfo->out_color_components];
+    for (unsigned int i=0; i < nplanes; ++i)
+    {
+        /* get the dimensions for every plane*/
+        width_in_blocks[i] = srcinfo->comp_info[i].width_in_blocks;
+        height_in_blocks[i] = srcinfo->comp_info[i].height_in_blocks;
+
+        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_in_blocks[i], height_in_blocks[i]);
+        bmp[i] = planes[i].ptr;
+    }
+
+    jvirt_barray_ptr *arrays = jpeg_read_coefficients(srcinfo);
+
+    /* Load the coefs */
+    for(unsigned int plane=0; plane < nplanes; plane++)
+    {
+        for (unsigned int brow = 0; brow < height_in_blocks[plane]; ++brow)
+        {
+            JBLOCKARRAY buffer = (*srcinfo->mem->access_virt_barray)
+                ((j_common_ptr) srcinfo, arrays[plane],
+                 (JDIMENSION) brow,
+                 (JDIMENSION) 1, FALSE);
+            for (unsigned int bcolumn = 0; bcolumn < width_in_blocks[plane];
+                ++bcolumn) 
+            {
+                unsigned int offset = (brow*width_in_blocks[plane] +
+                        bcolumn)*64;
+                for (unsigned int c = 0; c < DCTSIZE2; ++c)
+                    bmp[plane][offset + c] = buffer[0][bcolumn][c];
+            }
+        }
+    }
+
+    jpeg_destroy_decompress(srcinfo);
+
+    fclose(inputh);
+
+    CoefsImage *i = new CoefsImage(planes, nplanes);
+
+    delete[] planes;
+
+    return i;
+}
+
 JPEGFile::~JPEGFile()
 {
     delete[] filename;
--- a/qjpeg/JPEGFile.h	Mon Jan 22 00:45:57 2007 +0100
+++ b/qjpeg/JPEGFile.h	Mon Jan 22 18:22:34 2007 +0100
@@ -1,6 +1,7 @@
 #include <cstdio>
 
 class Image;
+class CoefsImage;
 
 extern "C" {
 struct jpeg_decompress_struct;
@@ -9,21 +10,22 @@
 
 class JPEGFile
 {
-	char * filename;
-	Image *iDCTImage;
-	Image *unpackedImage;
+    char * filename;
+    Image *iDCTImage;
+    Image *unpackedImage;
 
     FILE *inputh;
 
-	struct jpeg_decompress_struct *srcinfo;
-	struct jpeg_error_mgr *jsrcerr;
+    struct jpeg_decompress_struct *srcinfo;
+    struct jpeg_error_mgr *jsrcerr;
 
-	void readHeader();
+    void readHeader();
 
 public:
-	JPEGFile (const char *_filename);
+    JPEGFile (const char *_filename);
     ~JPEGFile();
 
-	Image * getiDCTImage();
-	Image * getUnpackedImage();
+    Image * getiDCTImage();
+    Image * getUnpackedImage();
+    CoefsImage * getCoefs();
 };
--- a/qjpeg/Makefile	Mon Jan 22 00:45:57 2007 +0100
+++ b/qjpeg/Makefile	Mon Jan 22 18:22:34 2007 +0100
@@ -6,17 +6,26 @@
 # Tests
 tests: testFP testJPEGFile
 
-testFP: testFP.o FloatPlane.o
+testFP: testFP.o libimage.a
 	$(CXX) $(LDFLAGS) -lnetpbm -o $@ $^
-testJPEGFile: testJPEGFile.o FloatPlane.o Image.o JPEGFile.o
+testJPEGFile: testJPEGFile.o libimage.a
 	$(CXX) $(LDFLAGS) -lnetpbm -ljpeg -o $@ $^
+
 testFP.o: testFP.cpp FloatPlane.h
 testJPEGFile.o: testFP.cpp FloatPlane.h
-testJPEGFile.o: testJPEGFile.cpp Image.h JPEGFile.h
+testJPEGFile.o: testJPEGFile.cpp Image.h JPEGFile.h FloatPlane.h CoefsImage.h
+
 
 # API
+IMGLIBOBJECTS=FloatPlane.o CoefsPlane.o Image.o CoefsImage.o JPEGFile.o
+
+libimage.a: $(IMGLIBOBJECTS)
+	ar crs $@ $^
+
 FloatPlane.o: FloatPlane.cpp FloatPlane.h
+CoefsPlane.o: CoefsPlane.cpp CoefsPlane.h
 Image.o: Image.cpp FloatPlane.h Image.h
+CoefsImage.o: CoefsImage.cpp CoefsPlane.h CoefsImage.h
 JPEGFile.o: JPEGFile.cpp JPEGFile.h FloatPlane.h Image.h
 
 clean:
--- a/qjpeg/data.txt	Mon Jan 22 00:45:57 2007 +0100
+++ b/qjpeg/data.txt	Mon Jan 22 18:22:34 2007 +0100
@@ -5,3 +5,9 @@
 output from decompress() or jpeg_read_raw_data()
 --------------------------------
 JSAMPLE scanlines[0..planes][scanline][column]
+
+Coefficients in jpeg_read_coefficients()
+--------------------------------
+(The content of the virt array)
+JBLOCKARRAY buffer = access_virt_array()
+JCOEF buffer[0..nblockrows][0..nblockcolumns][0..63]
--- a/qjpeg/testJPEGFile.cpp	Mon Jan 22 00:45:57 2007 +0100
+++ b/qjpeg/testJPEGFile.cpp	Mon Jan 22 18:22:34 2007 +0100
@@ -1,6 +1,7 @@
 #include "JPEGFile.h"
 #include "Image.h"
 #include "FloatPlane.h"
+#include "CoefsImage.h"
 
 int main()
 {
@@ -18,6 +19,7 @@
     i->writePPM("hola.ppm");
     /* Remove the image and its planes */
     i->free();
+    delete i;
 
     i = a.getiDCTImage();
 
@@ -28,4 +30,12 @@
 
     i->free();
     delete i;
+
+    /* Coefs testing */
+    CoefsImage *ci;
+
+    ci = a.getCoefs();
+
+    ci->free();
+    delete ci;
 }