📄 mimage.cpp
字号:
#include "MImage.h"
void CMImage::freeData() {
freeCache();
if ( isConverted_ ) {
if ( isReallocated_ ) {
SYSFREE(bmp_);
bmp_ = NULL;
} else {
delete pBinary_;
pBinary_ = NULL;
}
} else {
delete pBinary_;
pBinary_ = NULL;
}
}
void CMImage::freeCache() {
#ifdef USE_MODULE_DYNAMIC_CACHE
if ( cache_ ) {
FREE(cache_);
cache_ = NULL;
}
#endif
}
CMImage *CMImage::createFrom() {
/*
// return value : 汗荤登绢 货肺 父甸绢龙 按眉
*/
return createFrom(this);
}
CMImage *CMImage::createFrom(CMImage *pImage) {
/*
// pImage : 汗荤窍霸瞪 盔夯 按眉
// return value : 汗荤登绢 货肺 父甸绢龙 按眉
*/
return new CMImage(CMBinary::createFrom(pImage->pBinary_), pImage->isBmpPalletteIn_);
}
boolean CMImage::setBmpPallette(CMBinary *pPallette, boolean palFree) {
/*
// pPallette : BMP palette data啊 淬变 binary 按眉
// palFree : pPallette binary 按眉甫 荤侩 饶 秦力 咯何
*/
if ( isBmpPalletteIn_ ) // 捞固 庆歹啊 嘿绢 乐绰 image俊绰 嘿老 荐 绝促.
return false;
CMBinary *newBuffer = new CMBinary(pPallette->getSize()+pBinary_->getSize()+BMP_HEADER_SIZE-6);
byte *header = createBmpHeader(pBinary_, pPallette); // BMP 庆歹 积己
newBuffer->writeFrom( header, 0, 0, BMP_HEADER_SIZE); // BMP 庆歹 汗荤茄促.
FREE(header);
newBuffer->writeFrom( pPallette, BMP_HEADER_SIZE, 2, pPallette->getSize()-2 );
// BrewSmith啊 嘿牢 迫饭飘 庆歹 2官捞飘 力寇窍绊 迫饭飘 汗荤.
newBuffer->writeFrom( pBinary_, BMP_HEADER_SIZE + pPallette->getSize() - 2, 4, pBinary_->getSize()-4 );
// BrewSmith啊 嘿牢 官叼 庆歹 4官捞飘 力寇窍绊 官叼 汗荤.
delete pBinary_; // 官叼 滚欺甫 秦力茄 饶,
pBinary_ = newBuffer; // 庆歹+官叼啊 等 肯己等 BMP binary甫 措涝
isBmpPalletteIn_ = true;
processHeader(); // 庆歹啊 肯己登菌栏骨肺 颇教茄促.
if ( palFree )
delete pPallette;
return true;
}
void CMImage::setPalletteColor(int index, RGBVAL color) {
/*
// index : 迫饭飘狼 牢郸胶
// color : 措涝且 祸惑 沥焊
*/
#ifdef DEBUG_CODE_ENABLED
if ( !isBmpPalletteIn_ ) {
DBGPRINTF("[MImage class]-[setPalletteColor func] : No pallette.");
return;
}
if ( index < 0 || index >= colorCount_ ) {
DBGPRINTF("[MImage class]-[setPalletteColor func] : Out of colors.");
return;
}
#endif
color = ( (color & 0xff000000) >> 24 ) + ( (color & 0x00ff0000) >> 8 ) +
( (color & 0x0000ff00) << 8 );
MEMCPY( pBinary_->getData() + PALLETTE_START_OFFSET + index*4,
&color,
sizeof(uint32) );
}
void CMImage::setPalletteColor(int index, uint8 r, uint8 g, uint8 b) {
/*
// index : 迫饭飘狼 牢郸胶
// r : 措涝且 祸惑狼 red 沥焊
// g : 措涝且 祸惑狼 green 沥焊
// b : 措涝且 祸惑狼 blue 沥焊
*/
setPalletteColor(index, MAKE_RGB(r,g,b) );
}
RGBVAL CMImage::getPalletteColor(int index) {
/*
// index : 迫饭飘狼 牢郸胶
*/
#ifdef DEBUG_CODE_ENABLED
if ( !isBmpPalletteIn_ ) {
DBGPRINTF("[MImage class]-[getPalletteColor func] : No pallette.");
return -1;
}
if ( index < 0 || index >= colorCount_ ) {
DBGPRINTF("[MImage class]-[getPalletteColor func] : Out of colors.");
return -1;
}
#endif
return CMTool::BytesToIntLittleEndian( pBinary_->getData() + PALLETTE_START_OFFSET + index*4, 4);
}
RGB_QUAD *CMImage::getPalletteRef(int index) {
/*
// index : 迫饭飘狼 牢郸胶
//
// 林狼 : 罐篮 器牢磐甫 1byte 捞惑狼 鸥涝 器牢磐肺 函券窍咯 静瘤 富巴.
*/
#ifdef DEBUG_CODE_ENABLED
if ( !isBmpPalletteIn_ ) {
DBGPRINTF("[MImage class]-[getPalletteRef func] : No pallette.");
return NULL;
}
if ( index < 0 || index >= colorCount_ ) {
DBGPRINTF("[MImage class]-[getPalletteRef func] : Out of colors.");
return NULL;
}
#endif
return (RGB_QUAD *)(pBinary_->getData() + PALLETTE_START_OFFSET + index*4);
}
void CMImage::setPixel(int x, int y, int data) {
/*
// x : x谅钎
// y : y谅钎
// data : 侨伎俊 甸绢哎 沥焊.
*/
int index;
int actual_index;
int from_bit;
index = getPixelIndex(x, y);
actual_index = index >> CACHE_shiftNo_; // actual_index = index / m_iPixelPerByte;
from_bit = (index - actual_index*CACHE_pixelPerByte_) * colorDepth_;
CACHE_maskStatic_ = CACHE_mask_ << ( (8-colorDepth_) - (from_bit) );
CACHE_maskStatic_ = ~CACHE_maskStatic_;
// mask = mask ^ 0xff;
data = data << ( (8-colorDepth_) - (from_bit) );
body_[actual_index] = ( body_[actual_index] & CACHE_maskStatic_ ) | data;
}
int CMImage::getPixel(int x, int y) {
/*
// x : x谅钎
// y : y谅钎
// return value :
*/
int index;
int actual_index;
int from_bit;
index = getPixelIndex(x, y);
actual_index = index >> CACHE_shiftNo_; // actual_index = index / m_iPixelPerByte;
from_bit = (index - actual_index*CACHE_pixelPerByte_) * colorDepth_;
return (byte)( body_[actual_index] >> (8-(from_bit+colorDepth_)) ) & CACHE_mask_;
}
void CMImage::swapPixel(int x1, int y1, int x2, int y2) {
/*
// x1 : swap 且 霉锅掳 侨伎狼 x谅钎
// y1 : swap 且 霉锅掳 侨伎狼 y谅钎
// x2 : swap 且 滴锅掳 侨伎狼 x谅钎
// y2 : swap 且 滴锅掳 侨伎狼 y谅钎
*/
int tmp;
tmp = getPixel(x1, y1);
setPixel(x1, y1, getPixel(x2, y2));
setPixel(x2, y2, tmp);
}
void CMImage::toGray() {
#ifdef DEBUG_CODE_ENABLED
if ( !isBmpPalletteIn_ ) {
DBGPRINTF("[MImage class]-[toGray func] : No pallette.");
return;
}
#endif
int i;
RGB_QUAD *col;
// uint8 grayColor;
uint16 grayColor;
for ( i = 0; i < colorCount_ ; ++i ) {
col = getPalletteRef(i);
if ( col->getIntValue() != COLOR_TRANSPARENT ) {
grayColor = (col->red + col->blue + col->green);
grayColor /= 3;
col->red = col->green = col->blue = (uint8)grayColor;
}
}
}
void CMImage::toTransparent(int rate, RGBVAL color) {
/*
// rate : 捧疙档
// color : 弊副 祸惑
*/
RGB_QUAD *col;
color = CMTool::ntohl( (uint32)color );
col = (RGB_QUAD *)(&color);
toTransparent(rate, col->red, col->green, col->blue );
}
void CMImage::toTransparent(int rate, int r, int g, int b) {
#ifdef DEBUG_CODE_ENABLED
if ( !isBmpPalletteIn_ ) {
DBGPRINTF("[MImage class]-[toTransparent func] : No pallette.");
return;
}
#endif
int i;
RGB_QUAD *col;
for ( i = 0; i < colorCount_ ; ++i ) {
col = getPalletteRef(i);
if ( col->getIntValue() != COLOR_TRANSPARENT ) {
col->red = ( ( col->red * rate ) + ( ( 100-rate ) * r ) ) / 100;
col->green = ( ( col->green * rate ) + ( ( 100-rate ) * g ) ) / 100;
col->blue = ( ( col->blue * rate ) + ( ( 100-rate ) * b ) ) / 100;
}
}
}
void CMImage::zoomIn(int level) {
/*
// level : 淋狼 硅啦
// ta_.x 客 ta_.y 困摹啊 淋窍妨绰 盔夯狼 哭率 困 谅钎啊 登绊 傈眉 农扁客 涝仿等 硅啦俊 蝶扼 盔夯狼 承捞啊 搬沥等促.
*/
int i,j, toI, toJ;
int i2,j2, index;
byte *cols;
// zoom 窍妨绰 康开狼 单捞磐甸阑 cols俊 急屈栏肺 历厘茄促.
toI = ta_.x + (width_/level) + (width_%level>0);
toJ = ta_.y + (height_/level) + (height_%level>0);
#ifdef DEBUG_CODE_ENABLED
if ( toI >= width_ || toJ >= height_ ) {
DBGPRINTF("[MImage class]-[zoomIn func] : out of source's bound.");
return;
}
#endif
cols = (byte *)MALLOC( (toI-ta_.x) * (toJ-ta_.y) );
index = 0;
for ( i = ta_.x; i < toI; ++i ) {
for ( j = ta_.y; j < toJ; ++j ) {
cols[index] = getPixel(i,j);
index++;
}
}
index = 0;
for ( i = 0; i < width_; i += level) {
for ( j = 0; j < height_; j += level ) {
for ( i2 = 0; i2 < level; ++i2 ) {
for ( j2 = 0; j2 < level; ++j2 ) {
if ( i+i2 < width_ && j+j2 < height_ ) {
setPixel(i+i2, j+j2, cols[index] );
}
}
}
index++;
}
}
FREE(cols);
}
void CMImage::zoomOut(int level) {
}
void CMImage::toMosaic(int level) {
int i,j,toI,toJ;
int i2,j2;
toI = ta_.x + ta_.dx + (ta_.dx%level>0);
toJ = ta_.y + ta_.dy + (ta_.dy%level>0);
for ( i = ta_.x; i < toI; i += level) {
for ( j = ta_.y; j < toJ; j += level ) {
for ( i2 = 0; i2 < level; ++i2 ) {
for ( j2 = 0; j2 < level; ++j2 ) {
if ( i+i2 < ta_.x+ta_.dx && j+j2 < ta_.y+ta_.dy ) {
setPixel(i+i2, j+j2, getPixel(i,j) );
}
}
}
}
}
}
void CMImage::toMask() {
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -