📄 codecy.cpp
字号:
#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;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -