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

Embedded Zerotree Image Codec with Bior97 SSE Optimized Wavelet-transform

, 20 Oct 2007 GPL3
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

#include "stdafx.h"
#include "codecy.h"

#include "basefwt.h"
#include "mbior97.h"
#include "ezw.h"


/////////////////////////////////////////constructor/destructor//////////////////////////////////////////////////
CodecY::CodecY() : m_status(0), m_width(0), m_height(0)
{
        bior97 = new mBior97();
        ezw = new EZW();
}
CodecY::~CodecY()
{
        delete bior97;
        delete ezw;
}
/////////////////////////////////////////constructor/destructor//////////////////////////////////////////////////



/////////////////////////////////////////init////////////////////////////////////////////////////////////////////
void CodecY::initgray(unsigned int width, unsigned int height)
{
        m_width = width;
        m_height = height;
        bior97->init(m_width, m_height);
        m_status = 1;
}
/////////////////////////////////////////init////////////////////////////////////////////////////////////////////


/////////////////////////////////////////compress/decompress/////////////////////////////////////////////////////
unsigned char* CodecY::compressgray(const unsigned char* data, unsigned int& size, unsigned int TH)
{
        const char hdr[] = "YBior97EZW";
        if (m_status == 0) return 0;

        bior97->trans(data, 3, TH);
        size = ezw->compress(bior97->gettspec() + sizeof(CODECHDR), bior97->getspec(), m_width, m_height);      //0 - err
        //ezw size - OK
        if (size == 0)
                return 0;

        struct CODECHDR* phdr = (struct CODECHDR*) bior97->gettspec();
        memset(phdr, 0, sizeof(CODECHDR));
        memcpy(phdr->hdr, hdr, 10);
        phdr->size = size;
        phdr->width = m_width;
        phdr->height = m_height;
        phdr->crc = crc((unsigned char *)phdr, sizeof(CODECHDR) + size);

        size += sizeof(CODECHDR);
        return (unsigned char*)phdr;
}

int CodecY::decompressgray(unsigned char* dest, unsigned char* sour)
{
        const char hdr[] = "YBior97EZW";
        if (m_status == 0) return 0;

        struct CODECHDR* phdr = (struct CODECHDR*)sour;

        if (memcmp(phdr->hdr, hdr, 10) != 0)
                return -1;
        if (phdr->width != m_width || phdr->height != m_height)
                return -2;

        unsigned short c = phdr->crc;
        phdr->crc = 0;
        if (c != crc(sour, sizeof(CODECHDR) + phdr->size))
                return -3;

        unsigned int size = ezw->decompress(bior97->getspec(), (char *)(sour + sizeof(CODECHDR)), phdr->width, phdr->height);       //size = phdr->size
        if (size != phdr->size)
                return -4;
 
        bior97->setJ(3);
        bior97->synth(dest);

        return m_width * m_height;
}
/////////////////////////////////////////compress/decompress/////////////////////////////////////////////////////










unsigned short CodecY::crc(const unsigned char* addr, unsigned int len) const
{
        unsigned int nleft = len;
        unsigned int sum = 0;

        while (nleft > 1) {
                sum += *addr++;
                nleft -= 2;
        }
        /* mop up an odd byte, if necessary */
        if (nleft == 1) {
                unsigned short u = 0;
                *(unsigned char *)(&u) = *(unsigned char *)addr;
                sum += u;
        }

        sum = (sum >> 16) + (sum & 0xffff);
        return (unsigned short)~sum;
}

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 | Terms of Use | Mobile
Web01 | 2.8.1411023.1 | Last Updated 20 Oct 2007
Article Copyright 2007 by Chesnokov Yuriy
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid