Click here to Skip to main content
Click here to Skip to main content
 
Add your own
alternative version
Go to top

Embedded Zerotree Image Codec with Bior97 SSE Optimized Wavelet-transform

, 20 Oct 2007
This article demonstrates the use of Embedded zero tree still image codec with JPEG 2000 wavelet-filter.
ezw_demo.zip
bin
ezwconsole.exe
ezw_demo_update_1_.zip
ezwconsole.exe
ezw_src.zip
src
Lib
Release
ezw_src_update_1_.zip
// ezwconsole.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include "ezwconsole.h"

#include "lib\codecy.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif


// The one and only application object
CWinApp theApp;

using namespace std;

double T1[3][3] = { 0.25678823529411759,  0.50412941176470583,   0.097905882352941176,
                   -0.1482235294117647,  -0.2909921568627451,   0.4392156862745098,
                   0.4392156862745098,   -0.36778823529411764,  -0.071427450980392146 };

double offset1[3][1] = {  16.0,
                         128.0,
                         128.0 };

//ycbcr = T1 * rgb + offset1

double T2[3][3] = { 1.1643835616438358,   3.0112439739763348e-007,   1.5960268873357037,
                    1.1643835616438358,   -0.39176253994145005,     -0.81296829216220534,
                    1.1643835616438358,   2.0172326395564593,       3.0542617452484697e-006 };

double offset2[3][1] = { 222.92161710919433,
                         -135.57540952296651,
                         276.83630579503154 };

//rgb = T2 * ycbcr - offset2


//global data
CodecY gCodec;

CodecY yCodec; 
CodecY uCodec;
CodecY vCodec;

void compress_gray(int argc, TCHAR* argv[]);
void decompress_gray(int argc, TCHAR* argv[]);

void compress_yuv(int argc, TCHAR* argv[]);
void decompress_yuv(int argc, TCHAR* argv[]);

void help();

int get_encoder_clsid(const WCHAR* format, CLSID* pClsid);
void change_extension(wchar_t* path, const wchar_t* ext);

void rgb2ycbcr(unsigned char& y, unsigned char& cb, unsigned char& cr, 
               unsigned char r, unsigned char g, unsigned char b);
void ycbcr2rgb(unsigned char& r, unsigned char& g, unsigned char& b,
               unsigned char y, unsigned char cb, unsigned char cr );
double round(double x);
void dump(const wchar_t* file, const unsigned char* data, unsigned int w, unsigned int h);


int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
{
	int nRetCode = 0;
        
        Gdiplus::GdiplusStartupInput gdiplusStartupInput;
        ULONG_PTR gdiplusToken;
        GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, NULL);
        
	// initialize MFC and print and error on failure
	if (!AfxWinInit(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0))
	{
		// TODO: change error code to suit your needs
		_tprintf(_T("Fatal Error: MFC initialization failed\n"));
		nRetCode = 1;
	}
	else
	{
		// TODO: code your application's behavior here.
                if (argc == 1+3 && wcscmp(argv[1], L"cgray") == 0)
                        compress_gray(argc, argv);
                else if (argc == 1+3 && wcscmp(argv[1], L"cyuv") == 0)
                        compress_yuv(argc, argv);
                else if (argc == 1+2 && wcscmp(argv[1], L"dgray") == 0)
                        decompress_gray(argc, argv);
                else if (argc == 1+2 && wcscmp(argv[1], L"dyuv") == 0)
                        decompress_yuv(argc, argv);
                else
                        help();

	}

        Gdiplus::GdiplusShutdown(gdiplusToken);

	return nRetCode;
}

void help()
{
        wprintf(L"ezwconsole cgray(cyuv) image_file quality(0-100) - to compress the image\n"  
                L"ezwconsole dgray(dyuv) compressed_file.ezw - to decompress it to ezw.bmp and open it in image viewer\n");
}

void compress_gray(int argc, TCHAR* argv[])
{
        Gdiplus::Bitmap image(argv[2]);
        if (image.GetLastStatus() == Gdiplus::Ok) {
                wprintf(L"opened %s %dx%d\n", argv[2], image.GetWidth(), image.GetHeight());

                unsigned int width = (image.GetWidth() % 8) ? image.GetWidth() - (image.GetWidth() % 8) : image.GetWidth();
                unsigned int height = (image.GetHeight() % 8) ? image.GetHeight() - (image.GetHeight() % 8) : image.GetHeight();

                Gdiplus::BitmapData bData;
                Gdiplus::Status s = image.LockBits(&Gdiplus::Rect(0, 0, width, height), Gdiplus::ImageLockModeRead, 
                                                                    PixelFormat24bppRGB, &bData);
                if (s == Gdiplus::Ok) {

                        unsigned char* gray = new unsigned char[width * height];
                        unsigned char* pgray = gray;

                        //rgb2gray
                        unsigned char* Pixels = (unsigned char *)bData.Scan0;     //BGR triplet
                        for(unsigned int y = 0; y < height; y++) {
                                for(unsigned int x = 0; x < width; x++) {
                                        *pgray++ = (unsigned char)(0.2989f * float(Pixels[3*x+2]) + 
                                                    0.5870f * float(Pixels[3*x+1]) +  
                                                    0.1140f * float(Pixels[3*x]));
                                }                        
                                Pixels += bData.Stride;
                        }
                        image.UnlockBits(&bData);

                        //compress gray
                        unsigned int size = 0;
                        unsigned int quality = _wtoi(argv[3]);
                        if (quality > 100) quality = 100;

                        quality = (unsigned int)(100.0f/log(100.0f) * log(float(quality)));     //put quality to log scale

                        gCodec.initgray(width, height);                        
                        unsigned char* frame = gCodec.compressgray(gray, size, 100 - quality);                        

                        if (frame != 0) {
                                wchar_t name[_MAX_PATH];
                                wcscpy(name, argv[2]);
                                change_extension(name, L".ezw");
                                FILE* fp = _wfopen(name, L"wb");
                                if (fp != 0)
                                        wprintf(L" compressed %s to %.2f Kb. 1:%.2f compression\n", argv[2],
                                                (float)fwrite(frame, 1, size, fp) / 1000.0f, float(width * height) / float(size));
                                else
                                        wprintf(L"failed to open %s for writing.\n", name);
                                fclose(fp);
                        }
                        else
                                wprintf(L"failed to compress image.\n");

                        delete[] gray;                        
                }
                else {
                        wprintf(L"failed to lock %s for data reading. err %d", argv[2], s);
                        return;
                }
        }
        else {
                wprintf(L"failed to open %s. err: %d", argv[2], image.GetLastStatus());
                return;
        }                
}

void compress_yuv(int argc, TCHAR* argv[])
{
        Gdiplus::Bitmap image(argv[2]);
        if (image.GetLastStatus() == Gdiplus::Ok) {
                wprintf(L"opened %s %dx%d\n", argv[2], image.GetWidth(), image.GetHeight());

                unsigned int width = (image.GetWidth() % 8) ? image.GetWidth() - (image.GetWidth() % 8) : image.GetWidth();
                unsigned int height = (image.GetHeight() % 8) ? image.GetHeight() - (image.GetHeight() % 8) : image.GetHeight();

                Gdiplus::BitmapData bData;
                Gdiplus::Status s = image.LockBits(&Gdiplus::Rect(0, 0, width, height), Gdiplus::ImageLockModeRead, 
                                                                    PixelFormat24bppRGB, &bData);
                if (s == Gdiplus::Ok) {

                        unsigned char* gray = new unsigned char[width * height];
                        unsigned char* Cb = new unsigned char[width * height];
                        unsigned char* Cr = new unsigned char[width * height];

                        unsigned char* pgray = gray;
                        unsigned char* pCb = Cb;
                        unsigned char* pCr = Cr;

                        //rgb2gray
                        unsigned char* Pixels = (unsigned char *)bData.Scan0;     //BGR triplet
                        for(unsigned int y = 0; y < height; y++) {
                                for(unsigned int x = 0; x < width; x++) {
                                        rgb2ycbcr(*pgray, *pCb, *pCr, Pixels[3*x+2], Pixels[3*x+1], Pixels[3*x]);
                                        pgray++;
                                        pCb++;
                                        pCr++;
                                }                        
                                Pixels += bData.Stride;
                        }
                        image.UnlockBits(&bData);

                        //dump(L"y.txt", gray, width, height);
                        //dump(L"cb.txt", Cb, width, height);
                        //dump(L"cr.txt", Cr, width, height);

                        //compress gray
                        unsigned int ysize = 0;
                        unsigned int usize = 0;
                        unsigned int vsize = 0;
                        unsigned int quality = _wtoi(argv[3]);
                        if (quality > 100) quality = 100;

                        quality = (unsigned int)(100.0f/log(100.0f) * log(float(quality)));     //put quality to log scale

                        yCodec.initgray(width, height);                        
                        uCodec.initgray(width, height);                        
                        vCodec.initgray(width, height);                        
                        unsigned char* yframe = yCodec.compressgray(gray, ysize, 100 - quality);
                        unsigned char* uframe = uCodec.compressgray(Cb, usize, (100 - quality) + 10);
                        unsigned char* vframe = vCodec.compressgray(Cr, vsize, (100 - quality) + 10);

                        if (yframe != 0 && uframe != 0 && vframe != 0) {
                                wchar_t name[_MAX_PATH];
                                wcscpy(name, argv[2]);
                                change_extension(name, L".ezw");
                                FILE* fp = _wfopen(name, L"wb");
                                if (fp != 0) {
                                        unsigned int size = fwrite(yframe, 1, ysize, fp);
                                        size += fwrite(uframe, 1, usize, fp);
                                        size += fwrite(vframe, 1, vsize, fp);
                                        wprintf(L" compressed %s to %.2f Kb. 1:%.2f compression\n", argv[2],
                                                float(size) / 1000.0f, 
                                                float(3 * width * height) / float(ysize + usize + vsize));
                                }
                                else
                                        wprintf(L"failed to open %s for writing.\n", name);
                                fclose(fp);
                        }
                        else
                                wprintf(L"failed to compress image.\n");

                        delete[] gray;      
                        delete[] Cb;
                        delete[] Cr;
                }
                else {
                        wprintf(L"failed to lock %s for data reading. err %d", argv[2], s);
                        return;
                }
        }
        else {
                wprintf(L"failed to open %s. err: %d", argv[2], image.GetLastStatus());
                return;
        }                
}



void decompress_gray(int argc, TCHAR* argv[])
{
        FILE* fp = _wfopen(argv[2], L"rb");
        if (fp != 0) {
                fseek(fp, 0, SEEK_END);
                long size = ftell(fp);                
                rewind(fp);

                if(size < 2 * sizeof(CODECHDR)) {
                        wprintf(L"the file %s is too small %d bytes.\n", argv[2], size);
                        fclose(fp);
                        return;
                }

                unsigned char* spec = new unsigned char[size];
                fread(spec, 1, size, fp);
                fclose(fp);

                struct CODECHDR* phdr = (CODECHDR *)spec;
                unsigned char* gray = new unsigned char[phdr->width * phdr->height];
                
                gCodec.initgray(phdr->width, phdr->height);
                int res = gCodec.decompressgray(gray, spec);
                if (res < 0) {
                        wprintf(L"failed to decompress: %d\n", res);
                        delete[] gray;
                        delete[] spec;
                        return;
                }
                size = res;

                unsigned int width = phdr->width;
                unsigned int height = phdr->height;                
                delete[] spec;

                unsigned char* bgr = new unsigned char[3 * size];   //arrange RGB buffer for saving image
                for (unsigned int i = 0; i < size; i++) {
                        bgr[3*i] = gray[i];
                        bgr[3*i+1] = gray[i];
                        bgr[3*i+2] = gray[i];        
                }
                delete[] gray;

                Gdiplus::Bitmap image(width, height, 3 * width, PixelFormat24bppRGB, bgr);
                CLSID encoder;
                if (get_encoder_clsid(L"image/bmp", &encoder) >= 0) {
                        image.Save(L"ezw.bmp", &encoder);
                        ShellExecute(0, L"open", L"ezw.bmp", 0, 0, SW_SHOWNORMAL);
                }
                else
                        wprintf(L"failed to get encoder to save reconstructed image.\n");

                delete[] bgr;                
        }
        else
                wprintf(L"failed to open %s", argv[2]);
}

void decompress_yuv(int argc, TCHAR* argv[])
{
        FILE* fp = _wfopen(argv[2], L"rb");
        if (fp != 0) {
                fseek(fp, 0, SEEK_END);
                long size = ftell(fp);                
                rewind(fp);

                if(size < 3 * sizeof(CODECHDR)) {
                        wprintf(L"the file %s is too small %d bytes.\n", argv[2], size);
                        fclose(fp);
                        return;
                }

                unsigned char* spec = new unsigned char[size];
                unsigned char* pspec = spec;
                fread(spec, 1, size, fp);
                fclose(fp);

                struct CODECHDR* phdr = (CODECHDR *)pspec;
                unsigned char* gray = new unsigned char[phdr->width * phdr->height];
                unsigned char* Cb = new unsigned char[phdr->width * phdr->height];
                unsigned char* Cr = new unsigned char[phdr->width * phdr->height];
                unsigned int width = phdr->width;
                unsigned int height = phdr->height;                

                gCodec.initgray(phdr->width, phdr->height);                
                int res = gCodec.decompressgray(gray, pspec);
                if (res < 0) {
                        wprintf(L"failed to decompress y: %d\n", res);
                        delete[] gray;
                        delete[] Cb;
                        delete[] Cr;
                        delete[] spec;
                        return;
                }
                size = res;
                pspec += phdr->size + sizeof(CODECHDR);
                phdr = (CODECHDR *)pspec;
                res = gCodec.decompressgray(Cb, pspec);
                if (res < 0) {
                        wprintf(L"failed to decompress Cb: %d\n", res);
                        delete[] gray;
                        delete[] Cb;
                        delete[] Cr;
                        delete[] spec;
                        return;
                }
                size += res;
                pspec += phdr->size + sizeof(CODECHDR);
                phdr = (CODECHDR *)pspec;
                res = gCodec.decompressgray(Cr, pspec);
                if (res < 0) {
                        wprintf(L"failed to decompress y: %d\n", res);
                        delete[] gray;
                        delete[] Cb;
                        delete[] Cr;
                        delete[] spec;
                        return;
                }           
                size += res;                
                delete[] spec;

                //dump(L"y_.txt", gray, width, height);
                //dump(L"cb_.txt", Cb, width, height);
                //dump(L"cr_.txt", Cr, width, height);

                unsigned char* bgr = new unsigned char[3 * width * height];   //arrange RGB buffer for saving image
                for (unsigned int i = 0; i < width * height; i++) 
                        ycbcr2rgb(bgr[3*i+2], bgr[3*i+1], bgr[3*i], gray[i], Cb[i], Cr[i]);                                         
                delete[] gray;
                delete[] Cb;
                delete[] Cr;
                        
                Gdiplus::Bitmap image(width, height, 3 * width, PixelFormat24bppRGB, bgr);
                CLSID encoder;
                if (get_encoder_clsid(L"image/bmp", &encoder) >= 0) {
                        image.Save(L"ezw.bmp", &encoder);
                        ShellExecute(0, L"open", L"ezw.bmp", 0, 0, SW_SHOWNORMAL);
                }
                else
                        wprintf(L"failed to get encoder to save reconstructed image.\n");

                delete[] bgr;                
        }
        else
                wprintf(L"failed to open %s", argv[2]);
}





void change_extension(wchar_t* path, const wchar_t* ext)
{
        for (int i = (int)wcslen(path) - 1; i > 0; i--) {
                if (path[i] == '.') {
                        path[i] = 0;
                        wcscat(path, ext);
                        return;
                }
        }
        wcscat(path, ext);
}

