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