]> de.git.xonotic.org Git - xonotic/netradiant.git/blob - plugins/image/bmp.cpp
more eol-style
[xonotic/netradiant.git] / plugins / image / bmp.cpp
1 /*
2 Copyright (C) 1999-2007 id Software, Inc. and contributors.
3 For a list of contributors, see the accompanying CONTRIBUTORS file.
4
5 This file is part of GtkRadiant.
6
7 GtkRadiant is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
11
12 GtkRadiant is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GtkRadiant; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
20 */
21
22 #include <stdio.h>
23 #include <string.h>
24 #include <glib.h>
25 #include "bmp.h"
26
27 #include "image.h"
28
29 static void BMPLineNone(FILE *f, char *sline, int pixbytes, int width)
30 {
31     int nbytes, i, k, j;
32
33         switch (pixbytes)
34         {
35             case 1 :
36             nbytes = (width + 3) / 4;
37             nbytes *= 4;
38
39             fread(sline, width, 1, f);
40             nbytes -= width;
41
42             while (nbytes-- > 0) fgetc(f);
43                         return;
44
45                 case 3 :
46                         nbytes = ((width * 3) + 3) / 4;
47                         nbytes *= 4;
48
49                         fread(sline, width, 3, f);
50                         nbytes -= width * 3;
51
52                         while (nbytes-- > 0) fgetc(f);
53
54                         // reorder bgr to rgb
55                         for (i = 0, j = 0; i < width; i++, j += 3)
56                         {
57                                 k = sline[j];
58                                 sline[j] = sline[j+2];
59                                 sline[j+2] = k;
60                         }
61
62                         return;
63         }
64
65         Error("BMPLineNone failed.");
66 }
67
68
69 static void BMPLineRLE8(FILE *f, char *sline, int pixbytes, int width)
70 {
71     Error("RLE8 not yet supported.");
72 }
73
74
75 static void BMPLineRLE4(FILE *f, char *sline, int pixbytes, int width)
76 {
77     Error("RLE4 not yet supported.");
78 }
79
80
81 static void BMPLine(FILE *f, char *scanline, int pixbytes, int width, int rle)
82 {
83     switch (rle)
84     {
85             case xBI_NONE : BMPLineNone(f, scanline, pixbytes, width); return;
86             case xBI_RLE8 : BMPLineRLE8(f, scanline, pixbytes, width); return;
87             case xBI_RLE4 : BMPLineRLE4(f, scanline, pixbytes, width); return;
88     }
89
90     Error("Unknown compression type.");
91 }
92
93
94 /*
95 static void PrintHeader(binfo_t *b)
96 {
97     printf("biSize         : %ld\n", b->biSize);
98     printf("biWidth        : %ld\n", b->biWidth);
99     printf("biHeight       : %ld\n", b->biHeight);
100     printf("biPlanes       : %d\n", b->biPlanes);
101     printf("biBitCount     : %d\n", b->biBitCount);
102     printf("biCompression  : %ld\n", b->biCompression);
103     printf("biSizeImage    : %ld\n", b->biSizeImage);
104     printf("biXPelsPerMeter: %ld\n", b->biXPelsPerMeter);
105     printf("biYPelsPerMeter: %ld\n", b->biYPelsPerMeter);
106     printf("biClrUsed      : %ld\n", b->biClrUsed);
107     printf("biClrImportant : %ld\n", b->biClrImportant);
108 }
109 */
110
111 // FIXME: calls to Error(const char *, ... ) are dependant on qe3.cpp
112 void LoadBMP(char *filename, bitmap_t *bit)
113 {
114     FILE    *f;
115     bmphd_t  bhd;
116     binfo_t  info;
117     //    int      pxlsize = 1;
118     int      rowbytes, i, pixbytes;
119     char    *scanline;
120
121     // open file
122     if ((f = fopen(filename, "rb")) == NULL)
123     {
124             Error("Unable to open file");// %s.", filename);
125     }
126
127     // read in bitmap header
128     if (fread(&bhd, sizeof(bhd), 1, f) != 1)
129     {
130                 fclose(f);
131             Error("Unable to read in bitmap header.");
132     }
133
134     // make sure we have a valid bitmap file
135     if (bhd.bfType != BMP_SIGNATURE_WORD)
136     {
137                 fclose(f);
138             Error("Invalid BMP file");//: %s", filename);
139     }
140
141     // load in info header
142     if (fread(&info, sizeof(info), 1, f) != 1)
143     {
144                 fclose(f);
145             Error("Unable to read bitmap info header.");
146     }
147
148     // make sure this is an info type of bitmap
149     if (info.biSize != sizeof(binfo_t))
150     {
151                 fclose(f);
152             Error("We only support the info bitmap type.");
153     }
154
155     // PrintHeader(&info);
156
157         bit->bpp      = info.biBitCount;
158         bit->width    = info.biWidth;
159     bit->height   = info.biHeight;
160     bit->data     = NULL;
161     bit->palette  = NULL;
162
163     //currently we only read in 8 and 24 bit bmp files
164         if      (info.biBitCount == 8)  pixbytes = 1;
165         else if (info.biBitCount == 24) pixbytes = 3;
166         else
167     {
168       Error("Only 8BPP and 24BPP supported");
169                 //Error("BPP %d not supported.", info.biBitCount);
170     }
171
172     // if this is an eight bit image load palette
173         if (pixbytes == 1)
174     {
175             drgb_t q;
176
177             bit->palette = reinterpret_cast<rgb_t*>(g_malloc(sizeof(rgb_t) * 256));
178
179             for (i = 0; i < 256; i++)
180             {
181                 if (fread(&q, sizeof(drgb_t), 1, f) != 1)
182                 {
183                                 fclose(f); g_free(bit->palette);
184                         Error("Unable to read palette.");
185                         }
186
187                 bit->palette[i].r   = q.red;
188                 bit->palette[i].g   = q.green;
189                 bit->palette[i].b   = q.blue;
190                 }
191     }
192
193     // position to start of bitmap
194     fseek(f, bhd.bfOffBits, SEEK_SET);
195
196     // create scanline to read data into
197     rowbytes = ((info.biWidth * pixbytes) + 3) / 4;
198     rowbytes *= 4;
199
200     scanline = reinterpret_cast<char*>(g_malloc(rowbytes));
201
202     // alloc space for new bitmap
203     bit->data = reinterpret_cast<unsigned char*>(g_malloc(info.biWidth * pixbytes * info.biHeight));
204
205     // read in image
206     for (i = 0; i < info.biHeight; i++)
207     {
208             BMPLine(f, scanline, pixbytes, info.biWidth, info.biCompression);
209
210             // store line
211             memcpy(&bit->data[info.biWidth * pixbytes * (info.biHeight - i - 1)], scanline, info.biWidth * pixbytes);
212     }
213
214     g_free(scanline);
215     fclose(f);
216 }
217
218
219
220 static void BMPEncodeLine(FILE *f, unsigned char *data, int npxls, int pixbytes)
221 {
222     int nbytes, i, j, k;
223
224         switch (pixbytes)
225         {
226             case 1 :
227             nbytes = (npxls + 3) / 4;
228             nbytes *= 4;
229
230             fwrite(data, npxls, 1, f);
231             nbytes -= npxls;
232
233             while (nbytes-- > 0) fputc(0, f);
234                         return;
235
236         case 3 :
237                         // reorder rgb to bgr
238                         for (i = 0, j = 0; i < npxls; i++, j+= 3)
239                         {
240                                 k = data[j];
241                                 data[j] = data[j + 2];
242                                 data[j + 2] = k;
243                         }
244
245                         nbytes = ((npxls * 3) + 3) / 4;
246                         nbytes *= 4;
247
248                         fwrite(data, npxls, 3, f);
249                         nbytes -= npxls * 3;
250
251                         while (nbytes-- > 0) fputc(0, f);
252                         return;
253         }
254
255         Error("BMPEncodeLine Failed.");
256 }
257
258
259
260 void WriteBMP(char *filename, bitmap_t *bit)
261 {
262     FILE    *f;
263     bmphd_t  header;
264     binfo_t  info;
265     drgb_t   q;        // palette that gets written
266     long     bmofs;
267     int      w, h, i;
268         int      pixbytes;
269
270     if      (bit->bpp == 8)  pixbytes = 1;
271         else if (bit->bpp == 24) pixbytes = 3;
272
273         else
274     {
275     Error("Only 8BPP and 24BPP supported");
276                 //Error("BPP %d not supported.", bit->bpp);
277     }
278
279
280     if ((f = fopen(filename, "wb")) == NULL)
281     {
282             Error("Unable to open file");//%s.", filename);
283     }
284
285     // write out an empty header as a place holder
286     if (fwrite(&header, sizeof(header), 1, f) != 1)
287     {
288             Error("Unable to fwrite.");
289     }
290
291     // init and write info header
292     info.biSize          = sizeof(binfo_t);
293     info.biWidth         = bit->width;
294     info.biHeight        = bit->height;
295     info.biPlanes        = 1;
296     info.biBitCount      = bit->bpp;
297     info.biCompression   = xBI_NONE;
298     info.biSizeImage     = bit->width * bit->height;
299     info.biXPelsPerMeter = 0;
300     info.biYPelsPerMeter = 0;
301     info.biClrUsed       = 256;
302     info.biClrImportant  = 256;
303
304     if (fwrite(&info, sizeof(binfo_t), 1, f) != 1)
305     {
306             Error("fwrite failed.");
307     }
308
309     // write out palette if we need to
310         if (bit->bpp == 8)
311         {
312         for (i = 0; i < 256; i++)
313         {
314                 q.red   = bit->palette[i].r;
315                 q.green = bit->palette[i].g;
316                 q.blue  = bit->palette[i].b;
317
318                 fwrite(&q, sizeof(q), 1, f);
319                 }
320     }
321
322     // save offset to start of bitmap
323     bmofs = ftell(f);
324
325     // output bitmap
326     w = bit->width;
327     h = bit->height;
328
329     for (i = h - 1; i >= 0; i--)
330     {
331             BMPEncodeLine(f, &bit->data[w * pixbytes * i], w, pixbytes);
332     }
333
334     // update and rewrite file header
335     header.bfType    = BMP_SIGNATURE_WORD;
336     header.bfSize    = ftell(f);
337     header.bfOffBits = bmofs;
338
339     fseek(f, 0L, SEEK_SET);
340     fwrite(&header, sizeof(header), 1, f);
341
342     fclose(f);
343 }
344
345
346 void NewBMP(int width, int height, int bpp, bitmap_t *bit)
347 {
348         int pixbytes;
349
350         if      (bpp == 8)  pixbytes = 1;
351         else if (bpp == 24) pixbytes = 3;
352
353         else
354         {
355                 Error("NewBMP: 8 or 24 bit only.");
356         }
357
358         bit->bpp    = bpp;
359         bit->width  = width;
360         bit->height = height;
361
362         bit->data = reinterpret_cast<unsigned char*>(g_malloc(width * height * pixbytes));
363
364         if (bit->data == NULL)
365         {
366                 Error("NewBMP: g_malloc failed.");
367         }
368
369         // see if we need to create a palette
370         if (pixbytes == 1)
371         {
372                 bit->palette = (rgb_t *) g_malloc(768);
373
374                 if (bit->palette == NULL)
375                 {
376                         Error("NewBMP: unable to g_malloc palette.");
377                 }
378         }
379         else
380         {
381                 bit->palette = NULL;
382         }
383 }
384
385
386
387 void FreeBMP(bitmap_t *bitmap)
388 {
389     if (bitmap->palette)
390     {
391             g_free(bitmap->palette);
392             bitmap->palette = NULL;
393     }
394
395     if (bitmap->data)
396     {
397             g_free(bitmap->data);
398             bitmap->data = NULL;
399     }
400 }
401
402