📄 picture.cpp
字号:
#include "type.h"
Picture::Picture():Block_Address(0), Pixel(0), Width(0), Height(0)
{
_set_HV(0,0x11);
_set_HV(1,0x11);
_set_HV(2,0x11);
_set_HV(3,0x11);
Component_H_Max=1;
Component_V_Max=1;
for(int i=0; i<MCU_Max; i++) MCU[i].Type=ForbiddenBlock;
_find_HV_max();
_clear_block();
}
/*----------------------------------------------------------------------------*/
Picture::~Picture(){ _delete_array(); }
/*----------------------------------------------------------------------------*/
void Picture::_find_HV_max()
{
for(int i=0 ;i<4; i++)
{ if( C[i].Horizontal > Component_H_Max ){ Component_H_Max = C[i].Horizontal; }
if( C[i].Vertical > Component_V_Max ){ Component_V_Max = C[i].Vertical ; }
}
}
void Picture::_copy_to_Pixel(Word x, Word y, int id)
{int ci=1;
switch (MCU[id].Type)
{ case Y_Block: ci=1;break;
case CbBlock: ci=2;break;
case CrBlock: ci=3;break;
case ForbiddenBlock: return;
}
for(Word yy=0; yy<Block_Height; yy++)
for(Word xx=0; xx<Block_Width ; xx++)
{
int val = MCU[id].Value[yy][xx]+128; if( val>255 ) val=255; else if( val<0 ) val=0;
int sub_w = Component_H_Max/C[ci].Horizontal; if( Component_H_Max%C[ci].Horizontal!=0 ) sub_w++;
int sub_h = Component_V_Max/C[ci].Vertical ; if( Component_V_Max%C[ci].Vertical !=0 ) sub_h++;
for(int j=0; j<sub_w; j++)
for(int i=0; i<sub_h; i++)
{ int py=y+yy*sub_h+j;
int px=x+xx*sub_w+i;
if( px<Width && py<Height )
{
switch (MCU[id].Type)
{ case Y_Block: Pixel[py][px]=Pixel[py][px]&0x00FFFF; val=val<<16; break;
case CbBlock: Pixel[py][px]=Pixel[py][px]&0xFF00FF; val=val<<8 ; break;
case CrBlock: Pixel[py][px]=Pixel[py][px]&0xFFFF00; break;
}
Pixel[py][px] = Pixel[py][px] | val;
}
}
}
}
void Picture::_copy_to_MCU(Word x, Word y, int id)
{int ci=1;
switch (MCU[id].Type)
{ case Y_Block: ci=1;break;
case CbBlock: ci=2;break;
case CrBlock: ci=3;break;
case ForbiddenBlock: return;
}
for(Word yy=0; yy<Block_Height; yy++)
for(Word xx=0; xx<Block_Width ; xx++)
{ int val=0;
int sub_w = Component_H_Max/C[ci].Horizontal; if( Component_H_Max%C[ci].Horizontal!=0 ) sub_w++;
int sub_h = Component_V_Max/C[ci].Vertical ; if( Component_V_Max%C[ci].Vertical !=0 ) sub_h++;
for(int j=0; j<sub_w; j++)
for(int i=0; i<sub_h; i++)
{ int py=y+yy*sub_h+j;
int px=x+xx*sub_w+i;
if( px<Width && py<Height )
{
switch (MCU[id].Type)
{ case Y_Block: val = val + (int)( (Pixel[py][px]&0xFF0000)>>16)-128; break;
case CbBlock: val = val + (int)( (Pixel[py][px]&0x00FF00)>>8 )-128; break;
case CrBlock: val = val + (int)( (Pixel[py][px]&0x0000FF) )-128; break;
default: val=0; break;
}
}
}
val = val / (sub_w*sub_h);
MCU[id].Value[yy][xx] = val;
}
}
/*----------------------------------------------------------------------------*/
void Picture::_set_HV(int index, Byte HV)
{ if( index>=4 ) return;
C[index].Horizontal =(HV&0xF0)>>4;
C[index].Vertical =(HV&0x0F);
}
void Picture::_clear_picture()
{
if( IsEmpty()==true ) return;
for(int x=0; x<Width ; x++)
for(int y=0; y<Height; y++)
{ Pixel[y][x] = 0x800000; }
}
void Picture::_clear_block()
{ for(int i=0; i<MCU_Max; i++)
{
for(int x=0; x<Block_Width ; x++)
for(int y=0; y<Block_Height; y++)
{ MCU[i].Value[y][x] = 0; }
}
}
void Picture::_create_array(Word w, Word h)
{
if( IsEmpty()==false ) _delete_array();
Pixel = new DWord*[h];
for(int i=0; i<Height; i++) Pixel[i] = new DWord[w];
_clear_picture();
}
void Picture::_delete_array()
{
for(int i=0; i<Height; i++) delete Pixel[i];
delete Pixel;
Pixel=0;
}
/*----------------------------------------------------------------------------*/
bool Picture::_put_min_code_unit(int address)
{
DWord mw = Block_Width * Component_H_Max;
DWord mh = Block_Height * Component_V_Max;
DWord bw = Width /mw; if( (Width %mw)!=0 ){ bw+=1; }
DWord bh = Height/mh; if( (Height%mh)!=0 ){ bh+=1; }
if( address<0 )
{ address=Block_Address;
Block_Address+=1;
}
int x, y, xx, yy, i=0, j=0, k=0;
x= (address%bw) * mw;
y= (address/bw) * mh;
for(k=1; k<=3; k++)
{
j = C[k].Horizontal * C[k].Vertical;
for(int index=i; index<(i+j); index++)
{
xx = x + ( (index-i)%(C[k].Horizontal) )*Block_Width ;
yy = y + ( (index-i)/(C[k].Horizontal) )*Block_Height;
_copy_to_Pixel(xx,yy,index);
}
i+=j;
if( i>=MCU_Max || MCU[i].Type==ForbiddenBlock ) break;
}
if(Block_Address>=(bw*bh)){ Block_Address=0; return false; }
return true;
}
/*----------------------------------------------------------------------------*/
bool Picture::_get_min_code_unit(int address)
{
DWord mw = Block_Width * Component_H_Max;
DWord mh = Block_Height * Component_V_Max;
DWord bw = Width /mw; if( (Width %mw)!=0 ){ bw+=1; }
DWord bh = Height/mh; if( (Height%mh)!=0 ){ bh+=1; }
if( address<0 )
{ if(Block_Address>=(bw*bh)){ Block_Address=0; return false; } //
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -