Click here to Skip to main content
15,879,326 members
Articles / Desktop Programming / MFC

Embedded Zerotree Image Codec with Bior97 SSE Optimized Wavelet-transform

Rate me:
Please Sign up or sign in to vote.
4.80/5 (14 votes)
20 Oct 2007GPL36 min read 56.3K   3.3K   26  
This article demonstrates the use of Embedded zero tree still image codec with JPEG 2000 wavelet-filter.

#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)


Written By
Engineer
Russian Federation Russian Federation
Highly skilled Engineer with 14 years of experience in academia, R&D and commercial product development supporting full software life-cycle from idea to implementation and further support. During my academic career I was able to succeed in MIT Computers in Cardiology 2006 international challenge, as a R&D and SW engineer gain CodeProject MVP, find algorithmic solutions to quickly resolve tough customer problems to pass product requirements in tight deadlines. My key areas of expertise involve Object-Oriented
Analysis and Design OOAD, OOP, machine learning, natural language processing, face recognition, computer vision and image processing, wavelet analysis, digital signal processing in cardiology.

Comments and Discussions