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