| 1 |
#include "image.h" |
| 2 |
#include "avm_fourcc.h" |
| 3 |
#include "avm_output.h" |
| 4 |
#include <stdlib.h> // labs |
| 5 |
#include <string.h> |
| 6 |
//#include <stdio.h> |
| 7 |
|
| 8 |
AVM_BEGIN_NAMESPACE; |
| 9 |
|
| 10 |
BitmapInfo::BitmapInfo() |
| 11 |
{ |
| 12 |
} |
| 13 |
|
| 14 |
BitmapInfo::BitmapInfo(int width, int height, int bpp) |
| 15 |
{ |
| 16 |
memset(this, 0, sizeof(BitmapInfo)); |
| 17 |
biWidth = width; |
| 18 |
biHeight = height; |
| 19 |
biPlanes = 1; |
| 20 |
switch (bpp) |
| 21 |
{ |
| 22 |
case 8: |
| 23 |
case 15: |
| 24 |
case 16: |
| 25 |
case 24: |
| 26 |
case 32: |
| 27 |
SetBits(bpp); |
| 28 |
break; |
| 29 |
default: |
| 30 |
SetSpace(bpp); |
| 31 |
break; |
| 32 |
} |
| 33 |
} |
| 34 |
|
| 35 |
BitmapInfo::BitmapInfo(const BITMAPINFOHEADER& hdr) |
| 36 |
{ |
| 37 |
unsigned int c = hdr.biSize; |
| 38 |
if (c > sizeof(BitmapInfo)) |
| 39 |
{ |
| 40 |
AVM_WRITE("BitmapInfo", "BitmapInfo::Unknown format (BIH) - size: %d\n", c); |
| 41 |
c = sizeof(BitmapInfo); |
| 42 |
} |
| 43 |
|
| 44 |
memcpy(this, &hdr, c); |
| 45 |
if (c <= sizeof(BITMAPINFOHEADER)) |
| 46 |
{ |
| 47 |
m_iColors[0] = m_iColors[1] = m_iColors[2] = 0; |
| 48 |
} |
| 49 |
} |
| 50 |
|
| 51 |
BitmapInfo::BitmapInfo(const BitmapInfo& bi) |
| 52 |
{ |
| 53 |
if (bi.biSize > int(sizeof(BitmapInfo))) |
| 54 |
AVM_WRITE("BitmapInfo", "BitmapInfo::Unknown format (BI) - size: %d\n", bi.biSize); |
| 55 |
|
| 56 |
memcpy(this, &bi, sizeof(BitmapInfo)); |
| 57 |
} |
| 58 |
|
| 59 |
BitmapInfo::BitmapInfo(const BitmapInfo* bi) |
| 60 |
{ |
| 61 |
if (!bi) |
| 62 |
AVM_WRITE("BitmapInfo", "BitmapInfo::Invalid argument\n"); |
| 63 |
else |
| 64 |
{ |
| 65 |
if (bi->biSize > int(sizeof(BitmapInfo))) |
| 66 |
AVM_WRITE("BitmapInfo", "BitmapInfo::Unknown format (BI*) - size: %d\n", bi->biSize); |
| 67 |
memcpy(this, bi, sizeof(BitmapInfo)); |
| 68 |
} |
| 69 |
} |
| 70 |
|
| 71 |
int BitmapInfo::BitCount(int csp) |
| 72 |
{ |
| 73 |
switch (csp) |
| 74 |
{ |
| 75 |
case IMG_FMT_RGB32: |
| 76 |
return 32; |
| 77 |
case IMG_FMT_YUV: |
| 78 |
case IMG_FMT_I444: |
| 79 |
case IMG_FMT_RGB24: |
| 80 |
return 24; |
| 81 |
case IMG_FMT_I422: |
| 82 |
case IMG_FMT_Y422: |
| 83 |
case IMG_FMT_YUY2: |
| 84 |
case IMG_FMT_UYVY: |
| 85 |
case IMG_FMT_YVYU: |
| 86 |
case IMG_FMT_RGB16: |
| 87 |
return 16; |
| 88 |
case IMG_FMT_YV12: |
| 89 |
case IMG_FMT_IYUV: |
| 90 |
case IMG_FMT_I420: |
| 91 |
case IMG_FMT_I411: |
| 92 |
return 12; |
| 93 |
case IMG_FMT_Y800: |
| 94 |
return 8; |
| 95 |
} |
| 96 |
return 0; |
| 97 |
} |
| 98 |
|
| 99 |
int BitmapInfo::Bpp() const |
| 100 |
{ |
| 101 |
if (biCompression == BI_RGB && biBitCount == 16) |
| 102 |
return 15; // 16bit BI_RGB is 5-5-5 |
| 103 |
|
| 104 |
if (biCompression == BI_BITFIELDS && m_iColors[0] == 0x7c00) |
| 105 |
return 15; |
| 106 |
|
| 107 |
return biBitCount; |
| 108 |
} |
| 109 |
|
| 110 |
|
| 111 |
void BitmapInfo::SetBitFields16() |
| 112 |
{ |
| 113 |
biBitCount = 16; |
| 114 |
SetRGB(); |
| 115 |
biSize = sizeof(BitmapInfo); |
| 116 |
biCompression = BI_BITFIELDS; |
| 117 |
m_iColors[0] = 0xF800; |
| 118 |
m_iColors[1] = 0x07E0; |
| 119 |
m_iColors[2] = 0x001F; |
| 120 |
} |
| 121 |
|
| 122 |
void BitmapInfo::SetBitFields15() |
| 123 |
{ |
| 124 |
biBitCount = 16; |
| 125 |
SetRGB(); |
| 126 |
biSize = sizeof(BitmapInfo); |
| 127 |
biCompression = BI_BITFIELDS; |
| 128 |
m_iColors[0] = 0x7C00; |
| 129 |
m_iColors[1] = 0x03E0; |
| 130 |
m_iColors[2] = 0x001F; |
| 131 |
} |
| 132 |
|
| 133 |
void BitmapInfo::SetRGB() |
| 134 |
{ |
| 135 |
biSize = sizeof(BITMAPINFOHEADER); |
| 136 |
biCompression = BI_RGB; |
| 137 |
biPlanes = 1; |
| 138 |
biSizeImage = uint32_t(biWidth * labs(biHeight) * ((biBitCount + 7) / 8)); |
| 139 |
biHeight = int32_t(-labs(biHeight)); |
| 140 |
} |
| 141 |
|
| 142 |
void BitmapInfo::SetBits(int bits) |
| 143 |
{ |
| 144 |
switch (bits) |
| 145 |
{ |
| 146 |
case 16: |
| 147 |
SetBitFields16(); |
| 148 |
break; |
| 149 |
case 15: |
| 150 |
bits = 16; // this is legal and probably prefered way by M$ |
| 151 |
// we will use BI_RGB format instead of BI_BITFIELD |
| 152 |
// this format says it's 16 bit - but it is in reality 15 bit |
| 153 |
//SetBitFields15(); |
| 154 |
//break; |
| 155 |
default: |
| 156 |
biBitCount = uint16_t(bits); |
| 157 |
SetRGB(); |
| 158 |
break; |
| 159 |
} |
| 160 |
} |
| 161 |
|
| 162 |
void BitmapInfo::SetSpace(int csp) |
| 163 |
{ |
| 164 |
biSize = sizeof(BITMAPINFOHEADER); |
| 165 |
biCompression = csp; |
| 166 |
biPlanes = 1; |
| 167 |
biBitCount = uint16_t(BitCount(csp)); |
| 168 |
biHeight = int32_t(-labs(biHeight)); |
| 169 |
biSizeImage = uint32_t(biWidth * labs(biHeight) * biBitCount / 8); |
| 170 |
} |
| 171 |
|
| 172 |
void BitmapInfo::Print() const |
| 173 |
{ |
| 174 |
AVM_WRITE("BitmapInfo", 0, "BitmapInfo, format: \n"); |
| 175 |
AVM_WRITE("BitmapInfo", 0, " biSize %d ( %d x %d x %db ) %d bytes\n", |
| 176 |
biSize, biWidth, biHeight, biBitCount, biSizeImage); |
| 177 |
AVM_WRITE("BitmapInfo", 0, " biPlanes %d, biCompression 0x%08x='%.4s'\n", |
| 178 |
biPlanes, biCompression, (const char*)&biCompression); |
| 179 |
if (biSize > 40) |
| 180 |
AVM_WRITE("BitmapInfo", 0, " colors: 0x%04x 0x%04x 0x%04x\n", |
| 181 |
m_iColors[0], m_iColors[1], m_iColors[2]); |
| 182 |
} |
| 183 |
|
| 184 |
AVM_END_NAMESPACE; |