| 1296 | } |
| 1297 | |
| 1298 | void bmp_get_alpha(char *bmpname,unsigned char code,int width,int height, |
| 1299 | unsigned char *data) |
| 1300 | { |
| 1301 | struct BMPHEAD *bmphdr; |
| 1302 | unsigned int *pal; |
| 1303 | unsigned char *pix,*bmp,*src,*tgt; |
| 1304 | int i,j,k,bmpsz,bmpw,warn; |
| 1305 | |
| 1306 | /* sanity checks */ |
| 1307 | map_file(bmpname,&bmp,&bmpsz); |
| 1308 | if ((bmp[0]!='B')||(bmp[1]!='M')) |
| 1309 | { printf("Not a BMP file !\n"); abandon_ship(); } |
| 1310 | bmphdr=(struct BMPHEAD *)(bmp+2); |
| 1311 | if ((bmphdr->planes!=1)||(bmphdr->compr)) |
| 1312 | { printf("Unsupported BMP format !\n"); abandon_ship(); } |
| 1313 | if ((bmphdr->wid!=width)||(bmphdr->hei!=height)) |
| 1314 | { printf("Incorrect geometry (%dx%d instead of %dx%d)\n", |
| 1315 | bmphdr->wid,bmphdr->hei,width,height); |
| 1316 | abandon_ship(); } |
| 1317 | if (bmphdr->bpp!=8) { printf("Alpha channel is not a 8-bit BMP !\n"); abandon_ship(); } |
| 1318 | |
| 1319 | pix=bmp+bmphdr->ofsbmp; |
| 1320 | pal=(unsigned int *)(bmp+54); |
| 1321 | warn=0; |
| 1322 | for (i=0;i<256;i++) |
| 1323 | if ((pal[i]&0xffffff)%0x010101!=0) warn=1; |
| 1324 | if (warn) { |
| 1325 | printf("WARNING: color palette is not grayscale in alpha channel bitmap.\n"); |
| 1326 | printf(" Keeping blue component.\n"); |
| 1327 | } |
| 1328 | bmpw=width; |
| 1329 | while (bmpw&3) bmpw++; |
| 1330 | |
| 1331 | if (code==0x7D) { /* 32 bit */ |
| 1332 | for (i=0;i<height;i++) { |
| 1333 | src=pix+(height-1-i)*bmpw; |
| 1334 | tgt=data+i*4*width; |
| 1335 | for (j=0;j<width;j++) |
| 1336 | tgt[4*j+3]=pal[src[j]]&0xff; |
| 1337 | } |
| 1338 | } |
| 1339 | else |
| 1340 | if (code==0x7E) { /* 16 bit 1:5:5:5 */ |
| 1341 | for (i=0;i<height;i++) { |
| 1342 | src=pix+(height-1-i)*bmpw; |
| 1343 | tgt=data+i*2*width; |
| 1344 | for (j=0;j<width;j++) |
| 1345 | tgt[2*j+1]+=pal[src[j]]&0x80; |
| 1346 | } |
| 1347 | } |
| 1348 | else |
| 1349 | if (code==0x6D) { /* 16 bit 4:4:4:4 */ |
| 1350 | for (i=0;i<height;i++) { |
| 1351 | src=pix+(height-1-i)*bmpw; |
| 1352 | tgt=data+i*2*width; |
| 1353 | for (j=0;j<width;j++) |
| 1354 | tgt[2*j+1]+=pal[src[j]]&0xf0; |
| 1355 | } |
no test coverage detected