This is patch# 19017 16bit SGI image loading
[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., 59 Temple Place - Suite 330, Boston, MA  02111-1307, 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->xorig = ibuf->yorig = 0;
208
209                         if (flags & IB_test) return ibuf;
210
211                         /* read in and decode the actual data */
212                         sline = (RGBE*)MEM_mallocN(sizeof(RGBE)*width, "radhdr_read_tmpscan");
213                         rect_float = (float *)ibuf->rect_float;
214                         
215                         for (y=0;y<height;y++) {
216                                 ptr = freadcolrs(sline, ptr, width);
217                                 if (ptr==NULL) {
218                                         printf("HDR decode error\n");
219                                         MEM_freeN(sline);
220                                         return ibuf;
221                                 }
222                                 for (x=0;x<width;x++) {
223                                         /* convert to ldr */
224                                         RGBE2FLOAT(sline[x], fcol);
225                                         *rect_float++ = fcol[RED];
226                                         *rect_float++ = fcol[GRN];
227                                         *rect_float++ = fcol[BLU];
228                                         *rect_float++ = 1.0f;
229                                 }
230                         }
231                         MEM_freeN(sline);
232                         if (oriY[0]=='-') IMB_flipy(ibuf);
233                         
234                         if (flags & IB_rect) {
235                                 IMB_rect_from_float(ibuf);
236                         }
237                         
238                         return ibuf;
239                 }
240                 //else printf("Data not found!\n");
241         }
242         //else printf("Not a valid radiance HDR file!\n");
243
244         return NULL;
245 }
246
247 /* ImBuf write */
248 static int fwritecolrs(FILE* file, int width, int channels, unsigned char* ibufscan, float* fpscan)
249 {
250         int x, i, j, beg, c2, cnt=0;
251         fCOLOR fcol;
252         RGBE rgbe, *rgbe_scan;
253
254         if ((ibufscan==NULL) && (fpscan==NULL)) return 0;
255
256         rgbe_scan = (RGBE*)MEM_mallocN(sizeof(RGBE)*width, "radhdr_write_tmpscan");
257
258         /* convert scanline */
259         j= 0;
260         for (i=0;i<width;i++) {
261                 if (fpscan) {
262                         fcol[RED] = fpscan[j];
263                         fcol[GRN] = (channels >= 2)? fpscan[j+1]: fpscan[j];
264                         fcol[BLU] = (channels >= 3)? fpscan[j+2]: fpscan[j];
265                 } else {
266                         fcol[RED] = (float)ibufscan[j] / 255.f;
267                         fcol[GRN] = (float)((channels >= 2)? ibufscan[j+1]: ibufscan[j]) / 255.f;
268                         fcol[BLU] = (float)((channels >= 3)? ibufscan[j+2]: ibufscan[j]) / 255.f;
269                 }
270                 FLOAT2RGBE(fcol, rgbe);
271                 copy_rgbe(rgbe, rgbe_scan[i]);
272                 j+=channels;
273         }
274
275         if ((width < MINELEN) | (width > MAXELEN)) {    /* OOBs, write out flat */
276                 x=fwrite((char *)rgbe_scan, sizeof(RGBE), width, file) - width;
277                 MEM_freeN(rgbe_scan);
278                 return x;
279         }
280         /* put magic header */
281         putc(2, file);
282         putc(2, file);
283         putc((unsigned char)(width >> 8), file);
284         putc((unsigned char)(width & 255), file);
285         /* put components seperately */
286         for (i=0;i<4;i++) {
287                 for (j=0;j<width;j+=cnt) {      /* find next run */
288                         for (beg=j;beg<width;beg+=cnt) {
289                                 for (cnt=1;(cnt<127) && ((beg+cnt)<width) && (rgbe_scan[beg+cnt][i] == rgbe_scan[beg][i]); cnt++);
290                                 if (cnt>=MINRUN) break;   /* long enough */
291                         }
292                         if (((beg-j)>1) && ((beg-j) < MINRUN)) {
293                                 c2 = j+1;
294                                 while (rgbe_scan[c2++][i] == rgbe_scan[j][i])
295                                         if (c2 == beg) {        /* short run */
296                                                 putc((unsigned char)(128+beg-j), file);
297                                                 putc((unsigned char)(rgbe_scan[j][i]), file);
298                                                 j = beg;
299                                                 break;
300                                         }
301                         }
302                         while (j < beg) {     /* write out non-run */
303                                 if ((c2 = beg-j) > 128) c2 = 128;
304                                 putc((unsigned char)(c2), file);
305                                 while (c2--) putc(rgbe_scan[j++][i], file);
306                         }
307                         if (cnt >= MINRUN) {      /* write out run */
308                                 putc((unsigned char)(128+cnt), file);
309                                 putc(rgbe_scan[beg][i], file);
310                         }
311                         else cnt = 0;
312                 }
313         }
314         MEM_freeN(rgbe_scan);
315         return(ferror(file) ? -1 : 0);
316 }
317
318 static void writeHeader(FILE *file, int width, int height)
319 {
320         fprintf(file, "#?RADIANCE");
321         fputc(10, file);
322         fprintf(file, "# %s", "Created with Blender");
323         fputc(10, file);
324         fprintf(file, "EXPOSURE=%25.13f", 1.0);
325         fputc(10, file);
326         fprintf(file, "FORMAT=32-bit_rle_rgbe");
327         fputc(10, file);
328         fputc(10, file);
329         fprintf(file, "-Y %d +X %d", height, width);
330         fputc(10, file);
331 }
332
333 short imb_savehdr(struct ImBuf *ibuf, char *name, int flags)
334 {
335         FILE* file = fopen(name, "wb");
336         float *fp= NULL;
337         int y, width=ibuf->x, height=ibuf->y;
338         unsigned char *cp= NULL;
339         
340         if (file==NULL) return 0;
341
342         writeHeader(file, width, height);
343
344         if(ibuf->rect)
345                 cp= (unsigned char *)ibuf->rect + ibuf->channels*(height-1)*width;
346         if(ibuf->rect_float)
347                 fp= ibuf->rect_float + ibuf->channels*(height-1)*width;
348         
349         for (y=height-1;y>=0;y--) {
350                 if (fwritecolrs(file, width, ibuf->channels, cp, fp) < 0) {
351                         fclose(file);
352                         printf("HDR write error\n");
353                         return 0;
354                 }
355                 if(cp) cp-= ibuf->channels*width;
356                 if(fp) fp-= ibuf->channels*width;
357         }
358
359         fclose(file);
360         return 1;
361 }
362