all repos — m12-patcher @ 2aaac7b27ad8359b672aaf1a66f566952a2c118d

tools/src/introconv.cpp (view raw)

  1// converts intro.bmp into a format usable by the translation patch
  2
  3#include <stdio.h>
  4
  5struct color {
  6   int r;
  7   int g;
  8   int b;
  9};
 10
 11void ClearPic(unsigned char [256][256]);
 12int LoadBmp(char[], unsigned char[256][256], int&, int&, color[256]);
 13int WriteTile(FILE*, int, int, unsigned char[256][256]);
 14int ConvertPalette(color[256]);
 15char* errors[] = { "Error opening BMP file", "Dimensions not right; BMP must be 240 wide by 136 tall",
 16                   "BMP file must be in 8-bit (256 color) format", "BMP file must not use compression" };
 17
 18
 19int main(int argc, char* argv[])
 20{
 21   unsigned char pic[256][256];
 22   int           width;
 23   int           height;
 24   int           retVal;
 25   FILE*         fout;
 26   color         palette[256];
 27
 28   ClearPic(pic);
 29   retVal = LoadBmp("intro.bmp", pic, width, height, palette);
 30   if (retVal != 0)
 31   {
 32      printf("\r\n Error converting intro.bmp, quitting.\r\n Error message: (%s)\r\n", errors[retVal]);
 33	  return -1;
 34   }
 35
 36   printf("\r\n Converting intro.bmp...");
 37
 38   fout = fopen("intro_screen_gfx.bin", "wb");
 39   for (int y = 0; y < 17; y++)
 40   {
 41      for (int x = 0; x < 30; x++)
 42	  {
 43	     WriteTile(fout, x * 8, y * 8, pic);
 44	  }
 45   }
 46   fclose(fout);
 47   printf(" DONE!\r\n");
 48
 49
 50   printf(" Converting intro.bmp palette...");
 51   ConvertPalette(palette);
 52   printf(" DONE!\r\n");
 53
 54   return 0;
 55}
 56
 57void ClearPic(unsigned char pic[256][256])
 58{
 59   for (int y = 0; y < 256; y++)
 60   {
 61      for (int x = 0; x < 256; x++)
 62      {
 63         pic[y][x] = 0;
 64      }
 65   }
 66}
 67
 68
 69int LoadBmp(char filename[256], unsigned char pic[256][256], int& width, int& height, color palette[256])
 70{
 71   // return values:
 72   //    1 - error opening file
 73   //    2 - dimensions too big (must be 240x136)
 74   //    3 - not 8-bit
 75   //    4 - compression being used
 76
 77   FILE* bmp;
 78   unsigned int  start;
 79   unsigned char ch;
 80   int           bpp;
 81   int           compr;
 82   int           x = 0;
 83   int           y = 0;
 84
 85   bmp = fopen(filename, "rb");
 86   if (bmp == NULL)
 87      return 1;
 88
 89   fseek(bmp, 18, SEEK_SET);
 90   width = fgetc(bmp);
 91   width += fgetc(bmp) << 8;
 92   width += fgetc(bmp) << 16;
 93   width += fgetc(bmp) << 24;
 94
 95   height = fgetc(bmp);
 96   height += fgetc(bmp) << 8;
 97   height += fgetc(bmp) << 16;
 98   height += fgetc(bmp) << 24;
 99
100   fseek(bmp, 28, SEEK_SET);
101   bpp = fgetc(bmp);
102   bpp += fgetc(bmp) << 8;
103
104   compr = fgetc(bmp);
105   compr += fgetc(bmp) << 8;
106   compr += fgetc(bmp) << 16;
107   compr += fgetc(bmp) << 24;
108
109   if (width != 240 || height != 136)
110   {
111      fclose(bmp);
112      return 2;
113   }
114
115   if (bpp != 8)
116   {
117      fclose(bmp);
118      return 3;
119   }
120
121   if (compr != 0)
122   {
123      fclose(bmp);
124      return 4;
125   }
126
127   fseek(bmp, 10, SEEK_SET);
128   start = fgetc(bmp);
129   start += fgetc(bmp) << 8;
130   start += fgetc(bmp) << 16;
131   start += fgetc(bmp) << 24;
132
133   fseek(bmp, start, SEEK_SET);
134
135   y = height - 1;
136
137   ch = fgetc(bmp);
138   while (!feof(bmp) && (y >= 0))
139   {
140      //pic[y][x++] = (ch & 0xF0) >> 4;
141	  pic[y][x++] = ch;
142      if (x >= width)
143      {
144         x = 0;
145         y--;
146      }
147
148      ch = fgetc(bmp);
149   }
150
151
152   fseek(bmp, 0x36, SEEK_SET);
153   for (int i = 0; i < 256; i++)
154   {
155      palette[i].r = fgetc(bmp);
156	  palette[i].g = fgetc(bmp);
157	  palette[i].b = fgetc(bmp);
158	  fgetc(bmp);
159   }
160
161   fclose(bmp);
162
163   return 0;
164}
165
166int WriteTile(FILE* fout, int x, int y, unsigned char pic[256][256])
167{
168   if (!fout)
169      return -1;
170
171   for (int iy = 0; iy < 8; iy++)
172   {
173	  for (int ix = 0; ix < 8; ix++)
174	  {
175	     fputc(pic[y + iy][x + ix], fout);
176	  }
177   }
178}
179
180int ConvertPalette(color palette[256])
181{
182   FILE* fout;
183   int   newValue;
184
185   fout = fopen("intro_screen_pal.bin", "wb");
186
187   for (int i = 0; i < 256; i++)
188   {
189      newValue = 0;
190	  newValue = (palette[i].r / 8) << 10;
191	  newValue |= (palette[i].g / 8) << 5;
192	  newValue |= (palette[i].b / 8);
193
194
195	  fputc(newValue & 0xFF, fout);
196	  fputc(newValue >> 8, fout);
197	  }
198
199   fclose(fout);
200}