Move allocation of imbuf from array to allocimbuf.
[blender.git] / source / blender / imbuf / intern / bmp.c
1 /*
2  * ***** BEGIN GPL LICENSE BLOCK *****
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software Foundation,
16  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  *
18  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
19  * All rights reserved.
20  *
21  * The Original Code is: all of this file.
22  *
23  * Contributor(s): none yet.
24  *
25  * ***** END GPL LICENSE BLOCK *****
26  */
27
28 /** \file blender/imbuf/intern/bmp.c
29  *  \ingroup imbuf
30  */
31
32 #include "BLI_utildefines.h"
33 #include "BLI_fileops.h"
34
35 #include "imbuf.h"
36
37 #include "IMB_imbuf_types.h"
38 #include "IMB_imbuf.h"
39 #include "IMB_filetype.h"
40
41 #include "IMB_colormanagement.h"
42 #include "IMB_colormanagement_intern.h"
43
44 /* some code copied from article on microsoft.com, copied
45  * here for enhanced BMP support in the future
46  * http://www.microsoft.com/msj/defaultframe.asp?page=/msj/0197/mfcp1/mfcp1.htm&nav=/msj/0197/newnav.htm
47  */
48
49 typedef struct BMPINFOHEADER {
50         unsigned int    biSize;
51         unsigned int    biWidth;
52         unsigned int    biHeight;
53         unsigned short  biPlanes;
54         unsigned short  biBitCount;
55         unsigned int    biCompression;
56         unsigned int    biSizeImage;
57         unsigned int    biXPelsPerMeter;
58         unsigned int    biYPelsPerMeter;
59         unsigned int    biClrUsed;
60         unsigned int    biClrImportant;
61 } BMPINFOHEADER;
62
63 #if 0
64 typedef struct BMPHEADER {
65         unsigned short biType;
66         unsigned int biSize;
67         unsigned short biRes1;
68         unsigned short biRes2;
69         unsigned int biOffBits;
70 } BMPHEADER;
71 #endif
72
73 #define BMP_FILEHEADER_SIZE 14
74
75 #define CHECK_HEADER_FIELD(_mem, _field) ((_mem[0] == _field[0]) && (_mem[1] == _field[1]))
76 #define CHECK_HEADER_FIELD_BMP(_mem)  \
77         (CHECK_HEADER_FIELD(_mem, "BM") ||  \
78          CHECK_HEADER_FIELD(_mem, "BA") ||  \
79          CHECK_HEADER_FIELD(_mem, "CI") ||  \
80          CHECK_HEADER_FIELD(_mem, "CP") ||  \
81          CHECK_HEADER_FIELD(_mem, "IC") ||  \
82          CHECK_HEADER_FIELD(_mem, "PT"))
83
84 static int checkbmp(unsigned char *mem)
85 {
86
87         int ret_val = 0;
88         BMPINFOHEADER bmi;
89         unsigned int u;
90
91         if (mem) {
92                 if (CHECK_HEADER_FIELD_BMP(mem)) {
93                         /* skip fileheader */
94                         mem += BMP_FILEHEADER_SIZE;
95                 }
96                 else {
97                         return 0;
98                 }
99
100                 /* for systems where an int needs to be 4 bytes aligned */
101                 memcpy(&bmi, mem, sizeof(bmi));
102
103                 u = LITTLE_LONG(bmi.biSize);
104                 /* we only support uncompressed images for now. */
105                 if (u >= sizeof(BMPINFOHEADER)) {
106                         if (bmi.biCompression == 0) {
107                                 u = LITTLE_SHORT(bmi.biBitCount);
108                                 if (u >= 8) {
109                                         ret_val = 1;
110                                 }
111                         }
112                 }
113         }
114
115         return(ret_val);
116 }
117
118 int imb_is_a_bmp(unsigned char *buf)
119 {
120         return checkbmp(buf);
121 }
122
123 struct ImBuf *imb_bmp_decode(unsigned char *mem, size_t size, int flags, char colorspace[IM_MAX_SPACE])
124 {
125         struct ImBuf *ibuf = NULL;
126         BMPINFOHEADER bmi;
127         int x, y, depth, ibuf_depth, skip, i, j;
128         unsigned char *bmp, *rect;
129         unsigned short col;
130         double xppm, yppm;
131         bool top_to_bottom = false;
132         
133         (void)size; /* unused */
134
135         if (checkbmp(mem) == 0) return(NULL);
136
137         colorspace_set_default_role(colorspace, IM_MAX_SPACE, COLOR_ROLE_DEFAULT_BYTE);
138
139         if (CHECK_HEADER_FIELD_BMP(mem)) {
140                 /* skip fileheader */
141                 mem += BMP_FILEHEADER_SIZE;
142         }
143
144         /* for systems where an int needs to be 4 bytes aligned */
145         memcpy(&bmi, mem, sizeof(bmi));
146
147         skip = LITTLE_LONG(bmi.biSize);
148         x = LITTLE_LONG(bmi.biWidth);
149         y = LITTLE_LONG(bmi.biHeight);
150         depth = LITTLE_SHORT(bmi.biBitCount);
151         xppm = LITTLE_LONG(bmi.biXPelsPerMeter);
152         yppm = LITTLE_LONG(bmi.biYPelsPerMeter);
153
154         if (depth <= 8) {
155                 ibuf_depth = 24;
156         }
157         else {
158                 ibuf_depth = depth;
159         }
160
161         if (y < 0) {
162                 /* Negative height means bitmap is stored top-to-bottom... */
163                 y = -y;
164                 top_to_bottom = true;
165         }
166
167 #if 0
168         printf("skip: %d, x: %d y: %d, depth: %d (%x)\n", skip, x, y, depth, bmi.biBitCount);
169 #endif
170
171         if (flags & IB_test) {
172                 ibuf = IMB_allocImBuf(x, y, ibuf_depth, 0);
173         }
174         else {
175                 ibuf = IMB_allocImBuf(x, y, ibuf_depth, IB_rect);
176                 bmp = mem + skip;
177                 rect = (unsigned char *) ibuf->rect;
178
179                 if (depth == 8) {
180                         const int x_pad = (4 - (x % 4)) % 4;
181                         const char (*palette)[4] = (void *)bmp;
182                         bmp += bmi.biClrUsed * 4;
183                         for (i = y; i > 0; i--) {
184                                 if (top_to_bottom) {
185                                         rect = (unsigned char *) &ibuf->rect[(i - 1) * x];
186                                 }
187                                 for (j = x; j > 0; j--) {
188                                         const char *pcol = palette[bmp[0]];
189                                         rect[0] = pcol[0];
190                                         rect[1] = pcol[1];
191                                         rect[2] = pcol[2];
192
193                                         rect[3] = 255;
194                                         rect += 4; bmp += 1;
195                                 }
196                                 /* rows are padded to multiples of 4 */
197                                 bmp += x_pad;
198                         }
199                 }
200                 else if (depth == 16) {
201                         for (i = y; i > 0; i--) {
202                                 if (top_to_bottom) {
203                                         rect = (unsigned char *) &ibuf->rect[(i - 1) * x];
204                                 }
205                                 for (j = x; j > 0; j--) {
206                                         col = bmp[0] + (bmp[1] << 8);
207                                         rect[0] = ((col >> 10) & 0x1f) << 3;
208                                         rect[1] = ((col >>  5) & 0x1f) << 3;
209                                         rect[2] = ((col >>  0) & 0x1f) << 3;
210
211                                         rect[3] = 255;
212                                         rect += 4; bmp += 2;
213                                 }
214                         }
215                 }
216                 else if (depth == 24) {
217                         const int x_pad = x % 4;
218                         for (i = y; i > 0; i--) {
219                                 if (top_to_bottom) {
220                                         rect = (unsigned char *) &ibuf->rect[(i - 1) * x];
221                                 }
222                                 for (j = x; j > 0; j--) {
223                                         rect[0] = bmp[2];
224                                         rect[1] = bmp[1];
225                                         rect[2] = bmp[0];
226                                         
227                                         rect[3] = 255;
228                                         rect += 4; bmp += 3;
229                                 }
230                                 /* for 24-bit images, rows are padded to multiples of 4 */
231                                 bmp += x_pad;
232                         }
233                 }
234                 else if (depth == 32) {
235                         for (i = y; i > 0; i--) {
236                                 if (top_to_bottom) {
237                                         rect = (unsigned char *) &ibuf->rect[(i - 1) * x];
238                                 }
239                                 for (j = x; j > 0; j--) {
240                                         rect[0] = bmp[2];
241                                         rect[1] = bmp[1];
242                                         rect[2] = bmp[0];
243                                         rect[3] = bmp[3];
244                                         rect += 4; bmp += 4;
245                                 }
246                         }
247                 }
248         }
249
250         if (ibuf) {
251                 ibuf->ppm[0] = xppm;
252                 ibuf->ppm[1] = yppm;
253                 ibuf->ftype = BMP;
254         }
255         
256         return(ibuf);
257 }
258
259 #undef CHECK_HEADER_FIELD_BMP
260 #undef CHECK_HEADER_FIELD
261
262 /* Couple of helper functions for writing our data */
263 static int putIntLSB(unsigned int ui, FILE *ofile)
264 {
265         putc((ui >> 0) & 0xFF, ofile);
266         putc((ui >> 8) & 0xFF, ofile);
267         putc((ui >> 16) & 0xFF, ofile);
268         return putc((ui >> 24) & 0xFF, ofile);
269 }
270
271 static int putShortLSB(unsigned short us, FILE *ofile)
272 {
273         putc((us >> 0) & 0xFF, ofile);
274         return putc((us >> 8) & 0xFF, ofile);
275
276
277 /* Found write info at http://users.ece.gatech.edu/~slabaugh/personal/c/bitmapUnix.c */
278 int imb_savebmp(struct ImBuf *ibuf, const char *name, int flags)
279 {
280         BMPINFOHEADER infoheader;
281         int bytesize, extrabytes, x, y, t, ptr;
282         uchar *data;
283         FILE *ofile;
284         
285         (void)flags; /* unused */
286
287         extrabytes = (4 - ibuf->x * 3 % 4) % 4;
288         bytesize = (ibuf->x * 3 + extrabytes) * ibuf->y;
289
290         data = (uchar *) ibuf->rect;
291         ofile = BLI_fopen(name, "wb");
292         if (!ofile) return 0;
293
294         putShortLSB(19778, ofile); /* "BM" */
295         putIntLSB(bytesize + BMP_FILEHEADER_SIZE + sizeof(infoheader), ofile);  /* Total file size */
296         putShortLSB(0, ofile); /* Res1 */
297         putShortLSB(0, ofile); /* Res2 */
298         putIntLSB(BMP_FILEHEADER_SIZE + sizeof(infoheader), ofile);
299
300         putIntLSB(sizeof(infoheader), ofile);
301         putIntLSB(ibuf->x, ofile);
302         putIntLSB(ibuf->y, ofile);
303         putShortLSB(1, ofile);
304         putShortLSB(24, ofile);
305         putIntLSB(0, ofile);
306         putIntLSB(bytesize, ofile);
307         putIntLSB((int)(ibuf->ppm[0] + 0.5), ofile);
308         putIntLSB((int)(ibuf->ppm[1] + 0.5), ofile);
309         putIntLSB(0, ofile);
310         putIntLSB(0, ofile);
311
312         /* Need to write out padded image data in bgr format */
313         for (y = 0; y < ibuf->y; y++) {
314                 for (x = 0; x < ibuf->x; x++) {
315                         ptr = (x + y * ibuf->x) * 4;
316                         if (putc(data[ptr + 2], ofile) == EOF) return 0;
317                         if (putc(data[ptr + 1], ofile) == EOF) return 0;
318                         if (putc(data[ptr], ofile) == EOF) return 0;
319                 }
320                 /* add padding here */
321                 for (t = 0; t < extrabytes; t++) {
322                         if (putc(0, ofile) == EOF) return 0;
323                 }
324         }
325         if (ofile) {
326                 fflush(ofile);
327                 fclose(ofile);
328         }
329         return 1;
330 }