00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043 #include <stdlib.h>
00044 #include <stdio.h>
00045 #include <math.h>
00046 #include <memory.h>
00047
00048 #include <OSGConfig.h>
00049
00050 #include <iostream>
00051 #include <fstream>
00052
00053 #include <OSGLog.h>
00054 #include <OSGImageFileHandler.h>
00055 #include <OSGPathHandler.h>
00056 #include <OSGFileSystem.h>
00057
00058 #include "OSGHDRImageFileType.h"
00059
00060 OSG_USING_NAMESPACE
00061
00062 #define MINELEN 8 // minimum scanline length for encoding
00063 #define MAXELEN 0x7fff // maximum scanline length for encoding
00064 #define MINRUN 4 // minimum run length
00065 #define RED 0
00066 #define GRN 1
00067 #define BLU 2
00068 #define EXP 3
00069 #define COLXS 128
00070
00071
00072 #define copy_rgbe(c1, c2) (c2[RED]=c1[RED],c2[GRN]=c1[GRN],c2[BLU]=c1[BLU],c2[EXP]=c1[EXP])
00073
00085
00086
00087
00088
00089
00090 static const Char8 *suffixArray[] =
00091 {
00092 "hdr"
00093 };
00094
00095 HDRImageFileType HDRImageFileType::_the( "image/x-hdr",
00096 suffixArray, sizeof(suffixArray),
00097 OSG_READ_SUPPORTED |
00098 OSG_WRITE_SUPPORTED );
00099
00100
00101
00102
00103
00104
00105
00106
00107
00108
00109
00110
00114 HDRImageFileType& HDRImageFileType::the (void)
00115 {
00116 return _the;
00117 }
00118
00119
00120
00121
00122
00123
00128 bool HDRImageFileType::read(ImagePtr &image, std::istream &is, const std::string &mimetype)
00129 {
00130 int width, height;
00131
00132 if (!checkHDR(is, width, height))
00133 {
00134 FWARNING(("No valid RADIANCE picture format\n"));
00135 return false;
00136 }
00137
00138 image->set (Image::OSG_RGB_PF, width, height, 1, 1, 1, 0.0, 0, Image::OSG_FLOAT32_IMAGEDATA);
00139 image->clear();
00140
00141 Real32 *data = ((Real32 *)(image->getData()));
00142
00143 return radiance2fp(is, data, width, height);
00144 }
00145
00146
00151 bool HDRImageFileType::write(const ImagePtr &image, std::ostream &os, const std::string &mimetype)
00152 {
00153 if( ( image->getDataType() != Image::OSG_FLOAT32_IMAGEDATA) &&
00154 ( image->getDataType() != Image::OSG_FLOAT16_IMAGEDATA))
00155 {
00156 FWARNING(("HDRImageFileType::write: Image has non float data type!\n"));
00157 return false;
00158 }
00159
00160 if (!os.good())
00161 return false;
00162
00163 int width = image->getWidth();
00164 int height = image->getHeight();
00165
00166 os << "#?RADIANCE" << std::endl;
00167 os << "# Written with OpenSG" << std::endl;
00168 os << "FORMAT=32-bit_rle_rgbe" << std::endl;
00169 os << "EXPOSURE=" << 1.0f << std::endl << std::endl;
00170 os << "-Y " << height << " +X " << width << std::endl;
00171
00172 RGBE *rgbe_scan = new RGBE[width];
00173
00174 if( image->getDataType() == Image::OSG_FLOAT32_IMAGEDATA)
00175 {
00176 Real32 *data = ((Real32 *)(image->getData()));
00177
00178
00179 for(int y=height-1;y>=0;y--)
00180 {
00181 if (fwritecolrs(os, &data[y * width * 3], rgbe_scan, width, height) < 0)
00182 {
00183 delete [] rgbe_scan;
00184 return false;
00185 }
00186 }
00187 }
00188 else
00189 {
00190 Real16 *data = ((Real16 *)(image->getData()));
00191
00192 for(int y=height-1;y>=0;y--)
00193 {
00194 if (fwritecolrs(os, &data[y * width * 3], rgbe_scan, width, height) < 0)
00195 {
00196 delete [] rgbe_scan;
00197 return false;
00198 }
00199 }
00200 }
00201
00202 delete [] rgbe_scan;
00203 return true;
00204 }
00205
00206
00211 bool HDRImageFileType::read ( ImagePtr &image,
00212 const Char8 *fileName )
00213 {
00214 FILE *file = fopen(fileName, "rb");
00215
00216 if(file == NULL && ImageFileHandler::the().getPathHandler())
00217 {
00218
00219 PathHandler *ph = ImageFileHandler::the().getPathHandler();
00220 file = fopen(ph->findFile(fileName).c_str(), "rb");
00221 }
00222
00223 if(file == NULL)
00224 return false;
00225
00226 int width, height;
00227 if(!checkHDR(file, width, height))
00228 {
00229 fclose(file);
00230 return false;
00231 }
00232
00233 image->set (Image::OSG_RGB_PF, width, height, 1, 1, 1, 0.0, 0, Image::OSG_FLOAT32_IMAGEDATA);
00234 image->clear();
00235
00236 Real32 *data = ((Real32 *)(image->getData()));
00237
00238 bool ok = radiance2fp(file, data, width, height);
00239 fclose(file);
00240 return ok;
00241 }
00242
00243
00248 bool HDRImageFileType::write(const ImagePtr &image,
00249 const Char8 *fileName)
00250 {
00251 if( ( image->getDataType() != Image::OSG_FLOAT32_IMAGEDATA) &&
00252 ( image->getDataType() != Image::OSG_FLOAT16_IMAGEDATA))
00253 {
00254 FWARNING(("HDRImageFileType::write: Image has non float data type!\n"));
00255 return false;
00256 }
00257
00258 FILE *file = fopen(fileName, "wb");
00259
00260 if(file == NULL)
00261 return false;
00262
00263 int width = image->getWidth();
00264 int height = image->getHeight();
00265
00266 fprintf(file, "#?RADIANCE");
00267 fputc(10, file);
00268 fprintf(file, "# %s", "Written with OpenSG");
00269 fputc(10, file);
00270 fprintf(file, "FORMAT=32-bit_rle_rgbe");
00271 fputc(10, file);
00272 fprintf(file, "EXPOSURE=%25.13f", 1.0);
00273 fputc(10, file);
00274 fputc(10, file);
00275 fprintf(file, "-Y %d +X %d", height, width);
00276 fputc(10, file);
00277
00278 RGBE *rgbe_scan = new RGBE[width];
00279
00280 if( image->getDataType() == Image::OSG_FLOAT32_IMAGEDATA)
00281 {
00282 Real32 *data = ((Real32 *)(image->getData()));
00283
00284
00285 for(int y=height-1;y>=0;y--)
00286 {
00287 if (fwritecolrs(file, &data[y * width * 3], rgbe_scan, width, height) < 0)
00288 {
00289 fclose(file);
00290 delete [] rgbe_scan;
00291 return false;
00292 }
00293 }
00294 }
00295 else
00296 {
00297 Real16 *data = ((Real16 *)(image->getData()));
00298
00299
00300 for(int y=height-1;y>=0;y--)
00301 {
00302 if (fwritecolrs(file, &data[y * width * 3], rgbe_scan, width, height) < 0)
00303 {
00304 fclose(file);
00305 delete [] rgbe_scan;
00306 return false;
00307 }
00308 }
00309 }
00310 fclose(file);
00311 delete [] rgbe_scan;
00312 return true;
00313 }
00314
00315
00316
00321 UInt64 HDRImageFileType::restoreData( ImagePtr &image,
00322 const UChar8 *buffer,
00323 Int32 OSG_CHECK_ARG(memSize) )
00324 {
00325 image->setData(buffer);
00326
00327 return image->getSize();
00328 }
00329
00330
00335 UInt64 HDRImageFileType::storeData(const ImagePtr &image,
00336 UChar8 *buffer,
00337 Int32 OSG_CHECK_ARG(memSize))
00338 {
00339 UInt32 dataSize = image->getSize();
00340 const UChar8 *src = image->getData();
00341
00342 if ( dataSize && src && buffer )
00343 memcpy( buffer, src, dataSize);
00344
00345 return dataSize;
00346 }
00347
00348
00349
00353 HDRImageFileType::HDRImageFileType ( const Char8 *mimeType,
00354 const Char8 *suffixArray[],
00355 UInt16 suffixByteCount,
00356 UInt32 flags )
00357 : ImageFileType ( mimeType, suffixArray, suffixByteCount, flags )
00358 {
00359
00360 }
00361
00362
00366 HDRImageFileType::~HDRImageFileType(void)
00367 {
00368 }
00369
00370
00371
00372 bool HDRImageFileType::checkHDR(FILE *file, int &width, int &height)
00373 {
00374 char cs[256], st1[80], st2[80];
00375 bool resok = false;
00376 bool HDRok = false;
00377 while(!feof(file) && !resok)
00378 {
00379 fgets(cs, 255, file);
00380 if(strstr(cs, "32-bit_rle_rgbe"))
00381 HDRok = true;
00382 if(strcmp(cs, "\n") == 0)
00383 {
00384
00385
00386 fgets(cs, 255, file);
00387 sscanf(cs, "%s %d %s %d", (char*)&st1, &height, (char*)&st2, &width);
00388 resok = true;
00389 }
00390 }
00391 return HDRok;
00392 }
00393
00394
00395 bool HDRImageFileType::checkHDR(std::istream &is, int &width, int &height)
00396 {
00397 char cs[256], st1[80], st2[80];
00398 bool resok = false;
00399 bool HDRok = false;
00400 int i = 0;
00401
00402 while (!is.eof() && !resok)
00403 {
00404 is.getline(cs, 255);
00405
00406 if (strstr(cs, "32-bit_rle_rgbe"))
00407 HDRok = true;
00408
00409 if (HDRok && (cs[0] == '\r' || cs[0] == '\n' || cs[0] == '\0'))
00410 {
00411
00412
00413 is.getline(cs, 255);
00414
00415 i = sscanf(cs, "%s %d %s %d", st1, &height, st2, &width);
00416 if (i == 4)
00417 resok = true;
00418 }
00419 }
00420
00421 return HDRok;
00422 }
00423
00424
00425 bool HDRImageFileType::radiance2fp(FILE *file, Real32 *data, int width, int height)
00426 {
00427 int x,y,yx;
00428 RGBE *sline = new RGBE[width];
00429
00430 for(y=height-1;y>=0;y--)
00431 {
00432 yx = y*width;
00433 if (!freadcolrs(file, sline, width))
00434 return false;
00435 Real32 *fcol = &data[yx * 3];
00436 for (x=0;x<width;x++)
00437 {
00438 RGBE2Float(sline[x], fcol);
00439 fcol += 3;
00440 }
00441 }
00442 delete[] sline;
00443 return true;
00444 }
00445
00446
00447 bool HDRImageFileType::radiance2fp(std::istream &is, Real32 *data, int width, int height)
00448 {
00449 int x,y,yx;
00450 RGBE *sline = new RGBE[width];
00451
00452 if (!sline)
00453 return false;
00454
00455 for(y=height-1;y>=0;y--)
00456 {
00457 yx = y*width;
00458 if (!freadcolrs(is, sline, width))
00459 return false;
00460 Real32 *fcol = &data[yx * 3];
00461 for (x=0;x<width;x++)
00462 {
00463 RGBE2Float(sline[x], fcol);
00464 fcol += 3;
00465 }
00466 }
00467 delete[] sline;
00468
00469 return true;
00470 }
00471
00472
00473 bool HDRImageFileType::freadcolrs(FILE *file, RGBE *scan, int width)
00474 {
00475 int i,j,code,val,size;
00476
00477 if((width < MINELEN) | (width > MAXELEN))
00478 return oldreadcolrs(file, scan, width);
00479
00480 if((i = getc(file)) == EOF)
00481 return false;
00482
00483 if(i != 2)
00484 {
00485 ungetc(i, file);
00486 return oldreadcolrs(file, scan, width);
00487 }
00488
00489 scan[0][GRN] = (unsigned char)getc(file);
00490 scan[0][BLU] = (unsigned char)getc(file);
00491
00492 if((i = getc(file)) == EOF)
00493 return false;
00494
00495 size = ((int)scan[0][BLU]) << 8;
00496 if ( (size | i) != width )
00497 return false;
00498
00499 for(i=0;i<4;i++)
00500 {
00501 for (j=0;j<width;)
00502 {
00503 if ((code = getc(file)) == EOF)
00504 return false;
00505 if (code > 128)
00506 {
00507 code &= 127;
00508 val = getc(file);
00509 while (code--)
00510 scan[j++][i] = (unsigned char)val;
00511 }
00512 else
00513 {
00514 while (code--)
00515 scan[j++][i] = (unsigned char)getc(file);
00516 }
00517 }
00518 }
00519 return feof(file) ? false : true;
00520 }
00521
00522
00523 bool HDRImageFileType::freadcolrs(std::istream &is, RGBE *scan, int width)
00524 {
00525 int i,j,code,val,size;
00526 unsigned char byte;
00527
00528 if ((width < MINELEN) | (width > MAXELEN))
00529 {
00530 FWARNING(("Sorry, format probably too old\n"));
00531 return false;
00532 }
00533
00534 byte = (unsigned char)is.get();
00535 if (is.eof())
00536 return false;
00537
00538 byte = (unsigned char)is.get();
00539 scan[0][GRN] = byte;
00540
00541 byte = (unsigned char)is.get();
00542 scan[0][BLU] = byte;
00543
00544 size = ((int)scan[0][BLU]) << 8;
00545 i = is.get();
00546
00547 if ( (size | i) != width )
00548 return false;
00549
00550 for(i=0;i<4;i++)
00551 {
00552 for (j=0;j<width;)
00553 {
00554 if (is.eof())
00555 return false;
00556
00557 code = is.get();
00558
00559 if (code > 128)
00560 {
00561 code &= 127;
00562 val = is.get();
00563
00564 while (code--)
00565 scan[j++][i] = (unsigned char)val;
00566 }
00567 else
00568 {
00569 while (code--)
00570 scan[j++][i] = is.get();
00571 }
00572 }
00573 }
00574
00575 return is.eof() ? false : true;
00576 }
00577
00578
00579 bool HDRImageFileType::oldreadcolrs(FILE *file, RGBE *scan, int width)
00580 {
00581 int i, rshift = 0, len = width;
00582 while (len > 0)
00583 {
00584 scan[0][RED] = (unsigned char)getc(file);
00585 scan[0][GRN] = (unsigned char)getc(file);
00586 scan[0][BLU] = (unsigned char)getc(file);
00587 scan[0][EXP] = (unsigned char)getc(file);
00588 if(feof(file) || ferror(file))
00589 return false;
00590 if(scan[0][RED] == 1 && scan[0][GRN] == 1 && scan[0][BLU] == 1)
00591 {
00592 for (i=scan[0][EXP]<<rshift;i>0;i--)
00593 {
00594 copy_rgbe(scan[-1], scan[0]);
00595 scan++;
00596 len--;
00597 }
00598 rshift += 8;
00599 }
00600 else
00601 {
00602 scan++;
00603 len--;
00604 rshift = 0;
00605 }
00606 }
00607 return true;
00608 }
00609
00610
00611 void HDRImageFileType::RGBE2Float(RGBE rgbe, Real32 *fcol)
00612 {
00613 if (rgbe[EXP] == 0)
00614 {
00615 *(fcol + RED) = *(fcol + GRN) = *(fcol + BLU) = 0;
00616 }
00617 else
00618 {
00619 Real32 f = ldexp(1., rgbe[EXP]-(COLXS+8));
00620 *(fcol + RED) = (rgbe[RED]+.5)*f;
00621 *(fcol + GRN) = (rgbe[GRN]+.5)*f;
00622 *(fcol + BLU) = (rgbe[BLU]+.5)*f;
00623 }
00624 }
00625
00626
00627 void HDRImageFileType::RGBE2Half(RGBE rgbe, Real16 *fcol)
00628 {
00629 if (rgbe[EXP] == 0)
00630 {
00631 *(fcol + RED) = *(fcol + GRN) = *(fcol + BLU) = 0;
00632 }
00633 else
00634 {
00635 Real32 f = ldexp(1., rgbe[EXP]-(COLXS+8));
00636 *(fcol + RED) = Real16( ( rgbe[RED]+.5) * f);
00637 *(fcol + GRN) = Real16( ( rgbe[GRN]+.5) * f);
00638 *(fcol + BLU) = Real16( ( rgbe[BLU]+.5) * f);
00639 }
00640 }
00641
00642 int HDRImageFileType::fwritecolrs(FILE *file, Real32 *scan, RGBE *rgbe_scan,
00643 int width, int height)
00644 {
00645
00646 for (unsigned int i=0; i < width; ++i)
00647 {
00648 float2RGBE(scan, rgbe_scan[i]);
00649 scan += 3;
00650 }
00651
00652 return fwriteRGBE(file, rgbe_scan, width, height);
00653 }
00654
00655 int HDRImageFileType::fwritecolrs(FILE *file, Real16 *scan, RGBE *rgbe_scan,
00656 int width, int height)
00657 {
00658
00659 for (unsigned int i=0; i < width; ++i)
00660 {
00661 half2RGBE(scan, rgbe_scan[i]);
00662 scan += 3;
00663 }
00664
00665 return fwriteRGBE(file, rgbe_scan, width, height);
00666 }
00667
00668 int HDRImageFileType::fwriteRGBE( FILE *file, RGBE *rgbe_scan, int width, int height)
00669 {
00670 int i, j, beg, c2, cnt=0;
00671 if ((width < MINELEN) | (width > MAXELEN))
00672 return (fwrite((char *)rgbe_scan, sizeof(RGBE), width, file) - width);
00673
00674
00675 putc(2, file);
00676 putc(2, file);
00677 putc((unsigned char)(width>>8), file);
00678 putc((unsigned char)(width&255), file);
00679
00680
00681 for (i=0;i<4;i++)
00682 {
00683 for (j=0;j<width;j+=cnt)
00684 {
00685 for (beg=j;beg<width;beg+=cnt)
00686 {
00687 for(cnt=1;(cnt<127) && ((beg+cnt)<width) && (rgbe_scan[beg+cnt][i] == rgbe_scan[beg][i]); cnt++);
00688 if (cnt>=MINRUN)
00689 break;
00690 }
00691 if (((beg-j)>1) && ((beg-j) < MINRUN))
00692 {
00693 c2 = j+1;
00694 while (rgbe_scan[c2++][i] == rgbe_scan[j][i])
00695 if (c2 == beg)
00696 {
00697 putc((unsigned char)(128+beg-j), file);
00698 putc((unsigned char)(rgbe_scan[j][i]), file);
00699 j = beg;
00700 break;
00701 }
00702 }
00703 while (j < beg)
00704 {
00705 if ((c2 = beg-j) > 128)
00706 c2 = 128;
00707 putc((unsigned char)(c2), file);
00708 while (c2--)
00709 putc(rgbe_scan[j++][i], file);
00710 }
00711 if (cnt >= MINRUN)
00712 {
00713 putc((unsigned char)(128+cnt), file);
00714 putc(rgbe_scan[beg][i], file);
00715 }
00716 else
00717 {
00718 cnt = 0;
00719 }
00720 }
00721 }
00722 return(ferror(file) ? -1 : 0);
00723 }
00724
00725 int HDRImageFileType::fwritecolrs(std::ostream &os, Real32 *scan, RGBE *rgbe_scan,
00726 int width, int height)
00727 {
00728
00729 for (unsigned int i=0;i<width;i++)
00730 {
00731 float2RGBE(scan, rgbe_scan[i]);
00732 scan += 3;
00733 }
00734
00735 return fwriteRGBE(os, rgbe_scan, width, height);
00736 }
00737
00738 int HDRImageFileType::fwritecolrs(std::ostream &os, Real16 *scan, RGBE *rgbe_scan,
00739 int width, int height)
00740 {
00741
00742 for (unsigned int i=0;i<width;i++)
00743 {
00744 half2RGBE(scan, rgbe_scan[i]);
00745 scan += 3;
00746 }
00747
00748 return fwriteRGBE(os, rgbe_scan, width, height);
00749 }
00750
00751
00752 int HDRImageFileType::fwriteRGBE(std::ostream &os, RGBE *rgbe_scan,
00753 int width, int height)
00754 {
00755 int i, j, beg, c2, cnt=0;
00756
00757 if ((width < MINELEN) | (width > MAXELEN))
00758 {
00759
00760 os.write((char *)rgbe_scan, width);
00761 return 0;
00762 }
00763
00764
00765 os << (unsigned char)2;
00766 os << (unsigned char)2;
00767 os << (unsigned char)(width>>8);
00768 os << (unsigned char)(width&255);
00769
00770
00771 for (i=0;i<4;i++)
00772 {
00773 for (j=0;j<width;j+=cnt)
00774 {
00775
00776 for (beg=j;beg<width;beg+=cnt)
00777 {
00778 for(cnt=1; (cnt<127)&&((beg+cnt)<width)&&(rgbe_scan[beg+cnt][i]==rgbe_scan[beg][i]); cnt++)
00779 ;
00780 if (cnt>=MINRUN)
00781 break;
00782
00783 }
00784 if (((beg-j)>1) && ((beg-j) < MINRUN))
00785 {
00786 c2 = j+1;
00787 while (rgbe_scan[c2++][i] == rgbe_scan[j][i])
00788 {
00789 if (c2 == beg)
00790 {
00791
00792 os << (unsigned char)(128+beg-j);
00793 os << (unsigned char)(rgbe_scan[j][i]);
00794 j = beg;
00795 break;
00796 }
00797 }
00798 }
00799 while (j < beg)
00800 {
00801
00802 if ((c2 = beg-j) > 128)
00803 c2 = 128;
00804 os << (unsigned char)(c2);
00805
00806 while (c2--)
00807 os << rgbe_scan[j++][i];
00808 }
00809 if (cnt >= MINRUN)
00810 {
00811
00812 os << (unsigned char)(128+cnt);
00813 os << rgbe_scan[beg][i];
00814 }
00815 else
00816 {
00817 cnt = 0;
00818 }
00819 }
00820 }
00821 return (os.fail() ? -1 : 0);
00822 }
00823
00824
00825 void HDRImageFileType::float2RGBE(Real32 *fcol, RGBE rgbe)
00826 {
00827 Real32 d = (*(fcol + RED) > *(fcol + GRN)) ? *(fcol + RED) : *(fcol + GRN);
00828
00829 if(*(fcol + BLU) > d)
00830 d = *(fcol + BLU);
00831 if(d <= 1e-32f)
00832 {
00833 rgbe[RED] = rgbe[GRN] = rgbe[BLU] = rgbe[EXP] = 0;
00834 }
00835 else
00836 {
00837 int e;
00838 d = frexp(d, &e) * 256.f / d;
00839 rgbe[RED] = (unsigned char)(*(fcol + RED) * d);
00840 rgbe[GRN] = (unsigned char)(*(fcol + GRN) * d);
00841 rgbe[BLU] = (unsigned char)(*(fcol + BLU) * d);
00842 rgbe[EXP] = (unsigned char)(e + COLXS);
00843 }
00844 }
00845
00846
00847 void HDRImageFileType::half2RGBE(Real16 *fcol, RGBE rgbe)
00848 {
00849 Real32 d = (*(fcol + RED) > *(fcol + GRN)) ? *(fcol + RED) : *(fcol + GRN);
00850
00851 if(*(fcol + BLU) > d)
00852 d = *(fcol + BLU);
00853 if(d <= 1e-32f)
00854 {
00855 rgbe[RED] = rgbe[GRN] = rgbe[BLU] = rgbe[EXP] = 0;
00856 }
00857 else
00858 {
00859 int e;
00860 d = frexp(d, &e) * 256.f / d;
00861 rgbe[RED] = (unsigned char)(*(fcol + RED) * d);
00862 rgbe[GRN] = (unsigned char)(*(fcol + GRN) * d);
00863 rgbe[BLU] = (unsigned char)(*(fcol + BLU) * d);
00864 rgbe[EXP] = (unsigned char)(e + COLXS);
00865 }
00866 }