Fix syntax for ID keyword.
[blender-staging.git] / source / blender / imbuf / intern / radiance_hdr.c
1 /*
2  * radiance_hdr.c
3  *
4  * $Id$
5  *
6  * ***** BEGIN GPL LICENSE BLOCK *****
7  *
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License
10  * as published by the Free Software Foundation; either version 2
11  * of the License, or (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software Foundation,
20  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21  *
22  * The Original Code is Copyright
23  * All rights reserved.
24  *
25  * The Original Code is: all of this file.
26  *
27  * Contributor(s): none yet.
28  *
29  * ***** END GPL LICENSE BLOCK *****
30 */
31
32 /* ----------------------------------------------------------------------
33   Radiance High Dynamic Range image file IO
34   For description and code for reading/writing of radiance hdr files 
35         by Greg Ward, refer to:
36   http://radsite.lbl.gov/radiance/refer/Notes/picture_format.html
37 ----------------------------------------------------------------------
38 */
39
40 #ifdef WIN32
41 #include <io.h>
42 #endif
43 #include "BLI_blenlib.h"
44
45 #include "imbuf.h"
46 #include "imbuf_patch.h"
47
48 #include "IMB_imbuf_types.h"
49 #include "IMB_imbuf.h"
50
51 #include "IMB_allocimbuf.h"
52 #include "IMB_cmap.h"
53 #include "IMB_radiance_hdr.h"
54
55 /* needed constants */
56 #define MINELEN 8
57 #define MAXELEN 0x7fff
58 #define MINRUN  4       /* minimum run length */
59 #define RED 0
60 #define GRN 1
61 #define BLU 2
62 #define EXP 3
63 #define COLXS 128
64 #define STR_MAX 540
65 typedef unsigned char RGBE[4];
66 typedef float fCOLOR[3];
67 /* copy source -> dest */
68 #define copy_rgbe(c1, c2) (c2[RED]=c1[RED], c2[GRN]=c1[GRN], c2[BLU]=c1[BLU], c2[EXP]=c1[EXP])
69 #define copy_fcol(f1, f2) (f2[RED]=f1[RED], f2[GRN]=f1[GRN], f2[BLU]=f1[BLU])
70
71 /* read routines */
72 static unsigned char* oldreadcolrs(RGBE *scan, unsigned char *mem, int xmax)
73 {
74         int i, rshift = 0, len = xmax;
75         while (len > 0) {
76                 scan[0][RED] = *mem++;
77                 scan[0][GRN] = *mem++;
78                 scan[0][BLU] = *mem++;
79                 scan[0][EXP] = *mem++;
80                 if (scan[0][RED] == 1 && scan[0][GRN] == 1 && scan[0][BLU] == 1) {
81                         for (i=scan[0][EXP]<<rshift;i>0;i--) {
82                                 copy_rgbe(scan[-1], scan[0]);
83                                 scan++;
84                                 len--;
85                         }
86                         rshift += 8;
87                 }
88                 else {
89                         scan++;
90                         len--;
91                         rshift = 0;
92                 }
93         }
94         return mem;
95 }
96
97 static unsigned char* freadcolrs(RGBE *scan, unsigned char* mem, int xmax)
98 {
99         int i, j, code, val;
100
101         if ((xmax < MINELEN) | (xmax > MAXELEN)) return oldreadcolrs(scan, mem, xmax);
102
103         i = *mem++;
104         if (i != 2) return oldreadcolrs(scan, mem-1, xmax);
105
106         scan[0][GRN] = *mem++;
107         scan[0][BLU] = *mem++;
108
109         i = *mem++;
110         if (((scan[0][BLU] << 8) | i) != xmax) return NULL;
111
112         for (i=0;i<4;i++)
113                 for (j=0;j<xmax;) {
114                         code = *mem++;
115                         if (code > 128) {
116                                 code &= 127;
117                                 val = *mem++;
118                                 while (code--)
119                                         scan[j++][i] = (unsigned char)val;
120                         }
121                         else
122                                 while (code--)
123                                         scan[j++][i] = *mem++;
124                 }
125         return mem;
126 }
127
128 /* helper functions */
129
130 /* rgbe -> float color */
131 static void RGBE2FLOAT(RGBE rgbe, fCOLOR fcol)
132 {
133         if (rgbe[EXP]==0) {
134                 fcol[RED] = fcol[GRN] = fcol[BLU] = 0;
135         }
136         else {
137                 float f = ldexp(1.0, rgbe[EXP]-(COLXS+8));
138                 fcol[RED] = f*(rgbe[RED] + 0.5f);
139                 fcol[GRN] = f*(rgbe[GRN] + 0.5f);
140                 fcol[BLU] = f*(rgbe[BLU] + 0.5f);
141         }
142 }
143
144 /* float color -> rgbe */
145 static void FLOAT2RGBE(fCOLOR fcol, RGBE rgbe)
146 {
147         int e;
148         float d = (fcol[RED]>fcol[GRN]) ? fcol[RED] : fcol[GRN];
149         if (fcol[BLU]>d) d = fcol[BLU];
150         if (d <= 1e-32f)
151                 rgbe[RED] = rgbe[GRN] = rgbe[BLU] = rgbe[EXP] = 0;
152         else {
153                 d = frexp(d, &e) * 256.f / d;
154                 rgbe[RED] = (unsigned char)(fcol[RED] * d);
155                 rgbe[GRN] = (unsigned char)(fcol[GRN] * d);
156                 rgbe[BLU] = (unsigned char)(fcol[BLU] * d);
157                 rgbe[EXP] = (unsigned char)(e + COLXS);
158         }
159 }
160
161 /* ImBuf read */
162
163 int imb_is_a_hdr(void *buf)
164 {
165         // For recognition, Blender only loads first 32 bytes, so use #?RADIANCE id instead
166         // update: actually, the 'RADIANCE' part is just an optional program name, the magic word is really only the '#?' part
167         //if (strstr((char*)buf, "#?RADIANCE")) return 1;
168         if (strstr((char*)buf, "#?")) return 1;
169         // if (strstr((char*)buf, "32-bit_rle_rgbe")) return 1;
170         return 0;
171 }
172
173 struct ImBuf *imb_loadhdr(unsigned char *mem, int size, int flags)
174 {
175         struct ImBuf* ibuf;
176         RGBE* sline;
177         fCOLOR fcol;
178         float* rect_float;
179         int found=0;
180         int width=0, height=0;
181         int x, y;
182         unsigned char* ptr;
183         char oriY[80], oriX[80];
184
185         if (imb_is_a_hdr((void*)mem))
186         {
187                 /* find empty line, next line is resolution info */
188                 for (x=1;x<size;x++) {
189                         if ((mem[x-1]=='\n') && (mem[x]=='\n')) {
190                                 found = 1;
191                                 break;
192                         }
193                 }
194                 if (found && (x<(size + 2))) {
195                         if (sscanf((char *)&mem[x+1], "%79s %d %79s %d", (char*)&oriY, &height, 
196                                 (char*)&oriX, &width) != 4) return NULL;
197
198                         /* find end of this line, data right behind it */
199                         ptr = (unsigned char *)strchr((char*)&mem[x+1], '\n');
200                         ptr++;
201
202                         if (flags & IB_test) ibuf = IMB_allocImBuf(width, height, 32, 0, 0);
203                         else ibuf = IMB_allocImBuf(width, height, 32, (flags & IB_rect)|IB_rectfloat, 0);
204
205                         if (ibuf==NULL) return NULL;
206                         ibuf->ftype = RADHDR;
207                         ibuf->profile = IB_PROFILE_LINEAR_RGB;
208                         ibuf->xorig = ibuf->yorig = 0;
209
210                         if (flags & IB_test) return ibuf;
211
212                         /* read in and decode the actual data */
213                         sline = (RGBE*)MEM_mallocN(sizeof(RGBE)*width, "radhdr_read_tmpscan");
214                         rect_float = (float *)ibuf->rect_float;
215                         
216                         for (y=0;y<height;y++) {
217                                 ptr = freadcolrs(sline, ptr, width);
218                                 if (ptr==NULL) {
219                                         printf("HDR decode error\n");
220                                         MEM_freeN(sline);
221                                         return ibuf;
222                                 }
223                                 for (x=0;x<width;x++) {
224                                         /* convert to ldr */
225                                         RGBE2FLOAT(sline[x], fcol);
226                                         *rect_float++ = fcol[RED];
227                                         *rect_float++ = fcol[GRN];
228                                         *rect_float++ = fcol[BLU];
229                                         *rect_float++ = 1.0f;
230                                 }
231                         }
232                         MEM_freeN(sline);
233                         if (oriY[0]=='-') IMB_flipy(ibuf);
234                         
235                         if (flags & IB_rect) {
236                                 IMB_rect_from_float(ibuf);
237                         }
238                         
239                         return ibuf;
240                 }
241                 //else printf("Data not found!\n");
242         }
243         //else printf("Not a valid radiance HDR file!\n");
244
245         return NULL;
246 }
247
248 /* ImBuf write */
249 static int fwritecolrs(FILE* file, int width, int channels, unsigned char* ibufscan, float* fpscan)
250 {
251         int x, i, j, beg, c2, cnt=0;
252         fCOLOR fcol;
253         RGBE rgbe, *rgbe_scan;
254
255         if ((ibufscan==NULL) && (fpscan==NULL)) return 0;
256
257         rgbe_scan = (RGBE*)MEM_mallocN(sizeof(RGBE)*width, "radhdr_write_tmpscan");
258
259         /* convert scanline */
260         j= 0;
261         for (i=0;i<width;i++) {
262                 if (fpscan) {
263                         fcol[RED] = fpscan[j];
264                         fcol[GRN] = (channels >= 2)? fpscan[j+1]: fpscan[j];
265                         fcol[BLU] = (channels >= 3)? fpscan[j+2]: fpscan[j];
266                 } else {
267                         fcol[RED] = (float)ibufscan[j] / 255.f;
268                         fcol[GRN] = (float)((channels >= 2)? ibufscan[j+1]: ibufscan[j]) / 255.f;
269                         fcol[BLU] = (float)((channels >= 3)? ibufscan[j+2]: ibufscan[j]) / 255.f;
270                 }
271                 FLOAT2RGBE(fcol, rgbe);
272                 copy_rgbe(rgbe, rgbe_scan[i]);
273                 j+=channels;
274         }
275
276         if ((width < MINELEN) | (width > MAXELEN)) {    /* OOBs, write out flat */
277                 x=fwrite((char *)rgbe_scan, sizeof(RGBE), width, file) - width;
278                 MEM_freeN(rgbe_scan);
279                 return x;
280         }
281         /* put magic header */
282         putc(2, file);
283         putc(2, file);
284         putc((unsigned char)(width >> 8), file);
285         putc((unsigned char)(width & 255), file);
286         /* put components separately */
287         for (i=0;i<4;i++) {
288                 for (j=0;j<width;j+=cnt) {      /* find next run */
289                         for (beg=j;beg<width;beg+=cnt) {
290                                 for (cnt=1;(cnt<127) && ((beg+cnt)<width) && (rgbe_scan[beg+cnt][i] == rgbe_scan[beg][i]); cnt++);
291                                 if (cnt>=MINRUN) break;   /* long enough */
292                         }
293                         if (((beg-j)>1) && ((beg-j) < MINRUN)) {
294                                 c2 = j+1;
295                                 while (rgbe_scan[c2++][i] == rgbe_scan[j][i])
296                                         if (c2 == beg) {        /* short run */
297                                                 putc((unsigned char)(128+beg-j), file);
298                                                 putc((unsigned char)(rgbe_scan[j][i]), file);
299                                                 j = beg;
300                                                 break;
301                                         }
302                         }
303                         while (j < beg) {     /* write out non-run */
304                                 if ((c2 = beg-j) > 128) c2 = 128;
305                                 putc((unsigned char)(c2), file);
306                                 while (c2--) putc(rgbe_scan[j++][i], file);
307                         }
308                         if (cnt >= MINRUN) {      /* write out run */
309                                 putc((unsigned char)(128+cnt), file);
310                                 putc(rgbe_scan[beg][i], file);
311                         }
312                         else cnt = 0;
313                 }
314         }
315         MEM_freeN(rgbe_scan);
316         return(ferror(file) ? -1 : 0);
317 }
318
319 static void writeHeader(FILE *file, int width, int height)
320 {
321         fprintf(file, "#?RADIANCE");
322         fputc(10, file);
323         fprintf(file, "# %s", "Created with Blender");
324         fputc(10, file);
325         fprintf(file, "EXPOSURE=%25.13f", 1.0);
326         fputc(10, file);
327         fprintf(file, "FORMAT=32-bit_rle_rgbe");
328         fputc(10, file);
329         fputc(10, file);
330         fprintf(file, "-Y %d +X %d", height, width);
331         fputc(10, file);
332 }
333
334 short imb_savehdr(struct ImBuf *ibuf, char *name, int flags)
335 {
336         FILE* file = fopen(name, "wb");
337         float *fp= NULL;
338         int y, width=ibuf->x, height=ibuf->y;
339         unsigned char *cp= NULL;
340         
341         if (file==NULL) return 0;
342
343         writeHeader(file, width, height);
344
345         if(ibuf->rect)
346                 cp= (unsigned char *)ibuf->rect + ibuf->channels*(height-1)*width;
347         if(ibuf->rect_float)
348                 fp= ibuf->rect_float + ibuf->channels*(height-1)*width;
349         
350         for (y=height-1;y>=0;y--) {
351                 if (fwritecolrs(file, width, ibuf->channels, cp, fp) < 0) {
352                         fclose(file);
353                         printf("HDR write error\n");
354                         return 0;
355                 }
356                 if(cp) cp-= ibuf->channels*width;
357                 if(fp) fp-= ibuf->channels*width;
358         }
359
360         fclose(file);
361         return 1;
362 }
363