int get_encoder_clsid(const WCHAR* format, CLSID* pClsid)
{
        UINT  num = 0;          // number of image encoders
        UINT  size = 0;         // size of the image encoder array in bytes

        Gdiplus::ImageCodecInfo* pImageCodecInfo = NULL;

        Gdiplus::GetImageEncodersSize(&num, &size);
        if (size == 0)
                return -1;  // Failure

        pImageCodecInfo = (Gdiplus::ImageCodecInfo*)(malloc(size));
        if (pImageCodecInfo == NULL)
                return -1;  // Failure

        Gdiplus::GetImageEncoders(num, size, pImageCodecInfo);

        for (UINT j = 0; j < num; ++j) {
                if (wcscmp(pImageCodecInfo[j].MimeType, format) == 0) {
                        *pClsid = pImageCodecInfo[j].Clsid;
                        free(pImageCodecInfo);
                        return j;  // Success
                }
        }

        free(pImageCodecInfo);
        return -1;  // Failure
}


void rgb2ycbcr(unsigned char& y, unsigned char& cb, unsigned char& cr, 
               unsigned char r, unsigned char g, unsigned char b)
{
        double dy = (T1[0][0] * double(r)) + (T1[0][1] * double(g)) + (T1[0][2] * double(b)) + offset1[0][0];
        double dcb = (T1[1][0] * double(r)) + (T1[1][1] * double(g)) + (T1[1][2] * double(b)) + offset1[1][0];
        double dcr = (T1[2][0] * double(r)) + (T1[2][1] * double(g)) + (T1[2][2] * double(b)) + offset1[2][0];

        if(dy > 255.0) dy = 255.0;
        if(dy < 0.0) dy = 0.0;
        y = (unsigned char)round(dy);

        if(dcb > 255.0) dcb = 255.0;
        if(dcb < 0.0) dcb = 0.0;
        cb = (unsigned char)round(dcb);

        if(dcr > 255.0) dcr = 255.0;
        if(dcr < 0.0) dcr = 0.0;
        cr = (unsigned char)round(dcr);
}

void ycbcr2rgb(unsigned char& r, unsigned char& g, unsigned char& b,
               unsigned char y, unsigned char cb, unsigned char cr )
{
        double dr = (T2[0][0] * double(y)) + (T2[0][1] * double(cb)) + (T2[0][2] * double(cr)) - offset2[0][0];
        double dg = (T2[1][0] * double(y)) + (T2[1][1] * double(cb)) + (T2[1][2] * double(cr)) - offset2[1][0];
        double db = (T2[2][0] * double(y)) + (T2[2][1] * double(cb)) + (T2[2][2] * double(cr)) - offset2[2][0];

        if(dr > 255.0) dr = 255.0;
        if(dr < 0.0) dr = 0.0;
        r = (unsigned char)round(dr);

        if(dg > 255.0) dg = 255.0;
        if(dg < 0.0) dg = 0.0;
        g = (unsigned char)round(dg);

        if(db > 255.0) db = 255.0;
        if(db < 0.0) db = 0.0;
        b = (unsigned char)round(db);
}

double round(double x)
{
        double y, n;
        y = modf(x, &n);
        if (x < 0.0) {
                if (-y > 0.5)
                        n -= 1.0;
        }
        else {
                if (y > 0.5)
                        n += 1.0;
        }
        return n;
}


void dump(const wchar_t* file, const unsigned char* data, unsigned int w, unsigned int h)
{
        FILE* fp = _wfopen(file, L"wt");
        for (unsigned int j = 0; j < h; j++) {
                for (unsigned int i = 0; i < w; i++) 
                        fwprintf(fp, L"%3d ", *data++);
                fwprintf(fp, L"\n");
        }
        fclose(fp);
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The GNU General Public License (GPLv3)

Share

About the Author

Chesnokov Yuriy
Engineer
Russian Federation Russian Federation
No Biography provided

| Advertise | Privacy | Mobile
Web01 | 2.8.140916.1 | Last Updated 20 Oct 2007
Article Copyright 2007 by Chesnokov Yuriy
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid