Blender V2.27

radiance_hdr.c

00001 /*
00002  * radiance_hdr.c
00003  *
00004  * $Id: radiance_hdr.c 33167 2010-11-19 02:14:18Z campbellbarton $
00005  *
00006  * ***** BEGIN GPL LICENSE BLOCK *****
00007  *
00008  * This program is free software; you can redistribute it and/or
00009  * modify it under the terms of the GNU General Public License
00010  * as published by the Free Software Foundation; either version 2
00011  * of the License, or (at your option) any later version.
00012  *
00013  * This program is distributed in the hope that it will be useful,
00014  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00015  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00016  * GNU General Public License for more details.
00017  *
00018  * You should have received a copy of the GNU General Public License
00019  * along with this program; if not, write to the Free Software Foundation,
00020  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
00021  *
00022  * The Original Code is Copyright
00023  * All rights reserved.
00024  *
00025  * The Original Code is: all of this file.
00026  *
00027  * Contributor(s): none yet.
00028  *
00029  * ***** END GPL LICENSE BLOCK *****
00030 */
00031 
00032 #ifdef WITH_HDR
00033 
00034 /* ----------------------------------------------------------------------
00035   Radiance High Dynamic Range image file IO
00036   For description and code for reading/writing of radiance hdr files 
00037         by Greg Ward, refer to:
00038   http://radsite.lbl.gov/radiance/refer/Notes/picture_format.html
00039 ----------------------------------------------------------------------
00040 */
00041 
00042 #ifdef WIN32
00043 #include <io.h>
00044 #endif
00045 
00046 #include "MEM_guardedalloc.h"
00047 
00048 #include "BLI_blenlib.h"
00049 
00050 #include "imbuf.h"
00051 
00052 #include "IMB_imbuf_types.h"
00053 #include "IMB_imbuf.h"
00054 
00055 #include "IMB_allocimbuf.h"
00056 #include "IMB_filetype.h"
00057 
00058 /* needed constants */
00059 #define MINELEN 8
00060 #define MAXELEN 0x7fff
00061 #define MINRUN  4       /* minimum run length */
00062 #define RED 0
00063 #define GRN 1
00064 #define BLU 2
00065 #define EXP 3
00066 #define COLXS 128
00067 #define STR_MAX 540
00068 typedef unsigned char RGBE[4];
00069 typedef float fCOLOR[3];
00070 /* copy source -> dest */
00071 #define copy_rgbe(c1, c2) (c2[RED]=c1[RED], c2[GRN]=c1[GRN], c2[BLU]=c1[BLU], c2[EXP]=c1[EXP])
00072 #define copy_fcol(f1, f2) (f2[RED]=f1[RED], f2[GRN]=f1[GRN], f2[BLU]=f1[BLU])
00073 
00074 /* read routines */
00075 static unsigned char* oldreadcolrs(RGBE *scan, unsigned char *mem, int xmax)
00076 {
00077         int i, rshift = 0, len = xmax;
00078         while (len > 0) {
00079                 scan[0][RED] = *mem++;
00080                 scan[0][GRN] = *mem++;
00081                 scan[0][BLU] = *mem++;
00082                 scan[0][EXP] = *mem++;
00083                 if (scan[0][RED] == 1 && scan[0][GRN] == 1 && scan[0][BLU] == 1) {
00084                         for (i=scan[0][EXP]<<rshift;i>0;i--) {
00085                                 copy_rgbe(scan[-1], scan[0]);
00086                                 scan++;
00087                                 len--;
00088                         }
00089                         rshift += 8;
00090                 }
00091                 else {
00092                         scan++;
00093                         len--;
00094                         rshift = 0;
00095                 }
00096         }
00097         return mem;
00098 }
00099 
00100 static unsigned char* freadcolrs(RGBE *scan, unsigned char* mem, int xmax)
00101 {
00102         int i, j, code, val;
00103 
00104         if ((xmax < MINELEN) | (xmax > MAXELEN)) return oldreadcolrs(scan, mem, xmax);
00105 
00106         i = *mem++;
00107         if (i != 2) return oldreadcolrs(scan, mem-1, xmax);
00108 
00109         scan[0][GRN] = *mem++;
00110         scan[0][BLU] = *mem++;
00111 
00112         i = *mem++;
00113         if (((scan[0][BLU] << 8) | i) != xmax) return NULL;
00114 
00115         for (i=0;i<4;i++)
00116                 for (j=0;j<xmax;) {
00117                         code = *mem++;
00118                         if (code > 128) {
00119                                 code &= 127;
00120                                 val = *mem++;
00121                                 while (code--)
00122                                         scan[j++][i] = (unsigned char)val;
00123                         }
00124                         else
00125                                 while (code--)
00126                                         scan[j++][i] = *mem++;
00127                 }
00128         return mem;
00129 }
00130 
00131 /* helper functions */
00132 
00133 /* rgbe -> float color */
00134 static void RGBE2FLOAT(RGBE rgbe, fCOLOR fcol)
00135 {
00136         if (rgbe[EXP]==0) {
00137                 fcol[RED] = fcol[GRN] = fcol[BLU] = 0;
00138         }
00139         else {
00140                 float f = ldexp(1.0, rgbe[EXP]-(COLXS+8));
00141                 fcol[RED] = f*(rgbe[RED] + 0.5f);
00142                 fcol[GRN] = f*(rgbe[GRN] + 0.5f);
00143                 fcol[BLU] = f*(rgbe[BLU] + 0.5f);
00144         }
00145 }
00146 
00147 /* float color -> rgbe */
00148 static void FLOAT2RGBE(fCOLOR fcol, RGBE rgbe)
00149 {
00150         int e;
00151         float d = (fcol[RED]>fcol[GRN]) ? fcol[RED] : fcol[GRN];
00152         if (fcol[BLU]>d) d = fcol[BLU];
00153         if (d <= 1e-32f)
00154                 rgbe[RED] = rgbe[GRN] = rgbe[BLU] = rgbe[EXP] = 0;
00155         else {
00156                 d = frexp(d, &e) * 256.f / d;
00157                 rgbe[RED] = (unsigned char)(fcol[RED] * d);
00158                 rgbe[GRN] = (unsigned char)(fcol[GRN] * d);
00159                 rgbe[BLU] = (unsigned char)(fcol[BLU] * d);
00160                 rgbe[EXP] = (unsigned char)(e + COLXS);
00161         }
00162 }
00163 
00164 /* ImBuf read */
00165 
00166 int imb_is_a_hdr(unsigned char *buf)
00167 {
00168         // For recognition, Blender only loads first 32 bytes, so use #?RADIANCE id instead
00169         // update: actually, the 'RADIANCE' part is just an optional program name, the magic word is really only the '#?' part
00170         //if (strstr((char*)buf, "#?RADIANCE")) return 1;
00171         if (strstr((char*)buf, "#?")) return 1;
00172         // if (strstr((char*)buf, "32-bit_rle_rgbe")) return 1;
00173         return 0;
00174 }
00175 
00176 struct ImBuf *imb_loadhdr(unsigned char *mem, size_t size, int flags)
00177 {
00178         struct ImBuf* ibuf;
00179         RGBE* sline;
00180         fCOLOR fcol;
00181         float* rect_float;
00182         int found=0;
00183         int width=0, height=0;
00184         int x, y;
00185         unsigned char* ptr;
00186         char oriY[80], oriX[80];
00187 
00188         if (imb_is_a_hdr((void*)mem))
00189         {
00190                 /* find empty line, next line is resolution info */
00191                 for (x=1;x<size;x++) {
00192                         if ((mem[x-1]=='\n') && (mem[x]=='\n')) {
00193                                 found = 1;
00194                                 break;
00195                         }
00196                 }
00197                 if (found && (x<(size + 2))) {
00198                         if (sscanf((char *)&mem[x+1], "%79s %d %79s %d", (char*)&oriY, &height, 
00199                                 (char*)&oriX, &width) != 4) return NULL;
00200 
00201                         /* find end of this line, data right behind it */
00202                         ptr = (unsigned char *)strchr((char*)&mem[x+1], '\n');
00203                         ptr++;
00204 
00205                         if (flags & IB_test) ibuf = IMB_allocImBuf(width, height, 32, 0);
00206                         else ibuf = IMB_allocImBuf(width, height, 32, (flags & IB_rect)|IB_rectfloat);
00207 
00208                         if (ibuf==NULL) return NULL;
00209                         ibuf->ftype = RADHDR;
00210                         ibuf->profile = IB_PROFILE_LINEAR_RGB;
00211 
00212                         if (flags & IB_test) return ibuf;
00213 
00214                         /* read in and decode the actual data */
00215                         sline = (RGBE*)MEM_mallocN(sizeof(RGBE)*width, "radhdr_read_tmpscan");
00216                         rect_float = (float *)ibuf->rect_float;
00217                         
00218                         for (y=0;y<height;y++) {
00219                                 ptr = freadcolrs(sline, ptr, width);
00220                                 if (ptr==NULL) {
00221                                         printf("HDR decode error\n");
00222                                         MEM_freeN(sline);
00223                                         return ibuf;
00224                                 }
00225                                 for (x=0;x<width;x++) {
00226                                         /* convert to ldr */
00227                                         RGBE2FLOAT(sline[x], fcol);
00228                                         *rect_float++ = fcol[RED];
00229                                         *rect_float++ = fcol[GRN];
00230                                         *rect_float++ = fcol[BLU];
00231                                         *rect_float++ = 1.0f;
00232                                 }
00233                         }
00234                         MEM_freeN(sline);
00235                         if (oriY[0]=='-') IMB_flipy(ibuf);
00236                         
00237                         if (flags & IB_rect) {
00238                                 IMB_rect_from_float(ibuf);
00239                         }
00240                         
00241                         return ibuf;
00242                 }
00243                 //else printf("Data not found!\n");
00244         }
00245         //else printf("Not a valid radiance HDR file!\n");
00246 
00247         return NULL;
00248 }
00249 
00250 /* ImBuf write */
00251 static int fwritecolrs(FILE* file, int width, int channels, unsigned char* ibufscan, float* fpscan)
00252 {
00253         int x, i, j, beg, c2, cnt=0;
00254         fCOLOR fcol;
00255         RGBE rgbe, *rgbe_scan;
00256 
00257         if ((ibufscan==NULL) && (fpscan==NULL)) return 0;
00258 
00259         rgbe_scan = (RGBE*)MEM_mallocN(sizeof(RGBE)*width, "radhdr_write_tmpscan");
00260 
00261         /* convert scanline */
00262                 j= 0;
00263         for (i=0;i<width;i++) {
00264                 if (fpscan) {
00265                         fcol[RED] = fpscan[j];
00266                         fcol[GRN] = (channels >= 2)? fpscan[j+1]: fpscan[j];
00267                         fcol[BLU] = (channels >= 3)? fpscan[j+2]: fpscan[j];
00268                 } else {
00269                         fcol[RED] = (float)ibufscan[j] / 255.f;
00270                         fcol[GRN] = (float)((channels >= 2)? ibufscan[j+1]: ibufscan[j]) / 255.f;
00271                         fcol[BLU] = (float)((channels >= 3)? ibufscan[j+2]: ibufscan[j]) / 255.f;
00272                 }
00273                 FLOAT2RGBE(fcol, rgbe);
00274                 copy_rgbe(rgbe, rgbe_scan[i]);
00275                 j+=channels;
00276         }
00277 
00278         if ((width < MINELEN) | (width > MAXELEN)) {    /* OOBs, write out flat */
00279                 x=fwrite((char *)rgbe_scan, sizeof(RGBE), width, file) - width;
00280                 MEM_freeN(rgbe_scan);
00281                 return x;
00282         }
00283         /* put magic header */
00284         putc(2, file);
00285         putc(2, file);
00286         putc((unsigned char)(width >> 8), file);
00287         putc((unsigned char)(width & 255), file);
00288         /* put components separately */
00289         for (i=0;i<4;i++) {
00290                 for (j=0;j<width;j+=cnt) {      /* find next run */
00291                         for (beg=j;beg<width;beg+=cnt) {
00292                                 for (cnt=1;(cnt<127) && ((beg+cnt)<width) && (rgbe_scan[beg+cnt][i] == rgbe_scan[beg][i]); cnt++);
00293                                 if (cnt>=MINRUN) break;   /* long enough */
00294                         }
00295                         if (((beg-j)>1) && ((beg-j) < MINRUN)) {
00296                                 c2 = j+1;
00297                                 while (rgbe_scan[c2++][i] == rgbe_scan[j][i])
00298                                         if (c2 == beg) {        /* short run */
00299                                                 putc((unsigned char)(128+beg-j), file);
00300                                                 putc((unsigned char)(rgbe_scan[j][i]), file);
00301                                                 j = beg;
00302                                                 break;
00303                                         }
00304                         }
00305                         while (j < beg) {     /* write out non-run */
00306                                 if ((c2 = beg-j) > 128) c2 = 128;
00307                                 putc((unsigned char)(c2), file);
00308                                 while (c2--) putc(rgbe_scan[j++][i], file);
00309                         }
00310                         if (cnt >= MINRUN) {      /* write out run */
00311                                 putc((unsigned char)(128+cnt), file);
00312                                 putc(rgbe_scan[beg][i], file);
00313                         }
00314                         else cnt = 0;
00315                 }
00316         }
00317         MEM_freeN(rgbe_scan);
00318         return(ferror(file) ? -1 : 0);
00319 }
00320 
00321 static void writeHeader(FILE *file, int width, int height)
00322 {
00323         fprintf(file, "#?RADIANCE");
00324         fputc(10, file);
00325         fprintf(file, "# %s", "Created with Blender");
00326         fputc(10, file);
00327         fprintf(file, "EXPOSURE=%25.13f", 1.0);
00328         fputc(10, file);
00329         fprintf(file, "FORMAT=32-bit_rle_rgbe");
00330         fputc(10, file);
00331         fputc(10, file);
00332         fprintf(file, "-Y %d +X %d", height, width);
00333         fputc(10, file);
00334 }
00335 
00336 int imb_savehdr(struct ImBuf *ibuf, const char *name, int flags)
00337 {
00338         FILE* file = fopen(name, "wb");
00339         float *fp= NULL;
00340         int y, width=ibuf->x, height=ibuf->y;
00341         unsigned char *cp= NULL;
00342         
00343         (void)flags; /* unused */
00344         
00345         if (file==NULL) return 0;
00346 
00347         writeHeader(file, width, height);
00348 
00349         if(ibuf->rect)
00350                 cp= (unsigned char *)ibuf->rect + ibuf->channels*(height-1)*width;
00351         if(ibuf->rect_float)
00352                 fp= ibuf->rect_float + ibuf->channels*(height-1)*width;
00353         
00354         for (y=height-1;y>=0;y--) {
00355                 if (fwritecolrs(file, width, ibuf->channels, cp, fp) < 0) {
00356                         fclose(file);
00357                         printf("HDR write error\n");
00358                         return 0;
00359                 }
00360                 if(cp) cp-= ibuf->channels*width;
00361                 if(fp) fp-= ibuf->channels*width;
00362         }
00363 
00364         fclose(file);
00365         return 1;
00366 }
00367 
00368 #endif /* WITH_HDR */