Click here to Skip to main content
12,890,181 members (44,252 online)
Click here to Skip to main content


26 bookmarked
Posted 20 Oct 2007

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.

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

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

CodecY::CodecY() : m_status(0), m_width(0), m_height(0)
        bior97 = new mBior97();
        ezw = new EZW();
        delete bior97;
        delete ezw;

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

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;

        return m_width * m_height;

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.


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


About the Author

Chesnokov Yuriy
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.

You may also be interested in...

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170424.1 | Last Updated 20 Oct 2007
Article Copyright 2007 by Chesnokov Yuriy
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid