This repository has been archived on 2023-10-09. You can view files and clone it, but cannot push or open issues or pull requests.
Files
blender-archive/source/blender/imbuf/intern/png.c

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

830 lines
24 KiB
C
Raw Normal View History

/*
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
* 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.
*/
/** \file
* \ingroup imbuf
*
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"
#include "BLI_fileops.h"
#include "BLI_math.h"
#include "BLI_utildefines.h"
#include "BKE_global.h"
#include "BKE_idprop.h"
#include "DNA_ID.h" /* ID property definitions. */
#include "MEM_guardedalloc.h"
2002-10-12 11:37:38 +00:00
#include "IMB_imbuf.h"
#include "IMB_imbuf_types.h"
2002-10-12 11:37:38 +00:00
#include "IMB_allocimbuf.h"
#include "IMB_filetype.h"
#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"
typedef struct PNGReadStruct {
const unsigned char *data;
unsigned int size;
unsigned int seek;
2012-04-21 13:37:26 +00:00
} PNGReadStruct;
2002-10-12 11:37:38 +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
BLI_INLINE unsigned short UPSAMPLE_8_TO_16(const unsigned char _val)
{
return (_val << 8) + _val;
}
bool imb_is_a_png(const unsigned char *mem, size_t size)
2002-10-12 11:37:38 +00: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
#if (PNG_LIBPNG_VER_MAJOR == 1) && (PNG_LIBPNG_VER_MINOR == 2)
/* Older version of libpng doesn't use const pointer to memory. */
ok = !png_sig_cmp((png_bytep)mem, 0, num_to_check);
#else
ok = !png_sig_cmp(mem, 0, num_to_check);
#endif
return ok;
2002-10-12 11:37:38 +00:00
}
static void Flush(png_structp png_ptr)
{
(void)png_ptr;
2002-10-12 11:37:38 +00:00
}
static void WriteData(png_structp png_ptr, png_bytep data, png_size_t length)
{
ImBuf *ibuf = (ImBuf *)png_get_io_ptr(png_ptr);
/* if buffer is too small increase it. */
while (ibuf->encodedsize + length > ibuf->encodedbuffersize) {
imb_enlargeencodedbufferImBuf(ibuf);
}
2002-10-12 11:37:38 +00:00
memcpy(ibuf->encodedbuffer + ibuf->encodedsize, data, length);
ibuf->encodedsize += length;
}
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);
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;
}
}
2002-10-12 11:37:38 +00:00
printf("Reached EOF while decoding PNG\n");
longjmp(png_jmpbuf(png_ptr), 1);
}
static float channel_colormanage_noop(float value)
{
return value;
}
/* wrap to avoid macro calling functions multiple times */
BLI_INLINE unsigned short ftoshort(float val)
{
return unit_float_to_ushort_clamp(val);
}
bool imb_savepng(struct ImBuf *ibuf, const char *filepath, int flags)
{
png_structp png_ptr;
png_infop info_ptr;
unsigned char *pixels = NULL;
unsigned char *from, *to;
unsigned short *pixels16 = NULL, *to16;
Alpha premul pipeline cleanup This assumptions are now made: - Internally float buffers are always linear alpha-premul colors - Readers should worry about delivering float buffers with that assumptions. - There's an input image setting to say whether it's stored with straight/premul alpha on the disk. - Byte buffers are now assumed have straight alpha, readers should deliver straight alpha. Some implementation details: - Removed scene's color unpremultiply setting, which was very much confusing and was wrong for default settings. Now all renderers assumes to deliver premultiplied alpha. - IMB_buffer_byte_from_float will now linearize alpha when converting from buffer. - Sequencer's effects were changed to assume bytes have got straight alpha. Most of effects will work with bytes still, however for glow it was more tricky to avoid data loss, so there's a commented out glow implementation which converts byte buffer to floats first, operates on floats and returns bytes back. It's slower and not sure if it should actually be used -- who're using glow on alpha anyway? - Sequencer modifiers should also be working nice with straight bytes now. - GLSL preview will predivide float textures to make nice shading, shading with byte textures worked nice (GLSL was assuming straight alpha). - Blender Internal will set alpha=1 to the whole sky. The same happens in Cycles and there's no way to avoid this -- sky is neither straight nor premul and doesn't fit color pipeline well. - Straight alpha mode for render result was also eliminated. - Conversion to correct alpha need to be done before linearizing float buffer. - TIFF will now load and save files with proper alpha mode setting in file meta data header. - Remove Use Alpha from texture mapping and replaced with image datablock setting. Behaves much more predictable and clear from code point of view and solves possible regressions when non-premultiplied images were used as textures with ignoring alpha channel.
2012-12-31 13:52:13 +00:00
float *from_float, from_straight[4];
png_bytepp row_pointers = NULL;
int i, bytesperpixel, color_type = PNG_COLOR_TYPE_GRAY;
FILE *fp = NULL;
bool is_16bit = (ibuf->foptions.flag & PNG_16BIT) != 0;
bool has_float = (ibuf->rect_float != NULL);
int channels_in_float = ibuf->channels ? ibuf->channels : 4;
float (*chanel_colormanage_cb)(float);
size_t num_bytes;
/* use the jpeg quality setting for compression */
int compression;
compression = (int)(((float)(ibuf->foptions.quality) / 11.1111f));
2012-05-09 15:21:45 +00:00
compression = compression < 0 ? 0 : (compression > 9 ? 9 : compression);
if (ibuf->float_colorspace || (ibuf->colormanage_flag & IMB_COLORMANAGE_IS_DATA)) {
/* 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;
}
/* for prints */
2019-04-23 11:01:30 +10:00
if (flags & IB_mem) {
filepath = "<memory>";
2019-04-23 11:01:30 +10:00
}
bytesperpixel = (ibuf->planes + 7) >> 3;
if ((bytesperpixel > 4) || (bytesperpixel == 2)) {
printf(
"imb_savepng: Unsupported bytes per pixel: %d for file: '%s'\n", bytesperpixel, filepath);
return 0;
}
png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
if (png_ptr == NULL) {
printf("imb_savepng: Cannot png_create_write_struct for file: '%s'\n", filepath);
return 0;
}
info_ptr = png_create_info_struct(png_ptr);
if (info_ptr == NULL) {
png_destroy_write_struct(&png_ptr, (png_infopp)NULL);
printf("imb_savepng: Cannot png_create_info_struct for file: '%s'\n", filepath);
return 0;
}
2012-07-07 22:51:57 +00:00
/* copy image data */
num_bytes = ((size_t)ibuf->x) * ibuf->y * bytesperpixel;
2019-04-23 11:01:30 +10:00
if (is_16bit) {
pixels16 = MEM_mallocN(num_bytes * sizeof(unsigned short), "png 16bit pixels");
2019-04-23 11:01:30 +10:00
}
else {
pixels = MEM_mallocN(num_bytes * sizeof(unsigned char), "png 8bit pixels");
2019-04-23 11:01:30 +10:00
}
if (pixels == NULL && pixels16 == NULL) {
printf(
"imb_savepng: Cannot allocate pixels array of %dx%d, %d bytes per pixel for file: "
"'%s'\n",
ibuf->x,
ibuf->y,
bytesperpixel,
filepath);
}
/* 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) {
printf("imb_savepng: Cannot allocate row-pointers array for file '%s'\n", filepath);
}
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);
}
return 0;
}
from = (unsigned char *)ibuf->rect;
to = pixels;
from_float = ibuf->rect_float;
to16 = pixels16;
switch (bytesperpixel) {
2012-05-09 15:21:45 +00:00
case 4:
color_type = PNG_COLOR_TYPE_RGBA;
if (is_16bit) {
if (has_float) {
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;
}
}
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;
}
}
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++;
}
}
}
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;
}
}
}
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
}
}
2012-05-09 15:21:45 +00:00
break;
case 3:
color_type = PNG_COLOR_TYPE_RGB;
if (is_16bit) {
if (has_float) {
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;
}
}
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;
}
}
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++;
}
}
}
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;
}
}
}
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
}
}
2012-05-09 15:21:45 +00:00
break;
case 1:
color_type = PNG_COLOR_TYPE_GRAY;
if (is_16bit) {
if (has_float) {
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]);
to16[0] = ftoshort(IMB_colormanagement_get_luminance(rgb));
to16++;
from_float += 4;
}
}
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]);
to16[0] = ftoshort(IMB_colormanagement_get_luminance(rgb));
to16++;
from_float += 3;
}
}
else {
for (i = ibuf->x * ibuf->y; i > 0; i--) {
to16[0] = ftoshort(chanel_colormanage_cb(from_float[0]));
to16++;
from_float++;
}
}
}
else {
for (i = ibuf->x * ibuf->y; i > 0; i--) {
to16[0] = UPSAMPLE_8_TO_16(from[0]);
to16++;
from += 4;
}
}
}
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
}
}
2012-05-09 15:21:45 +00:00
break;
}
if (flags & IB_mem) {
2012-07-07 22:51:57 +00:00
/* create image in memory */
imb_addencodedbufferImBuf(ibuf);
ibuf->encodedsize = 0;
png_set_write_fn(png_ptr, (png_voidp)ibuf, WriteData, Flush);
}
else {
fp = BLI_fopen(filepath, "wb");
if (!fp) {
png_destroy_write_struct(&png_ptr, &info_ptr);
2019-04-23 11:01:30 +10:00
if (pixels) {
MEM_freeN(pixels);
2019-04-23 11:01:30 +10:00
}
if (pixels16) {
MEM_freeN(pixels16);
2019-04-23 11:01:30 +10:00
}
MEM_freeN(row_pointers);
printf("imb_savepng: Cannot open file for writing: '%s'\n", filepath);
return 0;
}
png_init_io(png_ptr, fp);
}
2012-03-09 18:28:30 +00:00
#if 0
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
png_set_compression_level(png_ptr, compression);
2012-07-07 22:51:57 +00:00
/* png image settings */
png_set_IHDR(png_ptr,
info_ptr,
ibuf->x,
ibuf->y,
is_16bit ? 16 : 8,
color_type,
PNG_INTERLACE_NONE,
PNG_COMPRESSION_TYPE_DEFAULT,
PNG_FILTER_TYPE_DEFAULT);
/* image text info */
if (ibuf->metadata) {
2012-05-09 15:21:45 +00:00
png_text *metadata;
IDProperty *prop;
2012-05-09 15:21:45 +00:00
int num_text = 0;
for (prop = ibuf->metadata->data.group.first; prop; prop = prop->next) {
2015-02-01 04:19:43 +11:00
if (prop->type == IDP_STRING) {
num_text++;
}
}
2012-05-09 15:21:45 +00:00
metadata = MEM_callocN(num_text * sizeof(png_text), "png_metadata");
num_text = 0;
for (prop = ibuf->metadata->data.group.first; prop; prop = prop->next) {
2015-02-01 04:19:43 +11:00
if (prop->type == IDP_STRING) {
metadata[num_text].compression = PNG_TEXT_COMPRESSION_NONE;
metadata[num_text].key = prop->name;
metadata[num_text].text = IDP_String(prop);
num_text++;
}
}
png_set_text(png_ptr, info_ptr, metadata, num_text);
MEM_freeN(metadata);
}
if (ibuf->ppm[0] > 0.0 && ibuf->ppm[1] > 0.0) {
png_set_pHYs(png_ptr,
info_ptr,
(unsigned int)(ibuf->ppm[0] + 0.5),
(unsigned int)(ibuf->ppm[1] + 0.5),
PNG_RESOLUTION_METER);
}
2012-07-07 22:51:57 +00:00
/* write the file header information */
png_write_info(png_ptr, info_ptr);
#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 */
if (is_16bit) {
for (i = 0; i < ibuf->y; i++) {
row_pointers[ibuf->y - 1 - i] = (png_bytep)((unsigned short *)pixels16 +
(((size_t)i) * ibuf->x) * bytesperpixel);
}
}
else {
for (i = 0; i < ibuf->y; i++) {
row_pointers[ibuf->y - 1 - i] = (png_bytep)((unsigned char *)pixels +
(((size_t)i) * ibuf->x) * bytesperpixel *
sizeof(unsigned char));
}
}
2012-07-07 22:51:57 +00:00
/* write out the entire image data in one call */
png_write_image(png_ptr, row_pointers);
2012-07-07 22:51:57 +00:00
/* write the additional chunks to the PNG file (not really needed) */
png_write_end(png_ptr, info_ptr);
2012-07-07 22:51:57 +00:00
/* clean up */
2019-04-23 11:01:30 +10:00
if (pixels) {
MEM_freeN(pixels);
2019-04-23 11:01:30 +10:00
}
if (pixels16) {
MEM_freeN(pixels16);
2019-04-23 11:01:30 +10:00
}
MEM_freeN(row_pointers);
png_destroy_write_struct(&png_ptr, &info_ptr);
if (fp) {
fflush(fp);
fclose(fp);
}
return 1;
}
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-07-05 05:26:32 +10:00
/* We suppress iCCP warnings. That's how Blender always used to behave,
* and with new libpng it became too much picky, giving a warning on
* the splash screen even.
*/
if ((G.debug & G_DEBUG) == 0 && STRPREFIX(message, "iCCP")) {
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)
{
fprintf(stderr, "libpng error: %s\n", message);
}
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
{
struct ImBuf *ibuf = NULL;
2002-10-12 11:37:38 +00:00
png_structp png_ptr;
png_infop info_ptr;
unsigned char *pixels = NULL;
unsigned short *pixels16 = NULL;
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;
2002-10-12 11:37:38 +00:00
unsigned char *from, *to;
unsigned short *from16;
float *to_float;
unsigned int channels;
if (imb_is_a_png(mem, size) == 0) {
return NULL;
2019-04-23 11:01:30 +10: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);
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");
return NULL;
2002-10-12 11:37:38 +00:00
}
png_set_error_fn(png_ptr, NULL, imb_png_error, imb_png_warning);
2002-10-12 11:37:38 +00:00
info_ptr = png_create_info_struct(png_ptr);
if (info_ptr == NULL) {
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");
return NULL;
2002-10-12 11:37:38 +00:00
}
ps.size = size; /* XXX, 4gig limit! */
2002-10-12 11:37:38 +00:00
ps.data = mem;
ps.seek = 0;
2002-10-12 11:37:38 +00:00
png_set_read_fn(png_ptr, (void *)&ps, ReadData);
2002-10-12 11:37:38 +00:00
if (setjmp(png_jmpbuf(png_ptr))) {
/* 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) {
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
}
return NULL;
2002-10-12 11:37:38 +00:00
}
2002-10-12 11:37:38 +00:00
// png_set_sig_bytes(png_ptr, 8);
2002-10-12 11:37:38 +00:00
png_read_info(png_ptr, info_ptr);
png_get_IHDR(png_ptr, info_ptr, &width, &height, &bit_depth, &color_type, NULL, NULL, NULL);
channels = png_get_channels(png_ptr, info_ptr);
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)) {
channels = 4;
2012-05-09 15:21:45 +00:00
}
else {
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;
if (png_get_valid(png_ptr, info_ptr, PNG_INFO_tRNS)) {
/* PNG_COLOR_TYPE_GRAY may also have alpha 'values', like with palette. */
channels = 2;
}
}
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
}
ibuf = IMB_allocImBuf(width, height, 8 * channels, 0);
2002-10-12 11:37:38 +00:00
if (ibuf) {
ibuf->ftype = IMB_FTYPE_PNG;
2019-04-23 11:01:30 +10:00
if (bit_depth == 16) {
ibuf->foptions.flag |= PNG_16BIT;
2019-04-23 11:01:30 +10:00
}
2012-05-09 15:21:45 +00:00
if (png_get_valid(png_ptr, info_ptr, PNG_INFO_pHYs)) {
int unit_type;
2011-05-19 11:54:03 +00:00
png_uint_32 xres, yres;
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;
}
}
2012-05-09 15:21:45 +00:00
}
}
else {
2002-10-12 11:37:38 +00:00
printf("Couldn't allocate memory for PNG image\n");
}
2002-10-12 11:37:38 +00:00
if (ibuf && ((flags & IB_test) == 0)) {
if (bit_depth == 16) {
imb_addrectfloatImBuf(ibuf);
png_set_swap(png_ptr);
pixels16 = imb_alloc_pixels(ibuf->x, ibuf->y, channels, sizeof(png_uint_16), "pixels");
if (pixels16 == NULL || ibuf->rect_float == NULL) {
printf("Cannot allocate pixels array\n");
longjmp(png_jmpbuf(png_ptr), 1);
}
2012-07-07 22:51:57 +00:00
/* allocate memory for an array of row-pointers */
row_pointers = (png_bytepp)MEM_mallocN((size_t)ibuf->y * sizeof(png_uint_16p),
"row_pointers");
if (row_pointers == NULL) {
printf("Cannot allocate row-pointers array\n");
longjmp(png_jmpbuf(png_ptr), 1);
}
2012-07-07 22:51:57 +00:00
/* set the individual row-pointers to point at the correct offsets */
for (size_t i = 0; i < ibuf->y; i++) {
row_pointers[ibuf->y - 1 - i] = (png_bytep)((png_uint_16 *)pixels16 +
(i * ibuf->x) * channels);
}
png_read_image(png_ptr, row_pointers);
2012-07-07 22:51:57 +00:00
/* copy image data */
to_float = ibuf->rect_float;
from16 = pixels16;
switch (channels) {
case 4:
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;
to_float += 4;
from16 += 4;
}
break;
case 3:
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;
to_float += 4;
from16 += 3;
}
break;
case 2:
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;
to_float += 4;
from16 += 2;
}
break;
case 1:
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;
to_float += 4;
from16++;
}
break;
}
2002-10-12 11:37:38 +00:00
}
else {
imb_addrectImBuf(ibuf);
pixels = imb_alloc_pixels(ibuf->x, ibuf->y, channels, sizeof(unsigned char), "pixels");
if (pixels == NULL || ibuf->rect == NULL) {
printf("Cannot allocate pixels array\n");
longjmp(png_jmpbuf(png_ptr), 1);
2002-10-12 11:37:38 +00:00
}
2012-07-07 22:51:57 +00:00
/* allocate memory for an array of row-pointers */
row_pointers = (png_bytepp)MEM_mallocN((size_t)ibuf->y * sizeof(png_bytep), "row_pointers");
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
}
2012-07-07 22:51:57 +00:00
/* set the individual row-pointers to point at the correct offsets */
for (int i = 0; i < ibuf->y; i++) {
2012-05-09 15:21:45 +00:00
row_pointers[ibuf->y - 1 - i] = (png_bytep)(
(unsigned char *)pixels + (((size_t)i) * ibuf->x) * channels * sizeof(unsigned char));
2002-10-12 11:37:38 +00:00
}
png_read_image(png_ptr, row_pointers);
2012-07-07 22:51:57 +00:00
/* copy image data */
to = (unsigned char *)ibuf->rect;
from = pixels;
switch (channels) {
case 4:
for (size_t i = (size_t)ibuf->x * (size_t)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;
}
break;
case 3:
for (size_t i = (size_t)ibuf->x * (size_t)ibuf->y; i > 0; i--) {
to[0] = from[0];
to[1] = from[1];
to[2] = from[2];
to[3] = 0xff;
to += 4;
from += 3;
}
break;
case 2:
for (size_t i = (size_t)ibuf->x * (size_t)ibuf->y; i > 0; i--) {
to[0] = to[1] = to[2] = from[0];
to[3] = from[1];
to += 4;
from += 2;
}
break;
case 1:
for (size_t i = (size_t)ibuf->x * (size_t)ibuf->y; i > 0; i--) {
to[0] = to[1] = to[2] = from[0];
to[3] = 0xff;
to += 4;
from++;
}
break;
2002-10-12 11:37:38 +00:00
}
}
if (flags & IB_metadata) {
2012-05-09 15:21:45 +00:00
png_text *text_chunks;
int count = png_get_text(png_ptr, info_ptr, &text_chunks, NULL);
IMB_metadata_ensure(&ibuf->metadata);
for (int i = 0; i < count; i++) {
IMB_metadata_set_field(ibuf->metadata, text_chunks[i].key, text_chunks[i].text);
ibuf->flags |= IB_metadata;
}
}
2002-10-12 11:37:38 +00:00
png_read_end(png_ptr, info_ptr);
}
2012-07-07 22:51:57 +00:00
/* clean up */
2019-04-23 11:01:30 +10:00
if (pixels) {
MEM_freeN(pixels);
2019-04-23 11:01:30 +10:00
}
if (pixels16) {
MEM_freeN(pixels16);
2019-04-23 11:01:30 +10:00
}
if (row_pointers) {
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);
return ibuf;
2002-10-12 11:37:38 +00:00
}