J'ai un problème! Je voudrais obtenir les informations RVB de chaque pixel en bitmap 24 bits. Jusqu'à présent, j'ai écrit un code qui obtient une information sur bitmap, mais j'ai un problème avec les informations de gettin RVB sur chaque pixel. Je voudrais garder cette information dans les tabel pixs des structures Pixel. Pourriez-vous m'aider avec ça?Comment lire pixel après pixel en bmp
Je mets mon code ci-dessous:
#include <iostream>
#include <fstream>
#include <conio.h>
using namespace std;
#pragma pack(2)
struct BITMAPFILEHEADER // File header
{
char bfType[2]; // File type: should be BM (0x42 0x4D)
int bfSize; // File size in bytes
short bfReserved1; // Reserved - for what i have no idea :P
short bfReserved2; // -||-
int bfOffBits; // Offset, adress of the beginning of the information about image (pixels)
};
struct BITMAPINFOHEADER // Bitmap header
{
unsigned int biSize; // Size of this header
unsigned int biWidth; // Width of image (in pixels)
unsigned int biHeight; // Height of this image (in pixels)
unsigned short biPlanes; // Numer of color planes, always 1
unsigned short biBitCount; // Number of bytes for pixel. Possibility values :1,4,8,16, 24 and 32
unsigned int biCompression; // Used compression (0 -none)
unsigned int biSizeImage; // Size of image
signed int biXPelsPerMeter; // Horizontal resolution of the image (pixel per meter)
signed int biYPelsPerMeter; // Vertical resolution of the image (pixel per meter)
unsigned int biClrUsed; // Number of colors in the color palette, or 0 to default to 2^n (0- no palette)
unsigned int biClrImportant; // Number of important colors used
};
#pragma pack(push, 1)
struct Pixel{
unsigned int blue; // or double?
unsigned int green;
unsigned int red;
//unsigned char reserved;
};
#pragma pack(pop)
int main(){
// Openning the file
cout << "Openning the file for reading: "<< endl;
_getch();
ifstream ifs("moj.bmp", ios::binary);
if(!ifs){
cout << " There is no such of file ";
_getch();
return 0;
}
// Reading information about BITMAPFILEHEADER
char* temp = new char[sizeof(BITMAPFILEHEADER)];
ifs.read(temp, sizeof(BITMAPFILEHEADER));
BITMAPFILEHEADER* bfh = (BITMAPFILEHEADER*)(temp);
cout << "\n FILHEADER\n";
cout << "\n File type: " << bfh->bfType[0] << bfh->bfType[1] << endl;
cout << " File size: " << bfh->bfSize << endl;
cout << " Offset(adress of beggining of the image information): " << bfh->bfOffBits << endl;
_getch();
// Reading information about BITMAPINFOHEADER
temp = new char[sizeof(BITMAPINFOHEADER)];
ifs.read(temp, sizeof(BITMAPINFOHEADER));
BITMAPINFOHEADER* bih = (BITMAPINFOHEADER*)(temp);
cout << "\n INFOHEADER\n";
cout << "\n Header size: " << bih->biSize << endl;
cout << " Image width: " << bih->biWidth << endl;
cout << " Image height: " << bih->biHeight << endl;
cout << " Number of bytes for pixel: " << bih->biBitCount << endl;
cout << " Used compression: " << bih->biCompression << endl;
cout << " Image size: " << bih->biSizeImage<< endl;
cout << " Horizontal resolution: " << bih->biXPelsPerMeter << endl;
cout << " Vertical resolution: " << bih->biYPelsPerMeter << endl;
cout << " Number of colors in the color palette: " << bih->biClrUsed << endl;
cout << " Number of important colors used: " << bih->biClrImportant << endl;
_getch();
Pixel** pixs = new Pixel*[bih->biHeight];
for (int i = 0; i < bih->biHeight ; ++i)
pixs[i] = new Pixel[bih->biWidth];
ifs.seekg(bfh->bfOffBits, ios::beg); // bfOffBits points for beginning of the image information
/* I have no idea how to read pixel after pixel in this moment */
_getch();
for (int i = 0; i < bih->biHeight; ++i)
delete pixs[i];
delete pixs;
delete bfh;
delete bih;
return 0;
}
Cela fonctionne, mais comment dois-je changer cela sur une variable entière? Par exemple:
pixs[i][j]=(unsigned int)r;
ne fonctionne pas. Après cette commande en pixs [i] [j] il y a de la corbeille ...:/
Peut-être un conseil?
Si vous n'avez pas besoin de plus de 8 bits par pixel, 'unsigned char' (ou' uint8_t') devrait suffire. –
Oui, je le sais maintenant, il devrait y avoir un caractère non signé. Mais j'ai toujours un gros problème en un instant je veux dire au moment où je lis le pixel, car la fonction de lecture comme premier argument nécessite * char et je n'ai aucune idée de comment faire cela pour que cela fonctionne ...:/ – user1120709