2011-02-23 10:52:22 +00:00
|
|
|
/*
|
2002-10-12 11:37:38 +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
|
2008-04-16 22:40:48 +00:00
|
|
|
* of the License, or (at your option) any later version.
|
2002-10-12 11:37:38 +00:00
|
|
|
*
|
|
|
|
|
* 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.
|
2002-10-12 11:37:38 +00:00
|
|
|
*
|
|
|
|
|
* The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
|
|
|
|
|
* All rights reserved.
|
|
|
|
|
*/
|
|
|
|
|
|
2019-02-18 08:08:12 +11:00
|
|
|
/** \file
|
|
|
|
|
* \ingroup imbuf
|
2012-05-09 15:20:08 +00:00
|
|
|
*
|
2012-05-09 15:21:45 +00:00
|
|
|
* \todo Save floats as 16 bits per channel, currently readonly.
|
2011-02-27 20:23:21 +00:00
|
|
|
*/
|
|
|
|
|
|
2002-10-12 11:37:38 +00:00
|
|
|
#include "png.h"
|
|
|
|
|
|
2012-08-12 23:28:33 +00:00
|
|
|
#include "BLI_fileops.h"
|
2012-05-09 15:20:08 +00:00
|
|
|
#include "BLI_math.h"
|
2020-03-19 09:33:03 +01:00
|
|
|
#include "BLI_utildefines.h"
|
2014-06-30 21:06:23 +06:00
|
|
|
|
|
|
|
|
#include "BKE_global.h"
|
2015-01-31 17:15:43 +01:00
|
|
|
#include "BKE_idprop.h"
|
2014-06-30 21:06:23 +06:00
|
|
|
|
2020-12-15 10:47:58 +11:00
|
|
|
#include "DNA_ID.h" /* ID property definitions. */
|
|
|
|
|
|
2010-08-16 05:46:10 +00:00
|
|
|
#include "MEM_guardedalloc.h"
|
2002-10-12 11:37:38 +00:00
|
|
|
|
|
|
|
|
#include "IMB_imbuf.h"
|
2020-03-19 09:33:03 +01:00
|
|
|
#include "IMB_imbuf_types.h"
|
2002-10-12 11:37:38 +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"
|
2020-03-19 09:33:03 +01:00
|
|
|
#include "IMB_metadata.h"
|
2002-10-12 11:37:38 +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"
|
|
|
|
|
|
2003-12-04 18:18:05 +00:00
|
|
|
typedef struct PNGReadStruct {
|
2015-07-11 23:52:18 +10:00
|
|
|
const unsigned char *data;
|
2005-01-03 19:53:04 +00:00
|
|
|
unsigned int size;
|
|
|
|
|
unsigned int seek;
|
2012-04-21 13:37:26 +00:00
|
|
|
} PNGReadStruct;
|
2002-10-12 11:37:38 +00:00
|
|
|
|
2012-04-29 17:11:40 +00:00
|
|
|
static void ReadData(png_structp png_ptr, png_bytep data, png_size_t length);
|
|
|
|
|
static void WriteData(png_structp png_ptr, png_bytep data, png_size_t length);
|
|
|
|
|
static void Flush(png_structp png_ptr);
|
2002-10-12 11:37:38 +00:00
|
|
|
|
2013-01-15 07:35:32 +00:00
|
|
|
BLI_INLINE unsigned short UPSAMPLE_8_TO_16(const unsigned char _val)
|
|
|
|
|
{
|
|
|
|
|
return (_val << 8) + _val;
|
|
|
|
|
}
|
|
|
|
|
|
2020-11-11 16:14:09 +11:00
|
|
|
bool imb_is_a_png(const unsigned char *mem, size_t size)
|
2002-10-12 11:37:38 +00:00
|
|
|
{
|
2020-11-11 16:14:09 +11:00
|
|
|
const int num_to_check = 8;
|
|
|
|
|
if (size < num_to_check) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
bool ok = false;
|
2002-10-12 11:37:38 +00:00
|
|
|
|
2015-07-11 19:18:20 +02:00
|
|
|
#if (PNG_LIBPNG_VER_MAJOR == 1) && (PNG_LIBPNG_VER_MINOR == 2)
|
2020-11-11 14:46:32 +11:00
|
|
|
/* Older version of libpng doesn't use const pointer to memory. */
|
2020-11-11 16:14:09 +11:00
|
|
|
ok = !png_sig_cmp((png_bytep)mem, 0, num_to_check);
|
2015-07-11 19:18:20 +02:00
|
|
|
#else
|
2020-11-11 16:14:09 +11:00
|
|
|
ok = !png_sig_cmp(mem, 0, num_to_check);
|
2015-07-11 19:18:20 +02:00
|
|
|
#endif
|
2020-11-11 16:14:09 +11:00
|
|
|
return ok;
|
2002-10-12 11:37:38 +00:00
|
|
|
}
|
|
|
|
|
|
2018-06-17 17:04:54 +02:00
|
|
|
static void Flush(png_structp png_ptr)
|
2010-10-16 14:32:17 +00:00
|
|
|
{
|
|
|
|
|
(void)png_ptr;
|
2002-10-12 11:37:38 +00:00
|
|
|
}
|
|
|
|
|
|
2012-04-29 17:11:40 +00:00
|
|
|
static void WriteData(png_structp png_ptr, png_bytep data, png_size_t length)
|
2003-12-04 18:18:05 +00:00
|
|
|
{
|
|
|
|
|
ImBuf *ibuf = (ImBuf *)png_get_io_ptr(png_ptr);
|
|
|
|
|
|
2016-02-20 14:52:36 +01:00
|
|
|
/* if buffer is too small increase it. */
|
2003-12-04 18:18:05 +00:00
|
|
|
while (ibuf->encodedsize + length > ibuf->encodedbuffersize) {
|
|
|
|
|
imb_enlargeencodedbufferImBuf(ibuf);
|
|
|
|
|
}
|
2002-10-12 11:37:38 +00:00
|
|
|
|
2003-12-04 18:18:05 +00:00
|
|
|
memcpy(ibuf->encodedbuffer + ibuf->encodedsize, data, length);
|
|
|
|
|
ibuf->encodedsize += length;
|
|
|
|
|
}
|
|
|
|
|
|
2012-04-29 17:11:40 +00:00
|
|
|
static void ReadData(png_structp png_ptr, png_bytep data, png_size_t length)
|
2002-10-12 11:37:38 +00:00
|
|
|
{
|
2012-05-09 15:21:45 +00:00
|
|
|
PNGReadStruct *rs = (PNGReadStruct *)png_get_io_ptr(png_ptr);
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2002-10-12 11:37:38 +00:00
|
|
|
if (rs) {
|
|
|
|
|
if (length <= rs->size - rs->seek) {
|
|
|
|
|
memcpy(data, rs->data + rs->seek, length);
|
|
|
|
|
rs->seek += length;
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2002-10-12 11:37:38 +00:00
|
|
|
printf("Reached EOF while decoding PNG\n");
|
|
|
|
|
longjmp(png_jmpbuf(png_ptr), 1);
|
|
|
|
|
}
|
|
|
|
|
|
2013-01-15 08:10:02 +00:00
|
|
|
static float channel_colormanage_noop(float value)
|
|
|
|
|
{
|
|
|
|
|
return value;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* wrap to avoid macro calling functions multiple times */
|
|
|
|
|
BLI_INLINE unsigned short ftoshort(float val)
|
|
|
|
|
{
|
2018-05-07 17:31:28 +02:00
|
|
|
return unit_float_to_ushort_clamp(val);
|
2013-01-15 08:10:02 +00:00
|
|
|
}
|
|
|
|
|
|
2020-11-11 16:14:05 +11:00
|
|
|
bool imb_savepng(struct ImBuf *ibuf, const char *filepath, int flags)
|
2003-12-04 18:18:05 +00:00
|
|
|
{
|
|
|
|
|
png_structp png_ptr;
|
|
|
|
|
png_infop info_ptr;
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2011-03-03 17:58:06 +00:00
|
|
|
unsigned char *pixels = NULL;
|
2003-12-04 18:18:05 +00:00
|
|
|
unsigned char *from, *to;
|
2012-12-30 13:01:47 +00:00
|
|
|
unsigned short *pixels16 = NULL, *to16;
|
2012-12-31 13:52:13 +00:00
|
|
|
float *from_float, from_straight[4];
|
2011-03-03 17:58:06 +00:00
|
|
|
png_bytepp row_pointers = NULL;
|
2003-12-04 18:18:05 +00:00
|
|
|
int i, bytesperpixel, color_type = PNG_COLOR_TYPE_GRAY;
|
2011-03-03 17:58:06 +00:00
|
|
|
FILE *fp = NULL;
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2015-07-13 13:58:17 +02:00
|
|
|
bool is_16bit = (ibuf->foptions.flag & PNG_16BIT) != 0;
|
2013-01-15 07:35:32 +00:00
|
|
|
bool has_float = (ibuf->rect_float != NULL);
|
2013-02-22 09:20:22 +00:00
|
|
|
int channels_in_float = ibuf->channels ? ibuf->channels : 4;
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2013-01-15 08:10:02 +00:00
|
|
|
float (*chanel_colormanage_cb)(float);
|
2015-05-19 13:02:17 +05:00
|
|
|
size_t num_bytes;
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2010-07-17 13:29:55 +00:00
|
|
|
/* use the jpeg quality setting for compression */
|
|
|
|
|
int compression;
|
2015-07-13 13:58:17 +02:00
|
|
|
compression = (int)(((float)(ibuf->foptions.quality) / 11.1111f));
|
2012-05-09 15:21:45 +00:00
|
|
|
compression = compression < 0 ? 0 : (compression > 9 ? 9 : compression);
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2017-08-10 14:11:18 +02:00
|
|
|
if (ibuf->float_colorspace || (ibuf->colormanage_flag & IMB_COLORMANAGE_IS_DATA)) {
|
2013-01-15 08:10:02 +00:00
|
|
|
/* float buffer was managed already, no need in color space conversion */
|
|
|
|
|
chanel_colormanage_cb = channel_colormanage_noop;
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
/* standard linear-to-srgb conversion if float buffer wasn't managed */
|
|
|
|
|
chanel_colormanage_cb = linearrgb_to_srgb;
|
|
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2010-11-25 14:00:11 +00:00
|
|
|
/* for prints */
|
2019-04-23 11:01:30 +10:00
|
|
|
if (flags & IB_mem) {
|
2020-09-04 20:59:13 +02:00
|
|
|
filepath = "<memory>";
|
2019-04-23 11:01:30 +10:00
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2011-11-21 20:47:19 +00:00
|
|
|
bytesperpixel = (ibuf->planes + 7) >> 3;
|
2003-12-04 18:18:05 +00:00
|
|
|
if ((bytesperpixel > 4) || (bytesperpixel == 2)) {
|
2020-09-04 20:59:13 +02:00
|
|
|
printf(
|
|
|
|
|
"imb_savepng: Unsupported bytes per pixel: %d for file: '%s'\n", bytesperpixel, filepath);
|
2020-08-08 11:02:11 +10:00
|
|
|
return 0;
|
2003-12-04 18:18:05 +00:00
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2003-12-04 18:18:05 +00:00
|
|
|
png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
|
|
|
|
|
if (png_ptr == NULL) {
|
2020-09-04 20:59:13 +02:00
|
|
|
printf("imb_savepng: Cannot png_create_write_struct for file: '%s'\n", filepath);
|
2003-12-04 18:18:05 +00:00
|
|
|
return 0;
|
|
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2003-12-04 18:18:05 +00:00
|
|
|
info_ptr = png_create_info_struct(png_ptr);
|
|
|
|
|
if (info_ptr == NULL) {
|
|
|
|
|
png_destroy_write_struct(&png_ptr, (png_infopp)NULL);
|
2020-09-04 20:59:13 +02:00
|
|
|
printf("imb_savepng: Cannot png_create_info_struct for file: '%s'\n", filepath);
|
2003-12-04 18:18:05 +00:00
|
|
|
return 0;
|
|
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2012-07-07 22:51:57 +00:00
|
|
|
/* copy image data */
|
2015-05-19 13:02:17 +05:00
|
|
|
num_bytes = ((size_t)ibuf->x) * ibuf->y * bytesperpixel;
|
2019-04-23 11:01:30 +10:00
|
|
|
if (is_16bit) {
|
2015-05-19 13:02:17 +05:00
|
|
|
pixels16 = MEM_mallocN(num_bytes * sizeof(unsigned short), "png 16bit pixels");
|
2019-04-23 11:01:30 +10:00
|
|
|
}
|
|
|
|
|
else {
|
2015-05-19 13:02:17 +05:00
|
|
|
pixels = MEM_mallocN(num_bytes * sizeof(unsigned char), "png 8bit pixels");
|
2019-04-23 11:01:30 +10:00
|
|
|
}
|
2012-12-30 13:01:47 +00:00
|
|
|
if (pixels == NULL && pixels16 == NULL) {
|
2010-11-25 14:00:11 +00:00
|
|
|
printf(
|
2020-05-13 20:12:25 +02:00
|
|
|
"imb_savepng: Cannot allocate pixels array of %dx%d, %d bytes per pixel for file: "
|
|
|
|
|
"'%s'\n",
|
2010-11-25 14:00:11 +00:00
|
|
|
ibuf->x,
|
|
|
|
|
ibuf->y,
|
|
|
|
|
bytesperpixel,
|
2020-09-04 20:59:13 +02:00
|
|
|
filepath);
|
2020-05-13 20:12:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* allocate memory for an array of row-pointers */
|
|
|
|
|
row_pointers = (png_bytepp)MEM_mallocN(ibuf->y * sizeof(png_bytep), "row_pointers");
|
|
|
|
|
if (row_pointers == NULL) {
|
2020-09-04 20:59:13 +02:00
|
|
|
printf("imb_savepng: Cannot allocate row-pointers array for file '%s'\n", filepath);
|
2020-05-13 20:12:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ((pixels == NULL && pixels16 == NULL) || (row_pointers == NULL) ||
|
|
|
|
|
setjmp(png_jmpbuf(png_ptr))) {
|
|
|
|
|
/* On error jump here, and free any resources. */
|
|
|
|
|
png_destroy_write_struct(&png_ptr, &info_ptr);
|
|
|
|
|
if (pixels) {
|
|
|
|
|
MEM_freeN(pixels);
|
|
|
|
|
}
|
|
|
|
|
if (pixels16) {
|
|
|
|
|
MEM_freeN(pixels16);
|
|
|
|
|
}
|
|
|
|
|
if (row_pointers) {
|
|
|
|
|
MEM_freeN(row_pointers);
|
|
|
|
|
}
|
|
|
|
|
if (fp) {
|
|
|
|
|
fflush(fp);
|
|
|
|
|
fclose(fp);
|
|
|
|
|
}
|
2003-12-04 18:18:05 +00:00
|
|
|
return 0;
|
|
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2003-12-04 18:18:05 +00:00
|
|
|
from = (unsigned char *)ibuf->rect;
|
|
|
|
|
to = pixels;
|
2012-12-30 13:01:47 +00:00
|
|
|
from_float = ibuf->rect_float;
|
|
|
|
|
to16 = pixels16;
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2003-12-04 18:18:05 +00:00
|
|
|
switch (bytesperpixel) {
|
2012-05-09 15:21:45 +00:00
|
|
|
case 4:
|
|
|
|
|
color_type = PNG_COLOR_TYPE_RGBA;
|
2012-12-30 13:01:47 +00:00
|
|
|
if (is_16bit) {
|
2013-01-15 07:35:32 +00:00
|
|
|
if (has_float) {
|
2013-02-22 09:20:22 +00:00
|
|
|
if (channels_in_float == 4) {
|
|
|
|
|
for (i = ibuf->x * ibuf->y; i > 0; i--) {
|
|
|
|
|
premul_to_straight_v4_v4(from_straight, from_float);
|
|
|
|
|
to16[0] = ftoshort(chanel_colormanage_cb(from_straight[0]));
|
|
|
|
|
to16[1] = ftoshort(chanel_colormanage_cb(from_straight[1]));
|
|
|
|
|
to16[2] = ftoshort(chanel_colormanage_cb(from_straight[2]));
|
|
|
|
|
to16[3] = ftoshort(chanel_colormanage_cb(from_straight[3]));
|
|
|
|
|
to16 += 4;
|
|
|
|
|
from_float += 4;
|
2019-04-17 06:17:24 +02:00
|
|
|
}
|
2013-02-22 09:20:22 +00:00
|
|
|
}
|
|
|
|
|
else if (channels_in_float == 3) {
|
|
|
|
|
for (i = ibuf->x * ibuf->y; i > 0; i--) {
|
|
|
|
|
to16[0] = ftoshort(chanel_colormanage_cb(from_float[0]));
|
|
|
|
|
to16[1] = ftoshort(chanel_colormanage_cb(from_float[1]));
|
|
|
|
|
to16[2] = ftoshort(chanel_colormanage_cb(from_float[2]));
|
|
|
|
|
to16[3] = 65535;
|
|
|
|
|
to16 += 4;
|
|
|
|
|
from_float += 3;
|
2019-04-17 06:17:24 +02:00
|
|
|
}
|
2013-02-22 09:20:22 +00:00
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
for (i = ibuf->x * ibuf->y; i > 0; i--) {
|
|
|
|
|
to16[0] = ftoshort(chanel_colormanage_cb(from_float[0]));
|
|
|
|
|
to16[2] = to16[1] = to16[0];
|
|
|
|
|
to16[3] = 65535;
|
|
|
|
|
to16 += 4;
|
|
|
|
|
from_float++;
|
|
|
|
|
}
|
2013-01-15 07:35:32 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
for (i = ibuf->x * ibuf->y; i > 0; i--) {
|
|
|
|
|
to16[0] = UPSAMPLE_8_TO_16(from[0]);
|
|
|
|
|
to16[1] = UPSAMPLE_8_TO_16(from[1]);
|
|
|
|
|
to16[2] = UPSAMPLE_8_TO_16(from[2]);
|
|
|
|
|
to16[3] = UPSAMPLE_8_TO_16(from[3]);
|
|
|
|
|
to16 += 4;
|
|
|
|
|
from += 4;
|
|
|
|
|
}
|
2012-12-30 13:01:47 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
for (i = ibuf->x * ibuf->y; i > 0; i--) {
|
|
|
|
|
to[0] = from[0];
|
|
|
|
|
to[1] = from[1];
|
|
|
|
|
to[2] = from[2];
|
|
|
|
|
to[3] = from[3];
|
|
|
|
|
to += 4;
|
|
|
|
|
from += 4;
|
2012-05-09 15:21:45 +00:00
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
}
|
2012-05-09 15:21:45 +00:00
|
|
|
break;
|
|
|
|
|
case 3:
|
|
|
|
|
color_type = PNG_COLOR_TYPE_RGB;
|
2012-12-30 13:01:47 +00:00
|
|
|
if (is_16bit) {
|
2013-01-15 07:35:32 +00:00
|
|
|
if (has_float) {
|
2013-02-22 09:20:22 +00:00
|
|
|
if (channels_in_float == 4) {
|
|
|
|
|
for (i = ibuf->x * ibuf->y; i > 0; i--) {
|
|
|
|
|
premul_to_straight_v4_v4(from_straight, from_float);
|
|
|
|
|
to16[0] = ftoshort(chanel_colormanage_cb(from_straight[0]));
|
|
|
|
|
to16[1] = ftoshort(chanel_colormanage_cb(from_straight[1]));
|
|
|
|
|
to16[2] = ftoshort(chanel_colormanage_cb(from_straight[2]));
|
|
|
|
|
to16 += 3;
|
|
|
|
|
from_float += 4;
|
|
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
}
|
2013-02-22 09:20:22 +00:00
|
|
|
else if (channels_in_float == 3) {
|
|
|
|
|
for (i = ibuf->x * ibuf->y; i > 0; i--) {
|
|
|
|
|
to16[0] = ftoshort(chanel_colormanage_cb(from_float[0]));
|
|
|
|
|
to16[1] = ftoshort(chanel_colormanage_cb(from_float[1]));
|
|
|
|
|
to16[2] = ftoshort(chanel_colormanage_cb(from_float[2]));
|
|
|
|
|
to16 += 3;
|
|
|
|
|
from_float += 3;
|
|
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
}
|
2013-02-22 09:20:22 +00:00
|
|
|
else {
|
|
|
|
|
for (i = ibuf->x * ibuf->y; i > 0; i--) {
|
|
|
|
|
to16[0] = ftoshort(chanel_colormanage_cb(from_float[0]));
|
|
|
|
|
to16[2] = to16[1] = to16[0];
|
|
|
|
|
to16 += 3;
|
|
|
|
|
from_float++;
|
2013-01-15 07:35:32 +00:00
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
}
|
|
|
|
|
}
|
2013-01-15 07:35:32 +00:00
|
|
|
else {
|
|
|
|
|
for (i = ibuf->x * ibuf->y; i > 0; i--) {
|
|
|
|
|
to16[0] = UPSAMPLE_8_TO_16(from[0]);
|
|
|
|
|
to16[1] = UPSAMPLE_8_TO_16(from[1]);
|
|
|
|
|
to16[2] = UPSAMPLE_8_TO_16(from[2]);
|
|
|
|
|
to16 += 3;
|
|
|
|
|
from += 4;
|
2012-12-30 13:01:47 +00:00
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
}
|
|
|
|
|
}
|
2012-12-30 13:01:47 +00:00
|
|
|
else {
|
|
|
|
|
for (i = ibuf->x * ibuf->y; i > 0; i--) {
|
|
|
|
|
to[0] = from[0];
|
|
|
|
|
to[1] = from[1];
|
|
|
|
|
to[2] = from[2];
|
|
|
|
|
to += 3;
|
|
|
|
|
from += 4;
|
2012-05-09 15:21:45 +00:00
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
}
|
2012-05-09 15:21:45 +00:00
|
|
|
break;
|
|
|
|
|
case 1:
|
|
|
|
|
color_type = PNG_COLOR_TYPE_GRAY;
|
2012-12-30 13:01:47 +00:00
|
|
|
if (is_16bit) {
|
2013-01-15 07:35:32 +00:00
|
|
|
if (has_float) {
|
2013-02-22 09:20:22 +00:00
|
|
|
float rgb[3];
|
|
|
|
|
if (channels_in_float == 4) {
|
|
|
|
|
for (i = ibuf->x * ibuf->y; i > 0; i--) {
|
|
|
|
|
premul_to_straight_v4_v4(from_straight, from_float);
|
|
|
|
|
rgb[0] = chanel_colormanage_cb(from_straight[0]);
|
|
|
|
|
rgb[1] = chanel_colormanage_cb(from_straight[1]);
|
|
|
|
|
rgb[2] = chanel_colormanage_cb(from_straight[2]);
|
2015-03-17 15:20:33 +01:00
|
|
|
to16[0] = ftoshort(IMB_colormanagement_get_luminance(rgb));
|
2013-02-22 09:20:22 +00:00
|
|
|
to16++;
|
|
|
|
|
from_float += 4;
|
|
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
}
|
2013-02-22 09:20:22 +00:00
|
|
|
else if (channels_in_float == 3) {
|
|
|
|
|
for (i = ibuf->x * ibuf->y; i > 0; i--) {
|
|
|
|
|
rgb[0] = chanel_colormanage_cb(from_float[0]);
|
|
|
|
|
rgb[1] = chanel_colormanage_cb(from_float[1]);
|
|
|
|
|
rgb[2] = chanel_colormanage_cb(from_float[2]);
|
2015-03-17 15:20:33 +01:00
|
|
|
to16[0] = ftoshort(IMB_colormanagement_get_luminance(rgb));
|
2013-02-22 09:20:22 +00:00
|
|
|
to16++;
|
|
|
|
|
from_float += 3;
|
|
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
}
|
2013-02-22 09:20:22 +00:00
|
|
|
else {
|
|
|
|
|
for (i = ibuf->x * ibuf->y; i > 0; i--) {
|
|
|
|
|
to16[0] = ftoshort(chanel_colormanage_cb(from_float[0]));
|
|
|
|
|
to16++;
|
|
|
|
|
from_float++;
|
2013-01-15 07:35:32 +00:00
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
}
|
|
|
|
|
}
|
2013-01-15 07:35:32 +00:00
|
|
|
else {
|
|
|
|
|
for (i = ibuf->x * ibuf->y; i > 0; i--) {
|
|
|
|
|
to16[0] = UPSAMPLE_8_TO_16(from[0]);
|
|
|
|
|
to16++;
|
|
|
|
|
from += 4;
|
2012-12-30 13:01:47 +00:00
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
}
|
|
|
|
|
}
|
2012-12-30 13:01:47 +00:00
|
|
|
else {
|
|
|
|
|
for (i = ibuf->x * ibuf->y; i > 0; i--) {
|
|
|
|
|
to[0] = from[0];
|
|
|
|
|
to++;
|
|
|
|
|
from += 4;
|
2012-05-09 15:21:45 +00:00
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
}
|
2012-05-09 15:21:45 +00:00
|
|
|
break;
|
2003-12-04 18:18:05 +00:00
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2003-12-04 18:18:05 +00:00
|
|
|
if (flags & IB_mem) {
|
2012-07-07 22:51:57 +00:00
|
|
|
/* create image in memory */
|
2003-12-04 18:18:05 +00:00
|
|
|
imb_addencodedbufferImBuf(ibuf);
|
|
|
|
|
ibuf->encodedsize = 0;
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2003-12-04 18:18:05 +00:00
|
|
|
png_set_write_fn(png_ptr, (png_voidp)ibuf, WriteData, Flush);
|
2011-11-11 13:09:14 +00:00
|
|
|
}
|
|
|
|
|
else {
|
2020-09-04 20:59:13 +02:00
|
|
|
fp = BLI_fopen(filepath, "wb");
|
2010-03-22 09:30:00 +00:00
|
|
|
if (!fp) {
|
2010-11-25 14:00:11 +00:00
|
|
|
png_destroy_write_struct(&png_ptr, &info_ptr);
|
2019-04-23 11:01:30 +10:00
|
|
|
if (pixels) {
|
2012-12-30 13:01:47 +00:00
|
|
|
MEM_freeN(pixels);
|
2019-04-23 11:01:30 +10:00
|
|
|
}
|
|
|
|
|
if (pixels16) {
|
2012-12-30 13:01:47 +00:00
|
|
|
MEM_freeN(pixels16);
|
2019-04-23 11:01:30 +10:00
|
|
|
}
|
2020-11-13 11:34:53 +11:00
|
|
|
MEM_freeN(row_pointers);
|
2020-09-04 20:59:13 +02:00
|
|
|
printf("imb_savepng: Cannot open file for writing: '%s'\n", filepath);
|
2007-01-22 05:07:16 +00:00
|
|
|
return 0;
|
|
|
|
|
}
|
2003-12-04 18:18:05 +00:00
|
|
|
png_init_io(png_ptr, fp);
|
|
|
|
|
}
|
|
|
|
|
|
2012-03-09 18:28:30 +00:00
|
|
|
#if 0
|
2019-04-17 08:24:14 +02:00
|
|
|
png_set_filter(png_ptr,
|
|
|
|
|
0,
|
|
|
|
|
PNG_FILTER_NONE | PNG_FILTER_VALUE_NONE | PNG_FILTER_SUB | PNG_FILTER_VALUE_SUB |
|
|
|
|
|
PNG_FILTER_UP | PNG_FILTER_VALUE_UP | PNG_FILTER_AVG | PNG_FILTER_VALUE_AVG |
|
|
|
|
|
PNG_FILTER_PAETH | PNG_FILTER_VALUE_PAETH | PNG_ALL_FILTERS);
|
2012-03-09 18:28:30 +00:00
|
|
|
#endif
|
2003-12-04 18:18:05 +00:00
|
|
|
|
2010-07-17 13:29:55 +00:00
|
|
|
png_set_compression_level(png_ptr, compression);
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2012-07-07 22:51:57 +00:00
|
|
|
/* png image settings */
|
2003-12-04 18:18:05 +00:00
|
|
|
png_set_IHDR(png_ptr,
|
2011-11-11 13:09:14 +00:00
|
|
|
info_ptr,
|
|
|
|
|
ibuf->x,
|
|
|
|
|
ibuf->y,
|
2012-12-30 13:01:47 +00:00
|
|
|
is_16bit ? 16 : 8,
|
2011-11-11 13:09:14 +00:00
|
|
|
color_type,
|
|
|
|
|
PNG_INTERLACE_NONE,
|
|
|
|
|
PNG_COMPRESSION_TYPE_DEFAULT,
|
|
|
|
|
PNG_FILTER_TYPE_DEFAULT);
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2007-09-02 17:25:03 +00:00
|
|
|
/* image text info */
|
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
|
|
|
if (ibuf->metadata) {
|
2012-05-09 15:21:45 +00:00
|
|
|
png_text *metadata;
|
2015-01-31 17:15:43 +01:00
|
|
|
IDProperty *prop;
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2012-05-09 15:21:45 +00:00
|
|
|
int num_text = 0;
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2015-01-31 17:15:43 +01:00
|
|
|
for (prop = ibuf->metadata->data.group.first; prop; prop = prop->next) {
|
2015-02-01 04:19:43 +11:00
|
|
|
if (prop->type == IDP_STRING) {
|
2015-01-31 17:15:43 +01:00
|
|
|
num_text++;
|
2019-04-17 06:17:24 +02:00
|
|
|
}
|
2015-01-31 17:15:43 +01:00
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2012-05-09 15:21:45 +00:00
|
|
|
metadata = MEM_callocN(num_text * sizeof(png_text), "png_metadata");
|
2007-09-02 17:25:03 +00:00
|
|
|
num_text = 0;
|
2015-01-31 17:15:43 +01:00
|
|
|
for (prop = ibuf->metadata->data.group.first; prop; prop = prop->next) {
|
2015-02-01 04:19:43 +11:00
|
|
|
if (prop->type == IDP_STRING) {
|
2015-01-31 17:15:43 +01:00
|
|
|
metadata[num_text].compression = PNG_TEXT_COMPRESSION_NONE;
|
|
|
|
|
metadata[num_text].key = prop->name;
|
|
|
|
|
metadata[num_text].text = IDP_String(prop);
|
|
|
|
|
num_text++;
|
2019-04-17 06:17:24 +02:00
|
|
|
}
|
2015-01-31 17:15:43 +01:00
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
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
|
|
|
png_set_text(png_ptr, info_ptr, metadata, num_text);
|
|
|
|
|
MEM_freeN(metadata);
|
2007-09-02 17:25:03 +00:00
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2012-03-24 06:38:07 +00:00
|
|
|
if (ibuf->ppm[0] > 0.0 && ibuf->ppm[1] > 0.0) {
|
2011-05-13 14:27:12 +00:00
|
|
|
png_set_pHYs(png_ptr,
|
|
|
|
|
info_ptr,
|
|
|
|
|
(unsigned int)(ibuf->ppm[0] + 0.5),
|
|
|
|
|
(unsigned int)(ibuf->ppm[1] + 0.5),
|
|
|
|
|
PNG_RESOLUTION_METER);
|
|
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2012-07-07 22:51:57 +00:00
|
|
|
/* write the file header information */
|
2003-12-04 18:18:05 +00:00
|
|
|
png_write_info(png_ptr, info_ptr);
|
|
|
|
|
|
2012-12-30 13:01:47 +00:00
|
|
|
#ifdef __LITTLE_ENDIAN__
|
|
|
|
|
png_set_swap(png_ptr);
|
|
|
|
|
#endif
|
|
|
|
|
|
2012-07-07 22:51:57 +00:00
|
|
|
/* set the individual row-pointers to point at the correct offsets */
|
2012-12-30 13:01:47 +00:00
|
|
|
if (is_16bit) {
|
|
|
|
|
for (i = 0; i < ibuf->y; i++) {
|
|
|
|
|
row_pointers[ibuf->y - 1 - i] = (png_bytep)((unsigned short *)pixels16 +
|
2015-05-19 13:02:17 +05:00
|
|
|
(((size_t)i) * ibuf->x) * bytesperpixel);
|
2012-12-30 13:01:47 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
for (i = 0; i < ibuf->y; i++) {
|
|
|
|
|
row_pointers[ibuf->y - 1 - i] = (png_bytep)((unsigned char *)pixels +
|
2015-05-19 13:02:17 +05:00
|
|
|
(((size_t)i) * ibuf->x) * bytesperpixel *
|
|
|
|
|
sizeof(unsigned char));
|
2012-12-30 13:01:47 +00:00
|
|
|
}
|
2003-12-04 18:18:05 +00:00
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2012-07-07 22:51:57 +00:00
|
|
|
/* write out the entire image data in one call */
|
2003-12-04 18:18:05 +00:00
|
|
|
png_write_image(png_ptr, row_pointers);
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2012-07-07 22:51:57 +00:00
|
|
|
/* write the additional chunks to the PNG file (not really needed) */
|
2003-12-04 18:18:05 +00:00
|
|
|
png_write_end(png_ptr, info_ptr);
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2012-07-07 22:51:57 +00:00
|
|
|
/* clean up */
|
2019-04-23 11:01:30 +10:00
|
|
|
if (pixels) {
|
2012-12-30 13:01:47 +00:00
|
|
|
MEM_freeN(pixels);
|
2019-04-23 11:01:30 +10:00
|
|
|
}
|
|
|
|
|
if (pixels16) {
|
2012-12-30 13:01:47 +00:00
|
|
|
MEM_freeN(pixels16);
|
2019-04-23 11:01:30 +10:00
|
|
|
}
|
2003-12-04 18:18:05 +00:00
|
|
|
MEM_freeN(row_pointers);
|
|
|
|
|
png_destroy_write_struct(&png_ptr, &info_ptr);
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2003-12-04 18:18:05 +00:00
|
|
|
if (fp) {
|
|
|
|
|
fflush(fp);
|
|
|
|
|
fclose(fp);
|
|
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2020-08-08 11:02:11 +10:00
|
|
|
return 1;
|
2003-12-04 18:18:05 +00:00
|
|
|
}
|
2002-10-12 11:37:38 +00:00
|
|
|
|
2014-07-01 13:07:18 +06:00
|
|
|
static void imb_png_warning(png_structp UNUSED(png_ptr), png_const_charp message)
|
2014-06-30 21:06:23 +06:00
|
|
|
{
|
2014-07-05 05:26:32 +10:00
|
|
|
/* We suppress iCCP warnings. That's how Blender always used to behave,
|
2014-06-30 21:06:23 +06:00
|
|
|
* and with new libpng it became too much picky, giving a warning on
|
|
|
|
|
* the splash screen even.
|
|
|
|
|
*/
|
2020-11-06 15:56:03 +11:00
|
|
|
if ((G.debug & G_DEBUG) == 0 && STRPREFIX(message, "iCCP")) {
|
2014-06-30 21:06:23 +06:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
fprintf(stderr, "libpng warning: %s\n", message);
|
|
|
|
|
}
|
|
|
|
|
|
2014-07-01 13:07:18 +06:00
|
|
|
static void imb_png_error(png_structp UNUSED(png_ptr), png_const_charp message)
|
2014-06-30 21:06:23 +06:00
|
|
|
{
|
|
|
|
|
fprintf(stderr, "libpng error: %s\n", message);
|
|
|
|
|
}
|
|
|
|
|
|
2015-07-11 23:52:18 +10:00
|
|
|
ImBuf *imb_loadpng(const unsigned char *mem, size_t size, int flags, char colorspace[IM_MAX_SPACE])
|
2012-05-17 23:21:11 +00:00
|
|
|
{
|
2011-03-03 17:58:06 +00:00
|
|
|
struct ImBuf *ibuf = NULL;
|
2002-10-12 11:37:38 +00:00
|
|
|
png_structp png_ptr;
|
|
|
|
|
png_infop info_ptr;
|
2011-03-03 17:58:06 +00:00
|
|
|
unsigned char *pixels = NULL;
|
2012-05-09 15:20:08 +00:00
|
|
|
unsigned short *pixels16 = NULL;
|
2011-03-03 17:58:06 +00:00
|
|
|
png_bytepp row_pointers = NULL;
|
2002-10-12 11:37:38 +00:00
|
|
|
png_uint_32 width, height;
|
|
|
|
|
int bit_depth, color_type;
|
|
|
|
|
PNGReadStruct ps;
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2002-10-12 11:37:38 +00:00
|
|
|
unsigned char *from, *to;
|
2012-05-09 15:20:08 +00:00
|
|
|
unsigned short *from16;
|
|
|
|
|
float *to_float;
|
2018-01-14 14:19:57 +01:00
|
|
|
unsigned int channels;
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2020-11-11 16:14:06 +11:00
|
|
|
if (imb_is_a_png(mem, size) == 0) {
|
2020-08-08 11:02:11 +10:00
|
|
|
return NULL;
|
2019-04-23 11:01:30 +10:00
|
|
|
}
|
2019-04-17 06:17:24 +02: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
|
|
|
/* both 8 and 16 bit PNGs are default to standard byte colorspace */
|
|
|
|
|
colorspace_set_default_role(colorspace, IM_MAX_SPACE, COLOR_ROLE_DEFAULT_BYTE);
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2002-10-12 11:37:38 +00:00
|
|
|
png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
|
|
|
|
|
if (png_ptr == NULL) {
|
|
|
|
|
printf("Cannot png_create_read_struct\n");
|
2011-03-03 17:58:06 +00:00
|
|
|
return NULL;
|
2002-10-12 11:37:38 +00:00
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2014-06-30 21:06:23 +06:00
|
|
|
png_set_error_fn(png_ptr, NULL, imb_png_error, imb_png_warning);
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2002-10-12 11:37:38 +00:00
|
|
|
info_ptr = png_create_info_struct(png_ptr);
|
|
|
|
|
if (info_ptr == NULL) {
|
2018-06-17 17:04:54 +02:00
|
|
|
png_destroy_read_struct(&png_ptr, (png_infopp)NULL, (png_infopp)NULL);
|
2002-10-12 11:37:38 +00:00
|
|
|
printf("Cannot png_create_info_struct\n");
|
2011-03-03 17:58:06 +00:00
|
|
|
return NULL;
|
2002-10-12 11:37:38 +00:00
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2010-09-15 06:43:36 +00:00
|
|
|
ps.size = size; /* XXX, 4gig limit! */
|
2002-10-12 11:37:38 +00:00
|
|
|
ps.data = mem;
|
|
|
|
|
ps.seek = 0;
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2002-10-12 11:37:38 +00:00
|
|
|
png_set_read_fn(png_ptr, (void *)&ps, ReadData);
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2002-10-12 11:37:38 +00:00
|
|
|
if (setjmp(png_jmpbuf(png_ptr))) {
|
2020-05-13 20:12:25 +02:00
|
|
|
/* On error jump here, and free any resources. */
|
2002-10-12 11:37:38 +00:00
|
|
|
png_destroy_read_struct(&png_ptr, &info_ptr, (png_infopp)NULL);
|
2019-04-23 11:01:30 +10:00
|
|
|
if (pixels) {
|
2002-10-12 11:37:38 +00:00
|
|
|
MEM_freeN(pixels);
|
2019-04-23 11:01:30 +10:00
|
|
|
}
|
|
|
|
|
if (pixels16) {
|
2012-05-09 15:20:08 +00:00
|
|
|
MEM_freeN(pixels16);
|
2019-04-23 11:01:30 +10:00
|
|
|
}
|
|
|
|
|
if (row_pointers) {
|
2002-10-12 11:37:38 +00:00
|
|
|
MEM_freeN(row_pointers);
|
2019-04-23 11:01:30 +10:00
|
|
|
}
|
|
|
|
|
if (ibuf) {
|
2002-10-12 11:37:38 +00:00
|
|
|
IMB_freeImBuf(ibuf);
|
2019-04-23 11:01:30 +10:00
|
|
|
}
|
2011-03-03 17:58:06 +00:00
|
|
|
return NULL;
|
2002-10-12 11:37:38 +00:00
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2002-10-12 11:37:38 +00:00
|
|
|
// png_set_sig_bytes(png_ptr, 8);
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2002-10-12 11:37:38 +00:00
|
|
|
png_read_info(png_ptr, info_ptr);
|
2018-06-17 17:04:54 +02:00
|
|
|
png_get_IHDR(png_ptr, info_ptr, &width, &height, &bit_depth, &color_type, NULL, NULL, NULL);
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2018-01-14 14:19:57 +01:00
|
|
|
channels = png_get_channels(png_ptr, info_ptr);
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2012-04-28 06:31:57 +00:00
|
|
|
switch (color_type) {
|
2012-05-09 15:21:45 +00:00
|
|
|
case PNG_COLOR_TYPE_RGB:
|
|
|
|
|
case PNG_COLOR_TYPE_RGB_ALPHA:
|
|
|
|
|
break;
|
|
|
|
|
case PNG_COLOR_TYPE_PALETTE:
|
|
|
|
|
png_set_palette_to_rgb(png_ptr);
|
|
|
|
|
if (png_get_valid(png_ptr, info_ptr, PNG_INFO_tRNS)) {
|
2018-01-14 14:19:57 +01:00
|
|
|
channels = 4;
|
2012-05-09 15:21:45 +00:00
|
|
|
}
|
|
|
|
|
else {
|
2018-01-14 14:19:57 +01:00
|
|
|
channels = 3;
|
2012-05-09 15:21:45 +00:00
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
case PNG_COLOR_TYPE_GRAY:
|
|
|
|
|
case PNG_COLOR_TYPE_GRAY_ALPHA:
|
|
|
|
|
if (bit_depth < 8) {
|
|
|
|
|
png_set_expand(png_ptr);
|
|
|
|
|
bit_depth = 8;
|
2015-07-03 18:23:55 +02:00
|
|
|
if (png_get_valid(png_ptr, info_ptr, PNG_INFO_tRNS)) {
|
|
|
|
|
/* PNG_COLOR_TYPE_GRAY may also have alpha 'values', like with palette. */
|
2018-01-14 14:19:57 +01:00
|
|
|
channels = 2;
|
2019-04-17 06:17:24 +02:00
|
|
|
}
|
2015-07-03 18:23:55 +02:00
|
|
|
}
|
2012-05-09 15:21:45 +00:00
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
printf("PNG format not supported\n");
|
|
|
|
|
longjmp(png_jmpbuf(png_ptr), 1);
|
2002-10-12 11:37:38 +00:00
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2018-01-14 14:19:57 +01:00
|
|
|
ibuf = IMB_allocImBuf(width, height, 8 * channels, 0);
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2002-10-12 11:37:38 +00:00
|
|
|
if (ibuf) {
|
2015-07-13 13:58:17 +02:00
|
|
|
ibuf->ftype = IMB_FTYPE_PNG;
|
2019-04-23 11:01:30 +10:00
|
|
|
if (bit_depth == 16) {
|
2015-07-13 13:58:17 +02:00
|
|
|
ibuf->foptions.flag |= PNG_16BIT;
|
2019-04-23 11:01:30 +10:00
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2012-05-09 15:21:45 +00:00
|
|
|
if (png_get_valid(png_ptr, info_ptr, PNG_INFO_pHYs)) {
|
2011-05-13 14:27:12 +00:00
|
|
|
int unit_type;
|
2011-05-19 11:54:03 +00:00
|
|
|
png_uint_32 xres, yres;
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2017-05-19 22:18:54 +10:00
|
|
|
if (png_get_pHYs(png_ptr, info_ptr, &xres, &yres, &unit_type)) {
|
2012-05-09 15:21:45 +00:00
|
|
|
if (unit_type == PNG_RESOLUTION_METER) {
|
|
|
|
|
ibuf->ppm[0] = xres;
|
|
|
|
|
ibuf->ppm[1] = yres;
|
2019-04-17 06:17:24 +02:00
|
|
|
}
|
|
|
|
|
}
|
2012-05-09 15:21:45 +00:00
|
|
|
}
|
2017-05-19 22:18:54 +10:00
|
|
|
}
|
2011-05-13 14:27:12 +00:00
|
|
|
else {
|
2002-10-12 11:37:38 +00:00
|
|
|
printf("Couldn't allocate memory for PNG image\n");
|
|
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2002-10-12 11:37:38 +00:00
|
|
|
if (ibuf && ((flags & IB_test) == 0)) {
|
2012-05-09 15:20:08 +00:00
|
|
|
if (bit_depth == 16) {
|
|
|
|
|
imb_addrectfloatImBuf(ibuf);
|
|
|
|
|
png_set_swap(png_ptr);
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2018-01-14 14:19:57 +01:00
|
|
|
pixels16 = imb_alloc_pixels(ibuf->x, ibuf->y, channels, sizeof(png_uint_16), "pixels");
|
|
|
|
|
if (pixels16 == NULL || ibuf->rect_float == NULL) {
|
2012-05-09 15:20:08 +00:00
|
|
|
printf("Cannot allocate pixels array\n");
|
|
|
|
|
longjmp(png_jmpbuf(png_ptr), 1);
|
|
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2012-07-07 22:51:57 +00:00
|
|
|
/* allocate memory for an array of row-pointers */
|
2018-01-14 14:19:57 +01:00
|
|
|
row_pointers = (png_bytepp)MEM_mallocN((size_t)ibuf->y * sizeof(png_uint_16p),
|
|
|
|
|
"row_pointers");
|
2012-05-09 15:20:08 +00:00
|
|
|
if (row_pointers == NULL) {
|
|
|
|
|
printf("Cannot allocate row-pointers array\n");
|
|
|
|
|
longjmp(png_jmpbuf(png_ptr), 1);
|
|
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2012-07-07 22:51:57 +00:00
|
|
|
/* set the individual row-pointers to point at the correct offsets */
|
2018-01-14 14:19:57 +01:00
|
|
|
for (size_t i = 0; i < ibuf->y; i++) {
|
2012-05-09 15:20:08 +00:00
|
|
|
row_pointers[ibuf->y - 1 - i] = (png_bytep)((png_uint_16 *)pixels16 +
|
2018-01-14 14:19:57 +01:00
|
|
|
(i * ibuf->x) * channels);
|
2012-05-09 15:20:08 +00:00
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2012-05-09 15:20:08 +00:00
|
|
|
png_read_image(png_ptr, row_pointers);
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2012-07-07 22:51:57 +00:00
|
|
|
/* copy image data */
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2012-05-09 15:20:08 +00:00
|
|
|
to_float = ibuf->rect_float;
|
|
|
|
|
from16 = pixels16;
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2018-01-14 14:19:57 +01:00
|
|
|
switch (channels) {
|
2012-05-09 15:20:08 +00:00
|
|
|
case 4:
|
2018-01-14 14:19:57 +01:00
|
|
|
for (size_t i = (size_t)ibuf->x * (size_t)ibuf->y; i > 0; i--) {
|
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
|
|
|
to_float[0] = from16[0] / 65535.0;
|
|
|
|
|
to_float[1] = from16[1] / 65535.0;
|
|
|
|
|
to_float[2] = from16[2] / 65535.0;
|
|
|
|
|
to_float[3] = from16[3] / 65535.0;
|
2012-05-09 15:20:08 +00:00
|
|
|
to_float += 4;
|
|
|
|
|
from16 += 4;
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
case 3:
|
2018-01-14 14:19:57 +01:00
|
|
|
for (size_t i = (size_t)ibuf->x * (size_t)ibuf->y; i > 0; i--) {
|
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
|
|
|
to_float[0] = from16[0] / 65535.0;
|
|
|
|
|
to_float[1] = from16[1] / 65535.0;
|
|
|
|
|
to_float[2] = from16[2] / 65535.0;
|
|
|
|
|
to_float[3] = 1.0;
|
2012-05-09 15:20:08 +00:00
|
|
|
to_float += 4;
|
|
|
|
|
from16 += 3;
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
case 2:
|
2018-01-14 14:19:57 +01:00
|
|
|
for (size_t i = (size_t)ibuf->x * (size_t)ibuf->y; i > 0; i--) {
|
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
|
|
|
to_float[0] = to_float[1] = to_float[2] = from16[0] / 65535.0;
|
|
|
|
|
to_float[3] = from16[1] / 65535.0;
|
2012-05-09 15:20:08 +00:00
|
|
|
to_float += 4;
|
|
|
|
|
from16 += 2;
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
case 1:
|
2018-01-14 14:19:57 +01:00
|
|
|
for (size_t i = (size_t)ibuf->x * (size_t)ibuf->y; i > 0; i--) {
|
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
|
|
|
to_float[0] = to_float[1] = to_float[2] = from16[0] / 65535.0;
|
|
|
|
|
to_float[3] = 1.0;
|
2012-05-09 15:20:08 +00:00
|
|
|
to_float += 4;
|
|
|
|
|
from16++;
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
}
|
2002-10-12 11:37:38 +00:00
|
|
|
}
|
2012-05-09 15:20:08 +00:00
|
|
|
else {
|
|
|
|
|
imb_addrectImBuf(ibuf);
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2018-01-14 14:19:57 +01:00
|
|
|
pixels = imb_alloc_pixels(ibuf->x, ibuf->y, channels, sizeof(unsigned char), "pixels");
|
|
|
|
|
if (pixels == NULL || ibuf->rect == NULL) {
|
2012-05-09 15:20:08 +00:00
|
|
|
printf("Cannot allocate pixels array\n");
|
|
|
|
|
longjmp(png_jmpbuf(png_ptr), 1);
|
2002-10-12 11:37:38 +00:00
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2012-07-07 22:51:57 +00:00
|
|
|
/* allocate memory for an array of row-pointers */
|
2018-01-14 14:19:57 +01:00
|
|
|
row_pointers = (png_bytepp)MEM_mallocN((size_t)ibuf->y * sizeof(png_bytep), "row_pointers");
|
2012-05-09 15:20:08 +00:00
|
|
|
if (row_pointers == NULL) {
|
|
|
|
|
printf("Cannot allocate row-pointers array\n");
|
|
|
|
|
longjmp(png_jmpbuf(png_ptr), 1);
|
2002-10-12 11:37:38 +00:00
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2012-07-07 22:51:57 +00:00
|
|
|
/* set the individual row-pointers to point at the correct offsets */
|
2018-01-14 14:19:57 +01:00
|
|
|
for (int i = 0; i < ibuf->y; i++) {
|
2012-05-09 15:21:45 +00:00
|
|
|
row_pointers[ibuf->y - 1 - i] = (png_bytep)(
|
2018-01-14 14:19:57 +01:00
|
|
|
(unsigned char *)pixels + (((size_t)i) * ibuf->x) * channels * sizeof(unsigned char));
|
2002-10-12 11:37:38 +00:00
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2012-05-09 15:20:08 +00:00
|
|
|
png_read_image(png_ptr, row_pointers);
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2012-07-07 22:51:57 +00:00
|
|
|
/* copy image data */
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2012-05-09 15:20:08 +00:00
|
|
|
to = (unsigned char *)ibuf->rect;
|
|
|
|
|
from = pixels;
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2018-01-14 14:19:57 +01:00
|
|
|
switch (channels) {
|
2012-05-09 15:20:08 +00:00
|
|
|
case 4:
|
2018-01-14 14:19:57 +01:00
|
|
|
for (size_t i = (size_t)ibuf->x * (size_t)ibuf->y; i > 0; i--) {
|
2012-05-09 15:20:08 +00:00
|
|
|
to[0] = from[0];
|
|
|
|
|
to[1] = from[1];
|
|
|
|
|
to[2] = from[2];
|
|
|
|
|
to[3] = from[3];
|
|
|
|
|
to += 4;
|
|
|
|
|
from += 4;
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
case 3:
|
2018-01-14 14:19:57 +01:00
|
|
|
for (size_t i = (size_t)ibuf->x * (size_t)ibuf->y; i > 0; i--) {
|
2012-05-09 15:20:08 +00:00
|
|
|
to[0] = from[0];
|
|
|
|
|
to[1] = from[1];
|
|
|
|
|
to[2] = from[2];
|
|
|
|
|
to[3] = 0xff;
|
|
|
|
|
to += 4;
|
|
|
|
|
from += 3;
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
case 2:
|
2018-01-14 14:19:57 +01:00
|
|
|
for (size_t i = (size_t)ibuf->x * (size_t)ibuf->y; i > 0; i--) {
|
2012-05-09 15:20:08 +00:00
|
|
|
to[0] = to[1] = to[2] = from[0];
|
|
|
|
|
to[3] = from[1];
|
|
|
|
|
to += 4;
|
|
|
|
|
from += 2;
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
case 1:
|
2018-01-14 14:19:57 +01:00
|
|
|
for (size_t i = (size_t)ibuf->x * (size_t)ibuf->y; i > 0; i--) {
|
2012-05-09 15:20:08 +00:00
|
|
|
to[0] = to[1] = to[2] = from[0];
|
|
|
|
|
to[3] = 0xff;
|
|
|
|
|
to += 4;
|
|
|
|
|
from++;
|
|
|
|
|
}
|
|
|
|
|
break;
|
2002-10-12 11:37:38 +00:00
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
}
|
|
|
|
|
|
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
|
|
|
if (flags & IB_metadata) {
|
2012-05-09 15:21:45 +00:00
|
|
|
png_text *text_chunks;
|
2007-09-02 17:25:03 +00:00
|
|
|
int count = png_get_text(png_ptr, info_ptr, &text_chunks, NULL);
|
2018-04-05 16:27:15 +02:00
|
|
|
IMB_metadata_ensure(&ibuf->metadata);
|
2018-01-14 14:19:57 +01:00
|
|
|
for (int i = 0; i < count; i++) {
|
2018-04-05 16:27:15 +02:00
|
|
|
IMB_metadata_set_field(ibuf->metadata, text_chunks[i].key, text_chunks[i].text);
|
2012-05-09 15:20:08 +00:00
|
|
|
ibuf->flags |= IB_metadata;
|
2007-09-02 17:25:03 +00:00
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
}
|
|
|
|
|
|
2002-10-12 11:37:38 +00:00
|
|
|
png_read_end(png_ptr, info_ptr);
|
|
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2012-07-07 22:51:57 +00:00
|
|
|
/* clean up */
|
2019-04-23 11:01:30 +10:00
|
|
|
if (pixels) {
|
2012-05-09 15:20:08 +00:00
|
|
|
MEM_freeN(pixels);
|
2019-04-23 11:01:30 +10:00
|
|
|
}
|
|
|
|
|
if (pixels16) {
|
2012-05-09 15:20:08 +00:00
|
|
|
MEM_freeN(pixels16);
|
2019-04-23 11:01:30 +10:00
|
|
|
}
|
|
|
|
|
if (row_pointers) {
|
2012-11-05 10:32:47 +00:00
|
|
|
MEM_freeN(row_pointers);
|
2019-04-23 11:01:30 +10:00
|
|
|
}
|
2002-10-12 11:37:38 +00:00
|
|
|
png_destroy_read_struct(&png_ptr, &info_ptr, (png_infopp)NULL);
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2020-08-08 11:02:11 +10:00
|
|
|
return ibuf;
|
2002-10-12 11:37:38 +00:00
|
|
|
}
|