bitmap lesen/schreiben ohne windows api
-
Hallo,
ich möchte bmp dateien 'von hand' laden / speichern ohne dabei die windows funktionen (CBitmap::LoadBitmap etc) zu benutzen. Gibt es dafür irgendwo ein gutes Tutorial oder ähnliches?
Ansonsten habe ich mal hier angehängt was ich bisher habe (funktioniert allerdings nicht...) vielleicht kann mir ja hier jemand weiterhelfen.
Danke ~Raymond
typedef struct tagBITMAPFILEHEADER { UINT bfType; // Specifies the type of file. This member must be BM. (0x4D42) DWORD bfSize; // Specifies the size of the file, in bytes. UINT bfReserved1; // Reserved; must be set to zero. UINT bfReserved2; // Reserved; must be set to zero. DWORD bfOffBits; // Specifies the byte offset from the BITMAPFILEHEADER structure to the actual bitmap data in the file. } BITMAPFILEHEADER; typedef struct tagBITMAPINFOHEADER { DWORD biSize; // Specifies the number of bytes required by the BITMAPINFOHEADER structure. LONG biWidth; // Specifies the width of the bitmap, in pixels. LONG biHeight; // Specifies the height of the bitmap, in pixels. WORD biPlanes; // Specifies the number of planes for the target device. This member must be set to 1. WORD biBitCount; // Specifies the number of bits per pixel. This value must be 1,4, 8, or 24. DWORD biCompression; // Specifies the type of compression for a compressed bitmap. Itcan be one of the following values: BI_RGB, BI_RLE8, BI_RLE4 DWORD biSizeImage; // Specifies the size, in bytes, of the image. It is valid to set this member to zero if the bitmap is in the BI_RGB format. LONG biXPelsPerMeter; // Specifies the horizontal resolution, in pixels per meter, of the target device for the bitmap. LONG biYPelsPerMeter; // Specifies the vertical resolution, in pixels per meter, of the target device for the bitmap. DWORD biClrUsed; // Specifies the number of color indexes in the color table actually used by the bitmap. DWORD biClrImportant; // pecifies the number of color indexes that are considered important for displaying the bitmap. If this value is zero, all colors are important. } BITMAPINFOHEADER; load(char* file_name){ unsigned char *bitmapImage; //store image data BITMAPFILEHEADER bmfh; BITMAPINFOHEADER bmih; int imageIdx=0; //image index counter unsigned char tempRGB; //our swap variable //open filename in read binary mode FILE *fp = fopen(file_name, "rb"); if (fp == NULL) return ERROR; //read the bitmap file header fread(&bmfh, sizeof(bmfh),1,fp); //verify that this is a bmp file by check bitmap id if (bmfh.bfType !=0x4D42){ fclose(fp); return ERROR; } //read the bitmap info header fread(&bmih, sizeof(bmih),1,fp); //move file point to the begging of bitmap data fseek(fp, bmfh.bfOffBits, SEEK_SET); //allocate enough memory for the bitmap image data bitmapImage = (unsigned char*)malloc(bmih.biSizeImage); //verify memory allocation if (!bitmapImage){ free(bitmapImage); fclose(fp); return ERROR; } //read in the bitmap image data fread(bitmapImage,bmih.biSizeImage,1,fp); //make sure bitmap image data was read if (bitmapImage == NULL){ fclose(fp); return ERROR; } //swap the r and b values to get RGB (bitmap is BGR) for (imageIdx = 0;imageIdx < bmih.biSizeImage;imageIdx+=3){ tempRGB = bitmapImage[imageIdx]; bitmapImage[imageIdx] = bitmapImage[imageIdx + 2]; bitmapImage[imageIdx + 2] = tempRGB; } //close file fclose(fp); return OK; }
-
Klar kannst du die Bitmap-Daten von Hand auslesen, der BMP-Header ist standardisiert und liefert alle notwendigen Informationen, um die Farbdaten interpretieren zu können (die beiden struct's BITMAPFILEHEADER und BITMAPINFOHEADER werten diese Informationen für dich aus). Wichtig ist allerdings, daß du die verschiedenen Farbtiefen interpretieren kannst (so wie es aussieht, beschränkst du die auf 24-Bit-BMP).