Guest User

Untitled

a guest
Mar 15th, 2010
298
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.56 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. #include <windows.h>
  5.  
  6. struct _pal {
  7.     unsigned char r;
  8.     unsigned char g;
  9.     unsigned char b;
  10. };
  11. struct _pal pal[256];
  12.  
  13. BYTE* ConvertToBMP(BYTE* Buffer, int width, int height, long* newsize)
  14. {
  15.         if ((NULL == Buffer) || (width == 0) || (height == 0))
  16.                 return NULL;
  17.  
  18.         int padding = 0;
  19.         int scanlinebytes = width * 3;
  20.         while ((scanlinebytes + padding) % 4 != 0)
  21.                 padding++;
  22.         int psw = scanlinebytes + padding;
  23.  
  24.         *newsize = height * psw;
  25.  
  26.         BYTE* newbuf = (unsigned char *) malloc(*newsize);
  27.  
  28.         memset (newbuf, 0, *newsize);
  29.  
  30.         long bufpos = height * width * 4;
  31.         long newpos = 0;
  32.         int y = 0, x = 0;
  33.         for(y = 0; y < height; y++) {
  34.                 for (x = 0; x < 3 * width; x+=3) {
  35.                         bufpos-=4;
  36.                         newbuf[newpos++] = Buffer[bufpos + 2];
  37.                         newbuf[newpos++] = Buffer[bufpos + 1];
  38.                         newbuf[newpos++] = Buffer[bufpos];
  39.                 }
  40.         }
  41.  
  42.         return newbuf;
  43. }
  44.  
  45. int SaveBMP(BYTE* Buffer, int width, int height, long paddedsize, char* bmpfile)
  46. {
  47.         BITMAPFILEHEADER bmfh;
  48.         BITMAPINFOHEADER info;
  49.  
  50.         memset (&bmfh, 0, sizeof(BITMAPFILEHEADER));
  51.         memset (&info, 0, sizeof(BITMAPINFOHEADER));
  52.  
  53.         bmfh.bfType = 0x4d42;
  54.         bmfh.bfReserved1 = 0;
  55.         bmfh.bfReserved2 = 0;
  56.         bmfh.bfSize = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER) + paddedsize;
  57.         bmfh.bfOffBits = 0x36;
  58.  
  59.         info.biSize = sizeof(BITMAPINFOHEADER);
  60.         info.biWidth = width;
  61.         info.biHeight = height;
  62.         info.biPlanes = 1;
  63.         info.biBitCount = 24;
  64.         info.biCompression = BI_RGB;
  65.         info.biSizeImage = 0;
  66.         info.biXPelsPerMeter = 0x0ec4;
  67.         info.biYPelsPerMeter = 0x0ec4;
  68.         info.biClrUsed = 0;
  69.         info.biClrImportant = 0;
  70.  
  71.         HANDLE file = CreateFile(bmpfile, GENERIC_WRITE, FILE_SHARE_READ, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
  72.         if (file == NULL) {
  73.                 CloseHandle(file);
  74.                 return 0;
  75.         }
  76.  
  77.         unsigned long bwritten;
  78.         if (WriteFile(file, &bmfh, sizeof(BITMAPFILEHEADER), &bwritten, NULL) == 0) {
  79.                 CloseHandle(file);
  80.                 return 0;
  81.         }
  82.  
  83.         if (WriteFile(file, &info, sizeof(BITMAPINFOHEADER), &bwritten, NULL) == 0) {
  84.                 CloseHandle(file);
  85.                 return 0;
  86.         }
  87.  
  88.         if (WriteFile(file, Buffer, paddedsize, &bwritten, NULL) == 0) {
  89.                 CloseHandle(file);
  90.                 return 0;
  91.         }
  92.  
  93.         CloseHandle(file);
  94.  
  95.         return 1;
  96. }
  97.  
  98. int main(int argc, char * argv[])
  99. {
  100.     int w, h;
  101.     sscanf(argv[2], "%i", &w);
  102.     sscanf(argv[3], "%i", &h);
  103.  
  104.     printf("File %s, size %ix%i\n", argv[1], w, h);
  105.  
  106.     FILE * fp = fopen(argv[1], "rb");
  107.     fseek(fp, 0, SEEK_END);
  108.     int size = ftell(fp);
  109.     rewind(fp);
  110.     unsigned char * buffer = (unsigned char*) malloc (sizeof(char) * size);
  111.     fread(buffer, 1, size, fp);
  112.     fclose(fp);
  113.  
  114.     int palcount = 0, offset = 0;
  115.     while(palcount < 256) {
  116.         unsigned short extr = (buffer[offset+1] << 8) | buffer[offset];
  117.         pal[palcount].r = (extr & 0xF000) >> 8;
  118.         pal[palcount].g = ((extr & 0x0F00)) >> 4;
  119.         pal[palcount].b = ((extr & 0x00F0));
  120.         printf("%i: %04x -> %i %i %i\n", palcount, extr, pal[palcount].r, pal[palcount].g, pal[palcount].b);
  121.         palcount++;
  122.         offset+=2;
  123.     }
  124.  
  125.     unsigned char * imgbuffer = (unsigned char*) malloc (sizeof(char) * w * h * 4);
  126.  
  127.     int imgoffset = 0;
  128.     while(offset < size) {
  129.         unsigned char raw = buffer[offset];
  130.         imgbuffer[imgoffset] = pal[raw].r;
  131.         imgbuffer[imgoffset+1] = pal[raw].g;
  132.         imgbuffer[imgoffset+2] = pal[raw].b;
  133.         imgbuffer[imgoffset+3] = 0xFF;
  134.         offset++;
  135.         imgoffset+=4;
  136.     }
  137.  
  138.     long bmpsize = 0;
  139.     unsigned char * bmpbuffer = ConvertToBMP(imgbuffer, w, h, &bmpsize);
  140.     SaveBMP(bmpbuffer, w, h, bmpsize, "image.bmp");
  141.  
  142.     free(buffer);
  143.     free(imgbuffer);
  144.     free(bmpbuffer);
  145.  
  146.     return 0;
  147. }
Advertisement
Add Comment
Please, Sign In to add comment