Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string.h>
- #include <stdio.h>
- #include <stdlib.h>
- #pragma pack(2)
- typedef struct tagBITMAPFILEHEADER
- {
- unsigned short bfType; // must be 'BM'
- unsigned int bfSize; // size of the whole .bmp file
- unsigned short bfReserved1; // must be 0
- unsigned short bfReserved2; // must be 0
- unsigned int bfOffBits;
- } BITMAPFILEHEADER;
- #pragma pack()
- #pragma pack(2)
- typedef struct tagBITMAPINFOHEADER
- {
- unsigned int biSize; // size of the structure
- int biWidth; // image width
- int biHeight; // image height
- unsigned short biPlanes; // bitplanes
- unsigned short biBitCount; // resolution
- unsigned int biCompression; // compression
- unsigned int biSizeImage; // size of the image
- int biXPelsPerMeter; // pixels per meter X
- int biYPelsPerMeter; // pixels per meter Y
- unsigned int biClrUsed; // colors used
- unsigned int biClrImportant; // important colors
- } BITMAPINFOHEADER;
- #pragma pack()
- typedef struct tagRGBTriplet
- {
- unsigned char red;
- unsigned char green;
- unsigned char blue;
- } RGBTriplet;
- unsigned char* readBMP(char* filename)
- {
- int i;
- FILE* f = fopen(filename, "rb");
- unsigned char info[54];
- fread(info, sizeof(unsigned char), 54, f); // read the 54-byte header
- // extract image height and width from header
- int width = *(int*)&info[18];
- int height = *(int*)&info[22];
- int size = 3 * width * height;
- unsigned char* data = new unsigned char[size]; // allocate 3 bytes per pixel
- fread(data, sizeof(unsigned char), size, f); // read the rest of the data at once
- fclose(f);
- for(i = 0; i < size; i += 3)
- {
- unsigned char tmp = data[i];
- data[i] = data[i+2];
- data[i+2] = tmp;
- }
- return data;
- }
- //Now data should contain the (R, G, B) values of the pixels. The color of pixel (i, j) is stored at data[j * width + i], data[j * width + i + 1] and data[j * width + i + 2].
- //In the last part, the swap between every first and third pixel is done because windows stores the color values as (B, G, R) triples, not (R, G, B).
- void writeBMP(char* filename)
- {
- BITMAPFILEHEADER bfh;
- BITMAPINFOHEADER bih;
- /* Magic number for file. It does not fit in the header structure due to alignment requirements, so put it outside */
- unsigned short bfType=0x4d42;
- bfh.bfReserved1 = 0;
- bfh.bfReserved2 = 0;
- bfh.bfSize = 2+sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER)+640*480*3;
- bfh.bfOffBits = 0x36;
- bih.biSize = sizeof(BITMAPINFOHEADER);
- bih.biWidth = 640;
- bih.biHeight = 480;
- bih.biPlanes = 1;
- bih.biBitCount = 24;
- bih.biCompression = 0;
- bih.biSizeImage = 0;
- bih.biXPelsPerMeter = 5000;
- bih.biYPelsPerMeter = 5000;
- bih.biClrUsed = 0;
- bih.biClrImportant = 0;
- FILE *file = fopen(filename, "wb");
- if (!file)
- {
- printf("Could not write file\n");
- return;
- }
- /*Write headers*/
- fwrite(&bfType,1,sizeof(bfType),file);
- fwrite(&bfh, 1, sizeof(bfh), file);
- fwrite(&bih, 1, sizeof(bih), file);
- /*Write bitmap*/
- for (int y = bih.biHeight-1; y>=0; y--) /*Scanline loop backwards*/
- {
- for (int x = 0; x < bih.biWidth; x++) /*Column loop forwards*/
- {
- /*compute some pixel values*/
- unsigned char r = 255*((float)x/bih.biWidth);
- unsigned char g = 255*((float)y/bih.biHeight);
- unsigned char b = 0;
- fwrite(&b, 1, 1, file);
- fwrite(&g, 1, 1, file);
- fwrite(&r, 1, 1, file);
- }
- }
- fclose(file);
- }
- unsigned char* ConvertBMPToRGBBuffer ( unsigned char* Buffer, int width, int height )
- {
- if ((NULL == Buffer) || (width == 0) || (height == 0))
- return NULL;
- int padding = 0;
- int scanlinebytes = width * 3;
- while ( ( scanlinebytes + padding ) % 4 != 0 )
- padding++;
- int psw = scanlinebytes + padding;
- unsigned char* newbuf= new unsigned char[width*height*3];
- long bufpos = 0;
- long newpos = 0;
- for ( int y = 0; y < height; y++ )
- for ( int x = 0; x < 3 * width; x+=3 )
- {
- newpos = y * 3 * width + x;
- bufpos = ( height - y - 1 ) * psw + x;
- newbuf[newpos] = Buffer[bufpos + 2];
- newbuf[newpos + 1] = Buffer[bufpos+1];
- newbuf[newpos + 2] = Buffer[bufpos];
- }
- return newbuf;
- }
- unsigned char* ConvertRGBToBMPBuffer ( unsigned char* Buffer, int width, int height, long* newsize )
- {
- if ( ( NULL == Buffer ) || ( width == 0 ) || ( height == 0 ) )
- return NULL;
- int padding = 0;
- int scanlinebytes = width * 3;
- while ( ( scanlinebytes + padding ) % 4 != 0 )
- padding++;
- int psw = scanlinebytes + padding;
- *newsize = height * psw;
- unsigned char* newbuf = new unsigned char[*newsize];
- memset ( newbuf, 0, *newsize );
- long bufpos = 0;
- long newpos = 0;
- for ( int y = 0; y < height; y++ )
- for ( int x = 0; x < 3 * width; x+=3 )
- {
- bufpos = y * 3 * width + x; // position in original buffer
- newpos = ( height - y - 1 ) * psw + x; // position in padded buffer
- newbuf[newpos] = Buffer[bufpos+2]; // swap r and b
- newbuf[newpos + 1] = Buffer[bufpos + 1]; // g stays
- newbuf[newpos + 2] = Buffer[bufpos]; // swap b and r
- }
- return newbuf;
- }
- unsigned char* LoadBMP(int* width, int* height, long* size, char* filename)
- {
- BITMAPFILEHEADER bmpheader;
- BITMAPINFOHEADER bmpinfo;
- unsigned long bytesread;
- FILE *bitmap = fopen(filename, "rb");
- if (NULL == bitmap)
- return NULL;
- fread(&bmpheader, 1, sizeof(BITMAPFILEHEADER), bitmap);
- fread(&bmpinfo, 1, sizeof(BITMAPINFOHEADER), bitmap);
- if(bmpheader.bfType != 0x4d42 )
- {
- fclose(bitmap);
- }
- if(&bmpinfo.biCompression!= 0x0000)
- {
- fclose(bitmap);
- }
- if(bmpinfo.biBitCount!= 24)
- {
- fclose(bitmap);
- }
- *width = bmpinfo.biWidth;
- *height = abs(bmpinfo.biHeight);
- *size = bmpheader.bfSize - bmpheader.bfOffBits;
- unsigned char* Buffer = new unsigned char[*size];
- fseek(bitmap, bmpheader.bfOffBits ,SEEK_SET);
- fread(Buffer, *size, 4, bitmap);
- fclose(bitmap);
- return Buffer;
- }
- bool SaveBMP (unsigned char* Buffer, int width, int height, long paddedsize, char* bmpfile)
- {
- BITMAPFILEHEADER bmfh;
- BITMAPINFOHEADER info;
- memset ( &bmfh, 0, sizeof (BITMAPFILEHEADER ) );
- memset ( &info, 0, sizeof (BITMAPINFOHEADER ) );
- bmfh.bfType = 0x4d42; // 0x4d42 = 'BM'
- bmfh.bfReserved1 = 0;
- bmfh.bfReserved2 = 0;
- bmfh.bfSize = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER) + paddedsize;
- bmfh.bfOffBits = 0x36;
- info.biSize = sizeof(BITMAPINFOHEADER);
- info.biWidth = width;
- info.biHeight = height;
- info.biPlanes = 1;
- info.biBitCount = 24;
- info.biCompression = 0x0000;
- info.biSizeImage = 0;
- info.biXPelsPerMeter = 0x0ec4;
- info.biYPelsPerMeter = 0x0ec4;
- info.biClrUsed = 0;
- info.biClrImportant = 0;
- FILE *bitmap = fopen(bmpfile, "rb");
- if (NULL == bitmap)
- return NULL;
- unsigned long bwritten;
- fwrite(&bmfh, 1, sizeof(BITMAPFILEHEADER), bitmap);
- fwrite(&info, 1, sizeof(BITMAPINFOHEADER), bitmap);
- fwrite(Buffer, 1, paddedsize, bitmap);
- fclose(bitmap);
- return true;
- }
- int main()
- {
- int x, y;
- long s;
- unsigned char* b = LoadBMP ( &x, &y, &s, "testbitmap.bmp" );
- SaveBMP ( b, x, y, s, "nowabitmapa.bmp" );
- delete [] b;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement