2005-11-20 14:32:07 +00:00
|
|
|
/*
|
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
|
* as published by the Free Software Foundation; either version 2
|
|
|
|
|
* of the License, or (at your option) any later version.
|
|
|
|
|
*
|
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
|
*
|
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
|
* along with this program; if not, write to the Free Software Foundation,
|
2010-02-12 13:34:04 +00:00
|
|
|
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
2005-11-20 14:32:07 +00:00
|
|
|
*
|
|
|
|
|
* The Original Code is Copyright
|
|
|
|
|
* All rights reserved.
|
2011-10-23 17:52:20 +00:00
|
|
|
*/
|
2005-11-20 14:32:07 +00:00
|
|
|
|
2019-02-18 08:08:12 +11:00
|
|
|
/** \file
|
|
|
|
|
* \ingroup imbuf
|
2011-02-27 20:23:21 +00:00
|
|
|
*/
|
|
|
|
|
|
Orange branch: OpenEXR finally in Blender!
Credits go to Gernot Ziegler, who originally coded EXR support, and to
Austin Benesh for bringing it further. Kent Mein provided a lot of code
for integrating float buffers in Blender imbuf and ImBuf API cleanup,
and provided Make and Scons and static linking.
At this moment; the EXR libraries are a *dependency*, so you cannot get
the Orange branch compiled without having OpenEXR installed. Get the
(precompiled or sources) stuff from www.openexr.com. Current default is
that the headers and lib resides in /user/local/
Several changes/additions/fixes were added:
- EXR code only supported 'half' format (16 bits per channel). I've added
float writing, but for reading it I need tomorrow. :)
- Quite some clumsy copying of data happened in EXR code.
- cleaned up the api calls already a bit, preparing for more advanced
support
- Zbuffers were saved 16 bits, now 32 bits
- automatic adding of .exr extensions went wrong
Imbuf:
- added proper imbuf->flags and imbuf->mall support for float buffers, it
was created for *each* imbuf. :)
- found bugs for float buffers in scaling and flipping. Code there will
need more checks still
- imbuf also needs to be verified to behave properly when no 32 bits
rect exists (for saving for example)
TODO:
- support internal float images for textures, backbuf, AO probes, and
display in Image window
Hope this commit won't screwup syncing with bf-blender... :/
2006-01-09 00:40:35 +00:00
|
|
|
/* ----------------------------------------------------------------------
|
2012-03-09 18:28:30 +00:00
|
|
|
* Radiance High Dynamic Range image file IO
|
|
|
|
|
* For description and code for reading/writing of radiance hdr files
|
|
|
|
|
* by Greg Ward, refer to:
|
|
|
|
|
* http://radsite.lbl.gov/radiance/refer/Notes/picture_format.html
|
|
|
|
|
* ----------------------------------------------------------------------
|
|
|
|
|
*/
|
2005-11-20 14:32:07 +00:00
|
|
|
|
2010-08-18 10:42:00 +00:00
|
|
|
#include "MEM_guardedalloc.h"
|
|
|
|
|
|
2012-03-31 04:04:58 +00:00
|
|
|
#include "BLI_fileops.h"
|
2015-10-20 02:12:41 +11:00
|
|
|
#include "BLI_utildefines.h"
|
2005-11-20 14:32:07 +00:00
|
|
|
|
|
|
|
|
#include "imbuf.h"
|
|
|
|
|
|
|
|
|
|
#include "IMB_imbuf.h"
|
2020-03-19 09:33:03 +01:00
|
|
|
#include "IMB_imbuf_types.h"
|
2005-11-20 14:32:07 +00:00
|
|
|
|
|
|
|
|
#include "IMB_allocimbuf.h"
|
Merge image related changes from the render branch. This includes the image
tile cache code in imbuf, but it is not hooked up to the render engine.
Imbuf module: some small refactoring and removing a lot of unused or old code
(about 6.5k lines).
* Added a ImFileType struct with callbacks to make adding an file format type,
or making changes to the API easier.
* Move imbuf init/exit code into IMB_init()/IMB_exit() functions.
* Increased mipmap levels from 10 to 20, you run into this limit already with
a 2k image.
* Removed hamx, amiga, anim5 format support.
* Removed colormap saving, only simple colormap code now for reading tga.
* Removed gen_dynlibtiff.py, editing this is almost as much work as just
editing the code directly.
* Functions removed that were only used for sequencer plugin API:
IMB_anim_nextpic, IMB_clever_double, IMB_antialias, IMB_gamwarp,
IMB_scalefieldImBuf, IMB_scalefastfieldImBuf, IMB_onethird, IMB_halflace,
IMB_dit0, IMB_dit2, IMB_cspace
* Write metadata info into OpenEXR images. Can be viewed with the command
line utility 'exrheader'
For the image tile cache code, see this page:
http://wiki.blender.org/index.php/Dev:2.5/Source/Imaging/ImageTileCache
2010-05-07 15:18:04 +00:00
|
|
|
#include "IMB_filetype.h"
|
2005-11-20 14:32:07 +00:00
|
|
|
|
Color Management, Stage 2: Switch color pipeline to use OpenColorIO
Replace old color pipeline which was supporting linear/sRGB color spaces
only with OpenColorIO-based pipeline.
This introduces two configurable color spaces:
- Input color space for images and movie clips. This space is used to convert
images/movies from color space in which file is saved to Blender's linear
space (for float images, byte images are not internally converted, only input
space is stored for such images and used later).
This setting could be found in image/clip data block settings.
- Display color space which defines space in which particular display is working.
This settings could be found in scene's Color Management panel.
When render result is being displayed on the screen, apart from converting image
to display space, some additional conversions could happen.
This conversions are:
- View, which defines tone curve applying before display transformation.
These are different ways to view the image on the same display device.
For example it could be used to emulate film view on sRGB display.
- Exposure affects on image exposure before tone map is applied.
- Gamma is post-display gamma correction, could be used to match particular
display gamma.
- RGB curves are user-defined curves which are applying before display
transformation, could be used for different purposes.
All this settings by default are only applying on render result and does not
affect on other images. If some particular image needs to be affected by this
transformation, "View as Render" setting of image data block should be set to
truth. Movie clips are always affected by all display transformations.
This commit also introduces configurable color space in which sequencer is
working. This setting could be found in scene's Color Management panel and
it should be used if such stuff as grading needs to be done in color space
different from sRGB (i.e. when Film view on sRGB display is use, using VD16
space as sequencer's internal space would make grading working in space
which is close to the space using for display).
Some technical notes:
- Image buffer's float buffer is now always in linear space, even if it was
created from 16bit byte images.
- Space of byte buffer is stored in image buffer's rect_colorspace property.
- Profile of image buffer was removed since it's not longer meaningful.
- OpenGL and GLSL is supposed to always work in sRGB space. It is possible
to support other spaces, but it's quite large project which isn't so
much important.
- Legacy Color Management option disabled is emulated by using None display.
It could have some regressions, but there's no clear way to avoid them.
- If OpenColorIO is disabled on build time, it should make blender behaving
in the same way as previous release with color management enabled.
More details could be found at this page (more details would be added soon):
http://wiki.blender.org/index.php/Dev:Ref/Release_Notes/2.64/Color_Management
--
Thanks to Xavier Thomas, Lukas Toene for initial work on OpenColorIO
integration and to Brecht van Lommel for some further development and code/
usecase review!
2012-09-15 10:05:07 +00:00
|
|
|
#include "IMB_colormanagement.h"
|
|
|
|
|
#include "IMB_colormanagement_intern.h"
|
|
|
|
|
|
2005-11-20 14:32:07 +00:00
|
|
|
/* needed constants */
|
|
|
|
|
#define MINELEN 8
|
|
|
|
|
#define MAXELEN 0x7fff
|
2012-05-16 09:26:37 +00:00
|
|
|
#define MINRUN 4 /* minimum run length */
|
2005-11-20 14:32:07 +00:00
|
|
|
#define RED 0
|
|
|
|
|
#define GRN 1
|
|
|
|
|
#define BLU 2
|
|
|
|
|
#define EXP 3
|
|
|
|
|
#define COLXS 128
|
|
|
|
|
typedef unsigned char RGBE[4];
|
|
|
|
|
typedef float fCOLOR[3];
|
2012-05-23 22:45:39 +00:00
|
|
|
|
2005-11-20 14:32:07 +00:00
|
|
|
/* copy source -> dest */
|
2012-05-23 22:45:39 +00:00
|
|
|
#define COPY_RGBE(c1, c2) \
|
|
|
|
|
(c2[RED] = c1[RED], c2[GRN] = c1[GRN], c2[BLU] = c1[BLU], c2[EXP] = c1[EXP])
|
2005-11-20 14:32:07 +00:00
|
|
|
|
|
|
|
|
/* read routines */
|
2015-07-11 23:52:18 +10:00
|
|
|
static const unsigned char *oldreadcolrs(RGBE *scan,
|
|
|
|
|
const unsigned char *mem,
|
|
|
|
|
int xmax,
|
|
|
|
|
const unsigned char *mem_eof)
|
2005-11-20 14:32:07 +00:00
|
|
|
{
|
2018-01-14 14:19:57 +01:00
|
|
|
size_t i, rshift = 0, len = xmax;
|
2005-11-20 14:32:07 +00:00
|
|
|
while (len > 0) {
|
2015-10-20 02:12:41 +11:00
|
|
|
if (UNLIKELY(mem_eof - mem < 4)) {
|
2014-11-21 12:26:13 +01:00
|
|
|
return NULL;
|
|
|
|
|
}
|
2005-11-20 14:32:07 +00:00
|
|
|
scan[0][RED] = *mem++;
|
|
|
|
|
scan[0][GRN] = *mem++;
|
|
|
|
|
scan[0][BLU] = *mem++;
|
|
|
|
|
scan[0][EXP] = *mem++;
|
|
|
|
|
if (scan[0][RED] == 1 && scan[0][GRN] == 1 && scan[0][BLU] == 1) {
|
2012-05-16 09:26:37 +00:00
|
|
|
for (i = scan[0][EXP] << rshift; i > 0; i--) {
|
2012-05-23 22:45:39 +00:00
|
|
|
COPY_RGBE(scan[-1], scan[0]);
|
2005-11-20 14:32:07 +00:00
|
|
|
scan++;
|
|
|
|
|
len--;
|
|
|
|
|
}
|
|
|
|
|
rshift += 8;
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
scan++;
|
|
|
|
|
len--;
|
|
|
|
|
rshift = 0;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return mem;
|
|
|
|
|
}
|
|
|
|
|
|
2015-07-11 23:52:18 +10:00
|
|
|
static const unsigned char *freadcolrs(RGBE *scan,
|
|
|
|
|
const unsigned char *mem,
|
|
|
|
|
int xmax,
|
|
|
|
|
const unsigned char *mem_eof)
|
2005-11-20 14:32:07 +00:00
|
|
|
{
|
2015-10-20 02:12:41 +11:00
|
|
|
if (UNLIKELY(mem_eof - mem < 4)) {
|
2014-11-21 12:26:13 +01:00
|
|
|
return NULL;
|
|
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2015-10-20 02:12:41 +11:00
|
|
|
if (UNLIKELY((xmax < MINELEN) | (xmax > MAXELEN))) {
|
2014-11-21 12:26:13 +01:00
|
|
|
return oldreadcolrs(scan, mem, xmax, mem_eof);
|
|
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2018-01-14 14:19:57 +01:00
|
|
|
int val = *mem++;
|
|
|
|
|
if (val != 2) {
|
2014-11-21 12:26:13 +01:00
|
|
|
return oldreadcolrs(scan, mem - 1, xmax, mem_eof);
|
|
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2005-11-20 14:32:07 +00:00
|
|
|
scan[0][GRN] = *mem++;
|
|
|
|
|
scan[0][BLU] = *mem++;
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2018-01-14 14:19:57 +01:00
|
|
|
val = *mem++;
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2014-11-21 12:26:13 +01:00
|
|
|
if (scan[0][GRN] != 2 || scan[0][BLU] & 128) {
|
|
|
|
|
scan[0][RED] = 2;
|
2018-01-14 14:19:57 +01:00
|
|
|
scan[0][EXP] = val;
|
2014-11-21 12:26:13 +01:00
|
|
|
return oldreadcolrs(scan + 1, mem, xmax - 1, mem_eof);
|
|
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2018-01-14 14:19:57 +01:00
|
|
|
if (UNLIKELY(((scan[0][BLU] << 8) | val) != xmax)) {
|
2014-11-21 12:26:13 +01:00
|
|
|
return NULL;
|
|
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2018-01-14 14:19:57 +01:00
|
|
|
for (size_t i = 0; i < 4; i++) {
|
2015-10-20 02:12:41 +11:00
|
|
|
if (UNLIKELY(mem_eof - mem < 2)) {
|
2014-11-21 12:26:13 +01:00
|
|
|
return NULL;
|
|
|
|
|
}
|
2018-01-14 14:19:57 +01:00
|
|
|
for (size_t j = 0; j < xmax;) {
|
|
|
|
|
int code = *mem++;
|
2005-11-20 14:32:07 +00:00
|
|
|
if (code > 128) {
|
|
|
|
|
code &= 127;
|
2015-10-20 02:05:52 +11:00
|
|
|
if (UNLIKELY(code + j > xmax)) {
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
2018-01-17 21:11:59 +01:00
|
|
|
val = *mem++;
|
2014-11-21 12:26:13 +01:00
|
|
|
while (code--) {
|
2005-11-20 14:32:07 +00:00
|
|
|
scan[j++][i] = (unsigned char)val;
|
2014-11-21 12:26:13 +01:00
|
|
|
}
|
2005-11-20 14:32:07 +00:00
|
|
|
}
|
2014-11-21 12:26:13 +01:00
|
|
|
else {
|
2015-10-20 02:12:41 +11:00
|
|
|
if (UNLIKELY(mem_eof - mem < code)) {
|
2014-11-21 12:26:13 +01:00
|
|
|
return NULL;
|
|
|
|
|
}
|
2015-10-20 02:05:52 +11:00
|
|
|
if (UNLIKELY(code + j > xmax)) {
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
2014-11-21 12:26:13 +01:00
|
|
|
while (code--) {
|
2005-11-20 14:32:07 +00:00
|
|
|
scan[j++][i] = *mem++;
|
2014-11-21 12:26:13 +01:00
|
|
|
}
|
|
|
|
|
}
|
2005-11-20 14:32:07 +00:00
|
|
|
}
|
2014-11-21 12:26:13 +01:00
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2005-11-20 14:32:07 +00:00
|
|
|
return mem;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* helper functions */
|
|
|
|
|
|
|
|
|
|
/* rgbe -> float color */
|
|
|
|
|
static void RGBE2FLOAT(RGBE rgbe, fCOLOR fcol)
|
|
|
|
|
{
|
2012-05-16 09:26:37 +00:00
|
|
|
if (rgbe[EXP] == 0) {
|
2005-11-20 14:32:07 +00:00
|
|
|
fcol[RED] = fcol[GRN] = fcol[BLU] = 0;
|
|
|
|
|
}
|
|
|
|
|
else {
|
2012-05-16 09:26:37 +00:00
|
|
|
float f = ldexp(1.0, rgbe[EXP] - (COLXS + 8));
|
|
|
|
|
fcol[RED] = f * (rgbe[RED] + 0.5f);
|
|
|
|
|
fcol[GRN] = f * (rgbe[GRN] + 0.5f);
|
|
|
|
|
fcol[BLU] = f * (rgbe[BLU] + 0.5f);
|
2005-11-20 14:32:07 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* float color -> rgbe */
|
2020-07-13 11:27:09 +02:00
|
|
|
static void FLOAT2RGBE(const fCOLOR fcol, RGBE rgbe)
|
2005-11-20 14:32:07 +00:00
|
|
|
{
|
|
|
|
|
int e;
|
2012-05-16 09:26:37 +00:00
|
|
|
float d = (fcol[RED] > fcol[GRN]) ? fcol[RED] : fcol[GRN];
|
2019-04-23 11:01:30 +10:00
|
|
|
if (fcol[BLU] > d) {
|
2012-05-16 09:26:37 +00:00
|
|
|
d = fcol[BLU];
|
2019-04-23 11:01:30 +10:00
|
|
|
}
|
|
|
|
|
if (d <= 1e-32f) {
|
2005-11-20 14:32:07 +00:00
|
|
|
rgbe[RED] = rgbe[GRN] = rgbe[BLU] = rgbe[EXP] = 0;
|
2019-04-23 11:01:30 +10:00
|
|
|
}
|
2005-11-20 14:32:07 +00:00
|
|
|
else {
|
2012-11-04 10:20:16 +00:00
|
|
|
d = (float)frexp(d, &e) * 256.0f / d;
|
2005-11-20 14:32:07 +00:00
|
|
|
rgbe[RED] = (unsigned char)(fcol[RED] * d);
|
|
|
|
|
rgbe[GRN] = (unsigned char)(fcol[GRN] * d);
|
|
|
|
|
rgbe[BLU] = (unsigned char)(fcol[BLU] * d);
|
|
|
|
|
rgbe[EXP] = (unsigned char)(e + COLXS);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* ImBuf read */
|
|
|
|
|
|
2020-11-11 16:14:09 +11:00
|
|
|
bool imb_is_a_hdr(const unsigned char *buf, const size_t size)
|
2005-11-20 14:32:07 +00:00
|
|
|
{
|
2020-11-11 14:46:32 +11:00
|
|
|
/* NOTE: `#?RADIANCE` is used by other programs such as `ImageMagik`,
|
|
|
|
|
* Although there are some files in the wild that only use `#?` (from looking online).
|
|
|
|
|
* If this is ever a problem we could check for the longer header since this is part of the spec.
|
|
|
|
|
*
|
|
|
|
|
* We could check `32-bit_rle_rgbe` or `32-bit_rle_xyze` too since this is part of the format.
|
|
|
|
|
* Currently this isn't needed.
|
|
|
|
|
*
|
|
|
|
|
* See: http://paulbourke.net/dataformats/pic/
|
|
|
|
|
*/
|
|
|
|
|
const unsigned char magic[2] = {'#', '?'};
|
2020-11-11 16:14:09 +11:00
|
|
|
if (size < sizeof(magic)) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2020-11-11 14:46:32 +11:00
|
|
|
return memcmp(buf, magic, sizeof(magic)) == 0;
|
2005-11-20 14:32:07 +00:00
|
|
|
}
|
|
|
|
|
|
2015-07-11 23:52:18 +10:00
|
|
|
struct ImBuf *imb_loadhdr(const unsigned char *mem,
|
|
|
|
|
size_t size,
|
|
|
|
|
int flags,
|
|
|
|
|
char colorspace[IM_MAX_SPACE])
|
2005-11-20 14:32:07 +00:00
|
|
|
{
|
2012-05-16 09:26:37 +00:00
|
|
|
struct ImBuf *ibuf;
|
|
|
|
|
RGBE *sline;
|
Orange branch: OpenEXR finally in Blender!
Credits go to Gernot Ziegler, who originally coded EXR support, and to
Austin Benesh for bringing it further. Kent Mein provided a lot of code
for integrating float buffers in Blender imbuf and ImBuf API cleanup,
and provided Make and Scons and static linking.
At this moment; the EXR libraries are a *dependency*, so you cannot get
the Orange branch compiled without having OpenEXR installed. Get the
(precompiled or sources) stuff from www.openexr.com. Current default is
that the headers and lib resides in /user/local/
Several changes/additions/fixes were added:
- EXR code only supported 'half' format (16 bits per channel). I've added
float writing, but for reading it I need tomorrow. :)
- Quite some clumsy copying of data happened in EXR code.
- cleaned up the api calls already a bit, preparing for more advanced
support
- Zbuffers were saved 16 bits, now 32 bits
- automatic adding of .exr extensions went wrong
Imbuf:
- added proper imbuf->flags and imbuf->mall support for float buffers, it
was created for *each* imbuf. :)
- found bugs for float buffers in scaling and flipping. Code there will
need more checks still
- imbuf also needs to be verified to behave properly when no 32 bits
rect exists (for saving for example)
TODO:
- support internal float images for textures, backbuf, AO probes, and
display in Image window
Hope this commit won't screwup syncing with bf-blender... :/
2006-01-09 00:40:35 +00:00
|
|
|
fCOLOR fcol;
|
2012-05-16 09:26:37 +00:00
|
|
|
float *rect_float;
|
|
|
|
|
int found = 0;
|
|
|
|
|
int width = 0, height = 0;
|
2015-07-11 23:52:18 +10:00
|
|
|
const unsigned char *ptr, *mem_eof = mem + size;
|
Orange branch: OpenEXR finally in Blender!
Credits go to Gernot Ziegler, who originally coded EXR support, and to
Austin Benesh for bringing it further. Kent Mein provided a lot of code
for integrating float buffers in Blender imbuf and ImBuf API cleanup,
and provided Make and Scons and static linking.
At this moment; the EXR libraries are a *dependency*, so you cannot get
the Orange branch compiled without having OpenEXR installed. Get the
(precompiled or sources) stuff from www.openexr.com. Current default is
that the headers and lib resides in /user/local/
Several changes/additions/fixes were added:
- EXR code only supported 'half' format (16 bits per channel). I've added
float writing, but for reading it I need tomorrow. :)
- Quite some clumsy copying of data happened in EXR code.
- cleaned up the api calls already a bit, preparing for more advanced
support
- Zbuffers were saved 16 bits, now 32 bits
- automatic adding of .exr extensions went wrong
Imbuf:
- added proper imbuf->flags and imbuf->mall support for float buffers, it
was created for *each* imbuf. :)
- found bugs for float buffers in scaling and flipping. Code there will
need more checks still
- imbuf also needs to be verified to behave properly when no 32 bits
rect exists (for saving for example)
TODO:
- support internal float images for textures, backbuf, AO probes, and
display in Image window
Hope this commit won't screwup syncing with bf-blender... :/
2006-01-09 00:40:35 +00:00
|
|
|
char oriY[80], oriX[80];
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2021-03-31 17:05:57 +11:00
|
|
|
if (!imb_is_a_hdr(mem, size)) {
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
colorspace_set_default_role(colorspace, IM_MAX_SPACE, COLOR_ROLE_DEFAULT_FLOAT);
|
|
|
|
|
|
|
|
|
|
/* find empty line, next line is resolution info */
|
|
|
|
|
size_t x;
|
|
|
|
|
for (x = 1; x < size; x++) {
|
|
|
|
|
if ((mem[x - 1] == '\n') && (mem[x] == '\n')) {
|
|
|
|
|
found = 1;
|
|
|
|
|
break;
|
2005-11-20 14:32:07 +00:00
|
|
|
}
|
2021-03-31 17:05:57 +11:00
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2021-03-31 17:05:57 +11:00
|
|
|
if ((found && (x < (size + 2))) == 0) {
|
|
|
|
|
/* Data not found! */
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2021-03-31 17:05:57 +11:00
|
|
|
if (sscanf((const char *)&mem[x + 1],
|
|
|
|
|
"%79s %d %79s %d",
|
|
|
|
|
(char *)&oriY,
|
|
|
|
|
&height,
|
|
|
|
|
(char *)&oriX,
|
|
|
|
|
&width) != 4) {
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2021-03-31 17:05:57 +11:00
|
|
|
/* find end of this line, data right behind it */
|
|
|
|
|
ptr = (const unsigned char *)strchr((const char *)&mem[x + 1], '\n');
|
|
|
|
|
ptr++;
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2021-03-31 17:05:57 +11:00
|
|
|
if (flags & IB_test) {
|
|
|
|
|
ibuf = IMB_allocImBuf(width, height, 32, 0);
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
ibuf = IMB_allocImBuf(width, height, 32, (flags & IB_rect) | IB_rectfloat);
|
|
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2021-03-31 17:05:57 +11:00
|
|
|
if (UNLIKELY(ibuf == NULL)) {
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2021-03-31 17:05:57 +11:00
|
|
|
ibuf->ftype = IMB_FTYPE_RADHDR;
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2021-03-31 17:05:57 +11:00
|
|
|
if (flags & IB_alphamode_detect) {
|
|
|
|
|
ibuf->flags |= IB_alphamode_premul;
|
|
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2021-03-31 17:05:57 +11:00
|
|
|
if (flags & IB_test) {
|
|
|
|
|
return ibuf;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* read in and decode the actual data */
|
|
|
|
|
sline = (RGBE *)MEM_mallocN(sizeof(*sline) * width, __func__);
|
|
|
|
|
rect_float = ibuf->rect_float;
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2021-03-31 17:05:57 +11:00
|
|
|
for (size_t y = 0; y < height; y++) {
|
|
|
|
|
ptr = freadcolrs(sline, ptr, width, mem_eof);
|
|
|
|
|
if (ptr == NULL) {
|
|
|
|
|
printf("WARNING! HDR decode error, image may be just truncated, or completely wrong...\n");
|
|
|
|
|
break;
|
2005-11-20 14:32:07 +00:00
|
|
|
}
|
2021-03-31 17:05:57 +11:00
|
|
|
for (x = 0; x < width; x++) {
|
|
|
|
|
/* convert to ldr */
|
|
|
|
|
RGBE2FLOAT(sline[x], fcol);
|
|
|
|
|
*rect_float++ = fcol[RED];
|
|
|
|
|
*rect_float++ = fcol[GRN];
|
|
|
|
|
*rect_float++ = fcol[BLU];
|
|
|
|
|
*rect_float++ = 1.0f;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
MEM_freeN(sline);
|
|
|
|
|
if (oriY[0] == '-') {
|
|
|
|
|
IMB_flipy(ibuf);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (flags & IB_rect) {
|
|
|
|
|
IMB_rect_from_float(ibuf);
|
2005-11-20 14:32:07 +00:00
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2021-03-31 17:05:57 +11:00
|
|
|
return ibuf;
|
2005-11-20 14:32:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* ImBuf write */
|
2020-07-13 11:27:09 +02:00
|
|
|
static int fwritecolrs(
|
|
|
|
|
FILE *file, int width, int channels, const unsigned char *ibufscan, const float *fpscan)
|
2005-11-20 14:32:07 +00:00
|
|
|
{
|
2018-01-14 14:19:57 +01:00
|
|
|
int beg, c2, cnt = 0;
|
2005-11-20 14:32:07 +00:00
|
|
|
fCOLOR fcol;
|
Orange branch: OpenEXR finally in Blender!
Credits go to Gernot Ziegler, who originally coded EXR support, and to
Austin Benesh for bringing it further. Kent Mein provided a lot of code
for integrating float buffers in Blender imbuf and ImBuf API cleanup,
and provided Make and Scons and static linking.
At this moment; the EXR libraries are a *dependency*, so you cannot get
the Orange branch compiled without having OpenEXR installed. Get the
(precompiled or sources) stuff from www.openexr.com. Current default is
that the headers and lib resides in /user/local/
Several changes/additions/fixes were added:
- EXR code only supported 'half' format (16 bits per channel). I've added
float writing, but for reading it I need tomorrow. :)
- Quite some clumsy copying of data happened in EXR code.
- cleaned up the api calls already a bit, preparing for more advanced
support
- Zbuffers were saved 16 bits, now 32 bits
- automatic adding of .exr extensions went wrong
Imbuf:
- added proper imbuf->flags and imbuf->mall support for float buffers, it
was created for *each* imbuf. :)
- found bugs for float buffers in scaling and flipping. Code there will
need more checks still
- imbuf also needs to be verified to behave properly when no 32 bits
rect exists (for saving for example)
TODO:
- support internal float images for textures, backbuf, AO probes, and
display in Image window
Hope this commit won't screwup syncing with bf-blender... :/
2006-01-09 00:40:35 +00:00
|
|
|
RGBE rgbe, *rgbe_scan;
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2015-10-20 02:12:41 +11:00
|
|
|
if (UNLIKELY((ibufscan == NULL) && (fpscan == NULL))) {
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2012-05-16 09:26:37 +00:00
|
|
|
rgbe_scan = (RGBE *)MEM_mallocN(sizeof(RGBE) * width, "radhdr_write_tmpscan");
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2005-11-20 14:32:07 +00:00
|
|
|
/* convert scanline */
|
2018-01-17 21:11:59 +01:00
|
|
|
for (size_t i = 0, j = 0; i < width; i++) {
|
Orange branch: OpenEXR finally in Blender!
Credits go to Gernot Ziegler, who originally coded EXR support, and to
Austin Benesh for bringing it further. Kent Mein provided a lot of code
for integrating float buffers in Blender imbuf and ImBuf API cleanup,
and provided Make and Scons and static linking.
At this moment; the EXR libraries are a *dependency*, so you cannot get
the Orange branch compiled without having OpenEXR installed. Get the
(precompiled or sources) stuff from www.openexr.com. Current default is
that the headers and lib resides in /user/local/
Several changes/additions/fixes were added:
- EXR code only supported 'half' format (16 bits per channel). I've added
float writing, but for reading it I need tomorrow. :)
- Quite some clumsy copying of data happened in EXR code.
- cleaned up the api calls already a bit, preparing for more advanced
support
- Zbuffers were saved 16 bits, now 32 bits
- automatic adding of .exr extensions went wrong
Imbuf:
- added proper imbuf->flags and imbuf->mall support for float buffers, it
was created for *each* imbuf. :)
- found bugs for float buffers in scaling and flipping. Code there will
need more checks still
- imbuf also needs to be verified to behave properly when no 32 bits
rect exists (for saving for example)
TODO:
- support internal float images for textures, backbuf, AO probes, and
display in Image window
Hope this commit won't screwup syncing with bf-blender... :/
2006-01-09 00:40:35 +00:00
|
|
|
if (fpscan) {
|
|
|
|
|
fcol[RED] = fpscan[j];
|
2012-05-16 09:26:37 +00:00
|
|
|
fcol[GRN] = (channels >= 2) ? fpscan[j + 1] : fpscan[j];
|
|
|
|
|
fcol[BLU] = (channels >= 3) ? fpscan[j + 2] : fpscan[j];
|
2012-03-24 06:38:07 +00:00
|
|
|
}
|
|
|
|
|
else {
|
2020-11-06 11:25:27 +11:00
|
|
|
fcol[RED] = (float)ibufscan[j] / 255.0f;
|
|
|
|
|
fcol[GRN] = (float)((channels >= 2) ? ibufscan[j + 1] : ibufscan[j]) / 255.0f;
|
|
|
|
|
fcol[BLU] = (float)((channels >= 3) ? ibufscan[j + 2] : ibufscan[j]) / 255.0f;
|
Orange branch: OpenEXR finally in Blender!
Credits go to Gernot Ziegler, who originally coded EXR support, and to
Austin Benesh for bringing it further. Kent Mein provided a lot of code
for integrating float buffers in Blender imbuf and ImBuf API cleanup,
and provided Make and Scons and static linking.
At this moment; the EXR libraries are a *dependency*, so you cannot get
the Orange branch compiled without having OpenEXR installed. Get the
(precompiled or sources) stuff from www.openexr.com. Current default is
that the headers and lib resides in /user/local/
Several changes/additions/fixes were added:
- EXR code only supported 'half' format (16 bits per channel). I've added
float writing, but for reading it I need tomorrow. :)
- Quite some clumsy copying of data happened in EXR code.
- cleaned up the api calls already a bit, preparing for more advanced
support
- Zbuffers were saved 16 bits, now 32 bits
- automatic adding of .exr extensions went wrong
Imbuf:
- added proper imbuf->flags and imbuf->mall support for float buffers, it
was created for *each* imbuf. :)
- found bugs for float buffers in scaling and flipping. Code there will
need more checks still
- imbuf also needs to be verified to behave properly when no 32 bits
rect exists (for saving for example)
TODO:
- support internal float images for textures, backbuf, AO probes, and
display in Image window
Hope this commit won't screwup syncing with bf-blender... :/
2006-01-09 00:40:35 +00:00
|
|
|
}
|
|
|
|
|
FLOAT2RGBE(fcol, rgbe);
|
2012-05-23 22:45:39 +00:00
|
|
|
COPY_RGBE(rgbe, rgbe_scan[i]);
|
2012-05-16 09:26:37 +00:00
|
|
|
j += channels;
|
2005-11-20 14:32:07 +00:00
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2012-05-16 09:26:37 +00:00
|
|
|
if ((width < MINELEN) | (width > MAXELEN)) { /* OOBs, write out flat */
|
2018-01-14 14:19:57 +01:00
|
|
|
int x = fwrite((char *)rgbe_scan, sizeof(RGBE), width, file) - width;
|
Orange branch: OpenEXR finally in Blender!
Credits go to Gernot Ziegler, who originally coded EXR support, and to
Austin Benesh for bringing it further. Kent Mein provided a lot of code
for integrating float buffers in Blender imbuf and ImBuf API cleanup,
and provided Make and Scons and static linking.
At this moment; the EXR libraries are a *dependency*, so you cannot get
the Orange branch compiled without having OpenEXR installed. Get the
(precompiled or sources) stuff from www.openexr.com. Current default is
that the headers and lib resides in /user/local/
Several changes/additions/fixes were added:
- EXR code only supported 'half' format (16 bits per channel). I've added
float writing, but for reading it I need tomorrow. :)
- Quite some clumsy copying of data happened in EXR code.
- cleaned up the api calls already a bit, preparing for more advanced
support
- Zbuffers were saved 16 bits, now 32 bits
- automatic adding of .exr extensions went wrong
Imbuf:
- added proper imbuf->flags and imbuf->mall support for float buffers, it
was created for *each* imbuf. :)
- found bugs for float buffers in scaling and flipping. Code there will
need more checks still
- imbuf also needs to be verified to behave properly when no 32 bits
rect exists (for saving for example)
TODO:
- support internal float images for textures, backbuf, AO probes, and
display in Image window
Hope this commit won't screwup syncing with bf-blender... :/
2006-01-09 00:40:35 +00:00
|
|
|
MEM_freeN(rgbe_scan);
|
|
|
|
|
return x;
|
|
|
|
|
}
|
2005-11-20 14:32:07 +00:00
|
|
|
/* put magic header */
|
|
|
|
|
putc(2, file);
|
|
|
|
|
putc(2, file);
|
|
|
|
|
putc((unsigned char)(width >> 8), file);
|
|
|
|
|
putc((unsigned char)(width & 255), file);
|
2010-01-14 10:59:42 +00:00
|
|
|
/* put components separately */
|
2018-01-14 14:19:57 +01:00
|
|
|
for (size_t i = 0; i < 4; i++) {
|
|
|
|
|
for (size_t j = 0; j < width; j += cnt) { /* find next run */
|
2012-05-16 09:26:37 +00:00
|
|
|
for (beg = j; beg < width; beg += cnt) {
|
|
|
|
|
for (cnt = 1; (cnt < 127) && ((beg + cnt) < width) &&
|
|
|
|
|
(rgbe_scan[beg + cnt][i] == rgbe_scan[beg][i]);
|
2019-04-23 11:01:30 +10:00
|
|
|
cnt++) {
|
2019-05-03 12:41:06 +10:00
|
|
|
/* pass */
|
2019-04-23 11:01:30 +10:00
|
|
|
}
|
|
|
|
|
if (cnt >= MINRUN) {
|
2012-05-16 09:26:37 +00:00
|
|
|
break; /* long enough */
|
2019-04-23 11:01:30 +10:00
|
|
|
}
|
2005-11-20 14:32:07 +00:00
|
|
|
}
|
2012-05-16 09:26:37 +00:00
|
|
|
if (((beg - j) > 1) && ((beg - j) < MINRUN)) {
|
|
|
|
|
c2 = j + 1;
|
2017-05-19 22:18:54 +10:00
|
|
|
while (rgbe_scan[c2++][i] == rgbe_scan[j][i]) {
|
2005-11-20 14:32:07 +00:00
|
|
|
if (c2 == beg) { /* short run */
|
2012-05-16 09:26:37 +00:00
|
|
|
putc((unsigned char)(128 + beg - j), file);
|
2005-11-20 14:32:07 +00:00
|
|
|
putc((unsigned char)(rgbe_scan[j][i]), file);
|
|
|
|
|
j = beg;
|
|
|
|
|
break;
|
|
|
|
|
}
|
2017-05-19 22:18:54 +10:00
|
|
|
}
|
2005-11-20 14:32:07 +00:00
|
|
|
}
|
|
|
|
|
while (j < beg) { /* write out non-run */
|
2019-04-23 11:01:30 +10:00
|
|
|
if ((c2 = beg - j) > 128) {
|
2012-05-16 09:26:37 +00:00
|
|
|
c2 = 128;
|
2019-04-23 11:01:30 +10:00
|
|
|
}
|
2005-11-20 14:32:07 +00:00
|
|
|
putc((unsigned char)(c2), file);
|
2019-04-23 11:01:30 +10:00
|
|
|
while (c2--) {
|
2005-11-20 14:32:07 +00:00
|
|
|
putc(rgbe_scan[j++][i], file);
|
2019-04-23 11:01:30 +10:00
|
|
|
}
|
2005-11-20 14:32:07 +00:00
|
|
|
}
|
|
|
|
|
if (cnt >= MINRUN) { /* write out run */
|
2012-05-16 09:26:37 +00:00
|
|
|
putc((unsigned char)(128 + cnt), file);
|
2005-11-20 14:32:07 +00:00
|
|
|
putc(rgbe_scan[beg][i], file);
|
|
|
|
|
}
|
2013-03-09 03:46:30 +00:00
|
|
|
else {
|
|
|
|
|
cnt = 0;
|
|
|
|
|
}
|
2005-11-20 14:32:07 +00:00
|
|
|
}
|
|
|
|
|
}
|
Orange branch: OpenEXR finally in Blender!
Credits go to Gernot Ziegler, who originally coded EXR support, and to
Austin Benesh for bringing it further. Kent Mein provided a lot of code
for integrating float buffers in Blender imbuf and ImBuf API cleanup,
and provided Make and Scons and static linking.
At this moment; the EXR libraries are a *dependency*, so you cannot get
the Orange branch compiled without having OpenEXR installed. Get the
(precompiled or sources) stuff from www.openexr.com. Current default is
that the headers and lib resides in /user/local/
Several changes/additions/fixes were added:
- EXR code only supported 'half' format (16 bits per channel). I've added
float writing, but for reading it I need tomorrow. :)
- Quite some clumsy copying of data happened in EXR code.
- cleaned up the api calls already a bit, preparing for more advanced
support
- Zbuffers were saved 16 bits, now 32 bits
- automatic adding of .exr extensions went wrong
Imbuf:
- added proper imbuf->flags and imbuf->mall support for float buffers, it
was created for *each* imbuf. :)
- found bugs for float buffers in scaling and flipping. Code there will
need more checks still
- imbuf also needs to be verified to behave properly when no 32 bits
rect exists (for saving for example)
TODO:
- support internal float images for textures, backbuf, AO probes, and
display in Image window
Hope this commit won't screwup syncing with bf-blender... :/
2006-01-09 00:40:35 +00:00
|
|
|
MEM_freeN(rgbe_scan);
|
2005-11-20 14:32:07 +00:00
|
|
|
return (ferror(file) ? -1 : 0);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void writeHeader(FILE *file, int width, int height)
|
|
|
|
|
{
|
|
|
|
|
fprintf(file, "#?RADIANCE");
|
|
|
|
|
fputc(10, file);
|
|
|
|
|
fprintf(file, "# %s", "Created with Blender");
|
|
|
|
|
fputc(10, file);
|
|
|
|
|
fprintf(file, "EXPOSURE=%25.13f", 1.0);
|
|
|
|
|
fputc(10, file);
|
2007-10-26 15:32:36 +00:00
|
|
|
fprintf(file, "FORMAT=32-bit_rle_rgbe");
|
|
|
|
|
fputc(10, file);
|
2005-11-20 14:32:07 +00:00
|
|
|
fputc(10, file);
|
|
|
|
|
fprintf(file, "-Y %d +X %d", height, width);
|
|
|
|
|
fputc(10, file);
|
|
|
|
|
}
|
|
|
|
|
|
2020-11-11 16:14:05 +11:00
|
|
|
bool imb_savehdr(struct ImBuf *ibuf, const char *filepath, int flags)
|
2005-11-20 14:32:07 +00:00
|
|
|
{
|
2020-09-04 20:59:13 +02:00
|
|
|
FILE *file = BLI_fopen(filepath, "wb");
|
2012-05-16 09:26:37 +00:00
|
|
|
float *fp = NULL;
|
2018-01-14 14:19:57 +01:00
|
|
|
size_t width = ibuf->x, height = ibuf->y;
|
2012-05-16 09:26:37 +00:00
|
|
|
unsigned char *cp = NULL;
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2010-10-17 06:38:56 +00:00
|
|
|
(void)flags; /* unused */
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2015-10-20 02:12:41 +11:00
|
|
|
if (file == NULL) {
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2005-11-20 14:32:07 +00:00
|
|
|
writeHeader(file, width, height);
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2019-04-23 11:01:30 +10:00
|
|
|
if (ibuf->rect) {
|
2012-05-16 09:26:37 +00:00
|
|
|
cp = (unsigned char *)ibuf->rect + ibuf->channels * (height - 1) * width;
|
2019-04-23 11:01:30 +10:00
|
|
|
}
|
|
|
|
|
if (ibuf->rect_float) {
|
2012-05-16 09:26:37 +00:00
|
|
|
fp = ibuf->rect_float + ibuf->channels * (height - 1) * width;
|
2019-04-23 11:01:30 +10:00
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2018-01-17 21:11:59 +01:00
|
|
|
for (size_t y = 0; y < height; y++) {
|
2008-04-29 16:57:39 +00:00
|
|
|
if (fwritecolrs(file, width, ibuf->channels, cp, fp) < 0) {
|
2005-11-20 14:32:07 +00:00
|
|
|
fclose(file);
|
|
|
|
|
printf("HDR write error\n");
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
2019-04-23 11:01:30 +10:00
|
|
|
if (cp) {
|
2012-05-16 09:26:37 +00:00
|
|
|
cp -= ibuf->channels * width;
|
2019-04-23 11:01:30 +10:00
|
|
|
}
|
|
|
|
|
if (fp) {
|
2012-05-16 09:26:37 +00:00
|
|
|
fp -= ibuf->channels * width;
|
2019-04-23 11:01:30 +10:00
|
|
|
}
|
2005-11-20 14:32:07 +00:00
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2005-11-20 14:32:07 +00:00
|
|
|
fclose(file);
|
|
|
|
|
return 1;
|
|
|
|
|
}
|