#include <iostream>
using namespace std;
#include "bmph.h"
void showBmpFILEHead(tagBITMAPFILEHEADER *pBmpHead)
{
printf("File Head\n");
printf("File Type: %c%c\n", (pBmpHead->bfType) % 256, (pBmpHead->bfType) / 256);
printf("File Size: %d\n", pBmpHead->bfSize);
printf("Reserved1: %d\n", pBmpHead->bfReserved1);
printf("Reserved2: %d\n", pBmpHead->bfReserved2);
printf("Offset Bits: %d\n", pBmpHead->bfOffBits);
}
void showBmpInfoHead(tagBITMAPINFOHEADER *pBmpInfoHead)
{
printf("Info Head:\n");
printf("Struct Size: %d\n", pBmpInfoHead->biSize);
printf("Width: %d\n", pBmpInfoHead->biWidth);
printf("Height: %d\n", pBmpInfoHead->biHeight);
printf("Planes: %d\n", pBmpInfoHead->biPlanes);
printf("BitCout: %d\n", pBmpInfoHead->biBitCount);
printf("Compression: %d\n", pBmpInfoHead->biCompression);
printf("Image Size: %d\n", pBmpInfoHead->biSizeImage);
printf("X Resolution: %d\n", pBmpInfoHead->biXPelsPerMeter);
printf("Y Resolution: %d\n", pBmpInfoHead->biYPelsPerMeter);
printf("Color Used: %d\n", pBmpInfoHead->biClrUsed);
printf("Color Important: %d\n", pBmpInfoHead->biClrImportant);
}
void showRgbQuad(tagRGBQUAD *pRGB)
{
printf("(%-3d, %-3d, %-3d) ", pRGB->rgbRed, pRGB->rgbGreen, pRGB->rgbBlue);
}
void readBmp(string f, FILE *ifp, FILE *ofp)
{
tagBITMAPFILEHEADER bitHead;
tagBITMAPINFOHEADER bitInfoHead;
if (!ifp)
{
cerr << "error: unable to open file: "<< f << endl;
return ;
}
cout << "file " << "\"" << f + ".bmp" << "\""<< " open success." << endl << endl;
int c;
while ((c = fgetc(ifp)) != EOF)
{
fputc(c, ofp);
}
fclose(ifp);
fseek(ofp, 0, SEEK_SET);
fread(&bitHead.bfType, 1, 2, ofp);
if (bitHead.bfType != 0x4d42)
{
printf("file is not .bmp file!\n");
return ;
}
fseek(ofp, -2, SEEK_CUR);
fread(&bitHead, 1, sizeof(tagBITMAPFILEHEADER), ofp);
showBmpFILEHead(&bitHead);
cout << "\n\n";
fread(&bitInfoHead, 1, sizeof(BITMAPINFOHEADER), ofp);
showBmpInfoHead(&bitInfoHead);
cout << "\n\n";
RGBQUAD *pRgb;
if (bitInfoHead.biBitCount < 24)
{
long nPlantNum = long(pow(2, double(bitInfoHead.biBitCount)));
pRgb = new RGBQUAD[nPlantNum * sizeof(RGBQUAD)];
memset(pRgb, 0, nPlantNum * sizeof(RGBQUAD));
int num = fread(pRgb, 4, nPlantNum, ofp);
printf("Color Plant Number: %d\n", num);
printf("RGBQUAD Info: \n");
for (int i = 0; i < nPlantNum; i++)
{
if (i && (i % 5 == 0))
{
printf("\n");
}
showRgbQuad(&pRgb[i]);
}
cout << endl;
}
int width = bitInfoHead.biWidth;
int height = bitInfoHead.biHeight;
int l_width = WIDTHBYTES(width* bitInfoHead.biBitCount);
BYTE *pColorData = new BYTE[height * l_width];
memset(pColorData, 0, height * l_width);
long nData = height * l_width;
long curp = ftell(ofp);
fread(pColorData, 1, nData, ofp);
printf("%x\n", pColorData[0]);
fseek(ofp, curp - ftell(ofp), SEEK_CUR);
tagRGBQUAD *dataOfBmp;
dataOfBmp = new tagRGBQUAD[width * height * sizeof(tagRGBQUAD)];
memset(dataOfBmp, 0, width * height * sizeof(tagRGBQUAD));
if (bitInfoHead.biBitCount < 24)
{
int k, index = 0;
if (bitInfoHead.biBitCount == 1)
{
for (int i = 0; i < height; i++)
{
for (int j = 0; j < width; j++)
{
BYTE mixIndex = 0;
k = i * l_width + j / 8;
mixIndex = pColorData[k];
switch (j % 8)
{
case 0:
mixIndex = mixIndex << 7;
mixIndex = mixIndex >> 7;
break;
case 1:
mixIndex = mixIndex << 6;
mixIndex = mixIndex >> 7;
break;
case 2:
mixIndex = mixIndex << 5;
mixIndex = mixIndex >> 7;
break;
case 3:
mixIndex = mixIndex << 4;
mixIndex = mixIndex >> 7;
break;
case 4:
mixIndex = mixIndex << 3;
mixIndex = mixIndex >> 7;
break;
case 5:
mixIndex = mixIndex << 2;
mixIndex = mixIndex >> 7;
break;
case 6:
mixIndex = mixIndex << 1;
mixIndex = mixIndex >> 7;
break;
case 7:
mixIndex = mixIndex >> 7;
break;
default:
break;
}
dataOfBmp[index].rgbRed = pRgb[mixIndex].rgbRed;
dataOfBmp[index].rgbGreen = pRgb[mixIndex].rgbGreen;
dataOfBmp[index].rgbBlue = pRgb[mixIndex].rgbBlue;
dataOfBmp[index].rgbReserved = pRgb[mixIndex].rgbReserved;
index++;
}
}
}
if (bitInfoHead.biBitCount == 2)
{
for (int i = 0; i < height; i++)
{
for (int j = 0; j < width; j++)
{
BYTE mixIndex = 0;
k = i * l_width + j / 4;
mixIndex = pColorData[k];
switch(j % 4)
{
case 0:
mixIndex = mixIndex << 6;
mixIndex = mixIndex >> 6;
case 1:
mixIndex = mixIndex << 4;
mixIndex = mixIndex >> 6;
case 2:
mixIndex = mixIndex << 2;
mixIndex = mixIndex >> 6;
case 3:
mixIndex = mixIndex >> 6;
default:
break;
}
dataOfBmp[index].rgbRed = pRgb[mixIndex].rgbRed;
dataOfBmp[index].rgbGreen = pRgb[mixIndex].rgbGreen;
dataOfBmp[index].rgbBlue = pRgb[mixIndex].rgbBlue;
dataOfBmp[index].rgbReserved = pRgb[mixIndex].rgbReserved;
index++;
}
}
}
if (bitInfoHead.biBitCount == 4)
{
for (int i = 0; i < height; i++)
{
for (int j = 0; j < width; j++)
{
BYTE mixIndex = 0;
k = i * l_width + j / 2;
mixIndex = pColorData[k];
if (j % 2)
{
mixIndex = mixIndex << 4;
mixIndex = mixIndex >> 4;
}
else
{
mixIndex = mixIndex >> 4;
}
dataOfBmp[index].rgbRed = pRgb[mixIndex].rgbRed;
dataOfBmp[index].rgbGreen = pRgb[mixIndex].rgbGreen;
dataOfBmp[index].rgbBlue = pRgb[mixIndex].rgbBlue;
dataOfBmp[index].rgbReserved = pRgb[mixIndex].rgbReserved;
index++;
}
}
}
if (bitInfoHead.biBitCount == 8)
{
for (int i = 0; i < height; i++)
{
for (int j = 0; j < width; j++)
{
BYTE mixIndex = 0;
k = i * l_width + j;
mixIndex = pColorData[k];
dataOfBmp[index].rgbRed = pRgb[mixIndex].rgbRed;
dataOfBmp[index].rgbGreen = pRgb[mixIndex].rgbGreen;
dataOfBmp[index].rgbBlue = pRgb[mixIndex].rgbBlue;
dataOfBmp[index].rgbReserved = pRgb[mixIndex].rgbReserved;
index++;
}
}
}
if (bitInfoHead.biBitCount == 16)
{
for (int i = 0; i < height; i++)
{
for (int j = 0; j < width; j++)
{
WORD mixIndex = 0;
k = i * l_width + j * 2;
WORD shortTemp;
shortTemp = pColorData[k+1];
shortTemp = shortTemp << 8;
mixIndex = pColorData[k] + shortTemp;
dataOfBmp[index].rgbRed = pRgb[mixIndex].rgbRed;
dataOfBmp[index].rgbGreen = pRgb[mixIndex].rgbGreen;
dataOfBmp[index].rgbBlue = pRgb[mixIndex].rgbBlue;
dataOfBmp[index].rgbReserved = pRgb[mixIndex].rgbReserved;
index++;
}
}
}
}
else // 24x
{
int k, index = 0;
for (int i = 0; i < height; i++)
{
for (int j = 0; j < width; j++)
{
k = i * l_width + j * 3;
dataOfBmp[index].rgbRed = pColorData[k+2];
dataOfBmp[index].rgbGreen = pColorData[k+1];
dataOfBmp[index].rgbBlue = pColorData[k];
pColorData[k] = 255 - pColorData[k];
pColorData[k+1] = 255 - pColorData[k+1];
pColorData[k+2] = 255 - pColorData[k+2];
index++;
}
}
fwrite(pColorData, 1, nData, ofp);
}
/*printf("\n\nPixel Data Info:\n");
for (int i = 0; i < width * height; i++)
{
if (i && (i % 5 == 0))
{
cout << endl;
}
showRgbQuad(&dataOfBmp[i]);
}*/
fclose(ofp);
if (bitInfoHead.biBitCount < 24)
{
free(pRgb);
}
free(dataOfBmp);
free(pColorData);
cout << endl;
}
- 1
- 2
- 3
- 4
前往页