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
|
2011-02-27 20:23:21 +00:00
|
|
|
*/
|
|
|
|
|
|
2020-09-04 16:23:00 +02:00
|
|
|
#include <math.h>
|
|
|
|
|
|
2012-08-12 23:28:33 +00:00
|
|
|
#include "BLI_fileops.h"
|
2020-03-19 09:33:03 +01:00
|
|
|
#include "BLI_utildefines.h"
|
2002-10-12 11:37:38 +00:00
|
|
|
|
|
|
|
|
#include "imbuf.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_imbuf.h"
|
|
|
|
|
#include "IMB_imbuf_types.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"
|
|
|
|
|
|
2019-04-22 01:18:55 +10:00
|
|
|
/* Some code copied from article on microsoft.com,
|
|
|
|
|
* copied here for enhanced BMP support in the future:
|
2012-03-09 18:28:30 +00:00
|
|
|
* http://www.microsoft.com/msj/defaultframe.asp?page=/msj/0197/mfcp1/mfcp1.htm&nav=/msj/0197/newnav.htm
|
|
|
|
|
*/
|
2002-10-12 11:37:38 +00:00
|
|
|
|
2012-04-21 13:37:26 +00:00
|
|
|
typedef struct BMPINFOHEADER {
|
2020-01-29 12:52:57 +11:00
|
|
|
uint biSize;
|
|
|
|
|
uint biWidth;
|
|
|
|
|
uint biHeight;
|
|
|
|
|
ushort biPlanes;
|
|
|
|
|
ushort biBitCount;
|
|
|
|
|
uint biCompression;
|
|
|
|
|
uint biSizeImage;
|
|
|
|
|
uint biXPelsPerMeter;
|
|
|
|
|
uint biYPelsPerMeter;
|
|
|
|
|
uint biClrUsed;
|
|
|
|
|
uint biClrImportant;
|
2002-10-12 11:37:38 +00:00
|
|
|
} BMPINFOHEADER;
|
|
|
|
|
|
2013-03-17 10:26:23 +00:00
|
|
|
#if 0
|
2004-01-09 22:04:08 +00:00
|
|
|
typedef struct BMPHEADER {
|
2020-01-29 12:52:57 +11:00
|
|
|
ushort biType;
|
|
|
|
|
uint biSize;
|
|
|
|
|
ushort biRes1;
|
|
|
|
|
ushort biRes2;
|
|
|
|
|
uint biOffBits;
|
2004-01-09 22:04:08 +00:00
|
|
|
} BMPHEADER;
|
2013-03-17 10:26:23 +00:00
|
|
|
#endif
|
2004-01-09 22:04:08 +00:00
|
|
|
|
2002-10-12 11:37:38 +00:00
|
|
|
#define BMP_FILEHEADER_SIZE 14
|
|
|
|
|
|
2015-01-29 20:33:37 +01:00
|
|
|
#define CHECK_HEADER_FIELD(_mem, _field) ((_mem[0] == _field[0]) && (_mem[1] == _field[1]))
|
|
|
|
|
#define CHECK_HEADER_FIELD_BMP(_mem) \
|
|
|
|
|
(CHECK_HEADER_FIELD(_mem, "BM") || CHECK_HEADER_FIELD(_mem, "BA") || \
|
|
|
|
|
CHECK_HEADER_FIELD(_mem, "CI") || CHECK_HEADER_FIELD(_mem, "CP") || \
|
|
|
|
|
CHECK_HEADER_FIELD(_mem, "IC") || CHECK_HEADER_FIELD(_mem, "PT"))
|
|
|
|
|
|
2020-11-11 16:14:09 +11:00
|
|
|
static bool checkbmp(const uchar *mem, const size_t size)
|
2002-10-12 11:37:38 +00:00
|
|
|
{
|
2021-04-13 21:12:55 +10:00
|
|
|
if (size < (BMP_FILEHEADER_SIZE + sizeof(BMPINFOHEADER))) {
|
2020-11-11 16:14:09 +11:00
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2020-11-11 14:46:32 +11:00
|
|
|
if (!CHECK_HEADER_FIELD_BMP(mem)) {
|
2020-11-11 16:14:09 +11:00
|
|
|
return false;
|
2020-11-11 14:46:32 +11:00
|
|
|
}
|
2012-08-27 11:59:26 +00:00
|
|
|
|
2020-11-11 16:14:06 +11:00
|
|
|
bool ok = false;
|
2002-10-12 11:37:38 +00:00
|
|
|
BMPINFOHEADER bmi;
|
2020-01-29 12:52:57 +11:00
|
|
|
uint u;
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2020-11-11 14:46:32 +11:00
|
|
|
/* skip fileheader */
|
|
|
|
|
mem += BMP_FILEHEADER_SIZE;
|
|
|
|
|
|
|
|
|
|
/* for systems where an int needs to be 4 bytes aligned */
|
|
|
|
|
memcpy(&bmi, mem, sizeof(bmi));
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2020-11-11 14:46:32 +11:00
|
|
|
u = LITTLE_LONG(bmi.biSize);
|
|
|
|
|
/* we only support uncompressed images for now. */
|
|
|
|
|
if (u >= sizeof(BMPINFOHEADER)) {
|
|
|
|
|
if (bmi.biCompression == 0) {
|
|
|
|
|
u = LITTLE_SHORT(bmi.biBitCount);
|
|
|
|
|
if (u > 0 && u <= 32) {
|
2020-11-11 16:14:06 +11:00
|
|
|
ok = true;
|
2002-10-12 11:37:38 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2020-11-11 16:14:06 +11:00
|
|
|
return ok;
|
2002-10-12 11:37:38 +00:00
|
|
|
}
|
|
|
|
|
|
2020-11-11 16:14:09 +11:00
|
|
|
bool imb_is_a_bmp(const uchar *buf, size_t size)
|
2011-09-28 05:53:40 +00:00
|
|
|
{
|
2020-11-11 16:14:09 +11:00
|
|
|
return checkbmp(buf, size);
|
2002-10-12 11:37:38 +00:00
|
|
|
}
|
|
|
|
|
|
2021-02-08 12:26:50 +01:00
|
|
|
static size_t imb_bmp_calc_row_size_in_bytes(size_t x, size_t depth)
|
|
|
|
|
{
|
2021-04-13 21:12:55 +10:00
|
|
|
/* https://docs.microsoft.com/en-us/windows/win32/api/wingdi/ns-wingdi-bitmapinfoheader#calculating-surface-stride
|
|
|
|
|
*/
|
|
|
|
|
return (((x * depth) + 31) & ~31) >> 3;
|
2021-02-08 12:26:50 +01:00
|
|
|
}
|
|
|
|
|
|
2020-01-29 12:52:57 +11:00
|
|
|
ImBuf *imb_bmp_decode(const uchar *mem, size_t size, int flags, char colorspace[IM_MAX_SPACE])
|
2002-10-12 11:37:38 +00:00
|
|
|
{
|
2020-01-29 12:52:57 +11:00
|
|
|
ImBuf *ibuf = NULL;
|
2002-10-12 11:37:38 +00:00
|
|
|
BMPINFOHEADER bmi;
|
2021-04-13 21:12:55 +10:00
|
|
|
int ibuf_depth;
|
2020-01-29 12:52:57 +11:00
|
|
|
const uchar *bmp;
|
|
|
|
|
uchar *rect;
|
|
|
|
|
ushort col;
|
2015-01-29 20:33:37 +01:00
|
|
|
bool top_to_bottom = false;
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2020-11-11 16:14:09 +11:00
|
|
|
if (checkbmp(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
|
|
|
colorspace_set_default_role(colorspace, IM_MAX_SPACE, COLOR_ROLE_DEFAULT_BYTE);
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2021-04-13 21:12:55 +10:00
|
|
|
/* For systems where an int needs to be 4 bytes aligned. */
|
|
|
|
|
memcpy(&bmi, mem + BMP_FILEHEADER_SIZE, sizeof(bmi));
|
|
|
|
|
|
|
|
|
|
const size_t palette_offset = (size_t)BMP_FILEHEADER_SIZE + LITTLE_LONG(bmi.biSize);
|
|
|
|
|
const int depth = LITTLE_SHORT(bmi.biBitCount);
|
|
|
|
|
const int xppm = LITTLE_LONG(bmi.biXPelsPerMeter);
|
|
|
|
|
const int yppm = LITTLE_LONG(bmi.biYPelsPerMeter);
|
|
|
|
|
const int x = LITTLE_LONG(bmi.biWidth);
|
|
|
|
|
int y = LITTLE_LONG(bmi.biHeight);
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2021-04-13 21:12:55 +10:00
|
|
|
/* Negative height means bitmap is stored top-to-bottom. */
|
|
|
|
|
if (y < 0) {
|
|
|
|
|
y = -y;
|
|
|
|
|
top_to_bottom = true;
|
2002-10-12 11:37:38 +00:00
|
|
|
}
|
2021-04-13 21:12:55 +10:00
|
|
|
|
|
|
|
|
/* Validate and cross-check offsets and sizes. */
|
|
|
|
|
if (x < 1 ||
|
|
|
|
|
!(depth == 1 || depth == 4 || depth == 8 || depth == 16 || depth == 24 || depth == 32)) {
|
2015-03-14 12:10:09 +11:00
|
|
|
return NULL;
|
2015-03-11 21:32:00 +11:00
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2021-04-13 21:12:55 +10:00
|
|
|
const size_t pixel_data_offset = (size_t)LITTLE_LONG(*(int *)(mem + 10));
|
|
|
|
|
const size_t header_bytes = BMP_FILEHEADER_SIZE + sizeof(BMPINFOHEADER);
|
|
|
|
|
const size_t num_actual_data_bytes = size - pixel_data_offset;
|
2021-02-08 12:26:50 +01:00
|
|
|
const size_t row_size_in_bytes = imb_bmp_calc_row_size_in_bytes(x, depth);
|
|
|
|
|
const size_t num_expected_data_bytes = row_size_in_bytes * y;
|
2021-04-13 21:12:55 +10:00
|
|
|
if (num_actual_data_bytes < num_expected_data_bytes || num_actual_data_bytes > size ||
|
|
|
|
|
pixel_data_offset < header_bytes || pixel_data_offset > (size - num_expected_data_bytes) ||
|
|
|
|
|
palette_offset < header_bytes || palette_offset > pixel_data_offset) {
|
2021-02-08 12:26:50 +01:00
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
2013-08-13 07:18:07 +00:00
|
|
|
if (depth <= 8) {
|
|
|
|
|
ibuf_depth = 24;
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
ibuf_depth = depth;
|
|
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2021-04-13 21:12:55 +10:00
|
|
|
bmp = mem + pixel_data_offset;
|
2015-01-29 20:33:37 +01:00
|
|
|
|
2012-03-09 18:28:30 +00:00
|
|
|
#if 0
|
2021-04-13 21:12:55 +10:00
|
|
|
printf("palette_offset: %d, x: %d y: %d, depth: %d\n", palette_offset, x, y, depth);
|
2012-03-09 18:28:30 +00:00
|
|
|
#endif
|
|
|
|
|
|
2002-10-12 11:37:38 +00:00
|
|
|
if (flags & IB_test) {
|
2013-08-13 07:18:07 +00:00
|
|
|
ibuf = IMB_allocImBuf(x, y, ibuf_depth, 0);
|
2012-03-24 06:38:07 +00:00
|
|
|
}
|
|
|
|
|
else {
|
2013-08-13 07:18:07 +00:00
|
|
|
ibuf = IMB_allocImBuf(x, y, ibuf_depth, IB_rect);
|
2018-01-14 14:19:57 +01:00
|
|
|
if (!ibuf) {
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2020-01-29 12:52:57 +11:00
|
|
|
rect = (uchar *)ibuf->rect;
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2015-03-11 21:32:00 +11:00
|
|
|
if (depth <= 8) {
|
2021-04-13 21:12:55 +10:00
|
|
|
const char(*palette)[4] = (const char(*)[4])(mem + palette_offset);
|
2015-03-11 21:32:00 +11:00
|
|
|
const int startmask = ((1 << depth) - 1) << 8;
|
2018-01-14 14:19:57 +01:00
|
|
|
for (size_t i = y; i > 0; i--) {
|
2015-03-11 21:32:00 +11:00
|
|
|
int index;
|
|
|
|
|
int bitoffs = 8;
|
|
|
|
|
int bitmask = startmask;
|
|
|
|
|
int nbytes = 0;
|
|
|
|
|
const char *pcol;
|
2015-01-29 20:33:37 +01:00
|
|
|
if (top_to_bottom) {
|
2020-01-29 12:52:57 +11:00
|
|
|
rect = (uchar *)&ibuf->rect[(i - 1) * x];
|
2015-01-29 20:33:37 +01:00
|
|
|
}
|
2018-01-14 14:19:57 +01:00
|
|
|
for (size_t j = x; j > 0; j--) {
|
2015-03-11 21:32:00 +11:00
|
|
|
bitoffs -= depth;
|
|
|
|
|
bitmask >>= depth;
|
|
|
|
|
index = (bmp[0] & bitmask) >> bitoffs;
|
|
|
|
|
pcol = palette[index];
|
2015-03-11 21:26:29 +11:00
|
|
|
/* intentionally BGR -> RGB */
|
|
|
|
|
rect[0] = pcol[2];
|
2013-08-13 07:18:07 +00:00
|
|
|
rect[1] = pcol[1];
|
2015-03-11 21:26:29 +11:00
|
|
|
rect[2] = pcol[0];
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2013-08-13 07:18:07 +00:00
|
|
|
rect[3] = 255;
|
2015-03-11 21:32:00 +11:00
|
|
|
rect += 4;
|
|
|
|
|
if (bitoffs == 0) {
|
|
|
|
|
/* Advance to the next byte */
|
|
|
|
|
bitoffs = 8;
|
|
|
|
|
bitmask = startmask;
|
|
|
|
|
nbytes += 1;
|
|
|
|
|
bmp += 1;
|
2019-04-17 06:17:24 +02:00
|
|
|
}
|
2015-03-11 21:32:00 +11:00
|
|
|
}
|
|
|
|
|
/* Advance to the next row */
|
2021-02-08 12:26:50 +01:00
|
|
|
bmp += (row_size_in_bytes - nbytes);
|
2019-04-17 06:17:24 +02:00
|
|
|
}
|
2013-08-13 07:18:07 +00:00
|
|
|
}
|
|
|
|
|
else if (depth == 16) {
|
2018-01-14 14:19:57 +01:00
|
|
|
for (size_t i = y; i > 0; i--) {
|
2015-01-29 20:33:37 +01:00
|
|
|
if (top_to_bottom) {
|
2020-01-29 12:52:57 +11:00
|
|
|
rect = (uchar *)&ibuf->rect[(i - 1) * x];
|
2015-01-29 20:33:37 +01:00
|
|
|
}
|
2018-01-14 14:19:57 +01:00
|
|
|
for (size_t j = x; j > 0; j--) {
|
2015-01-29 20:33:37 +01:00
|
|
|
col = bmp[0] + (bmp[1] << 8);
|
|
|
|
|
rect[0] = ((col >> 10) & 0x1f) << 3;
|
|
|
|
|
rect[1] = ((col >> 5) & 0x1f) << 3;
|
|
|
|
|
rect[2] = ((col >> 0) & 0x1f) << 3;
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2015-01-29 20:33:37 +01:00
|
|
|
rect[3] = 255;
|
|
|
|
|
rect += 4;
|
|
|
|
|
bmp += 2;
|
2019-04-17 06:17:24 +02:00
|
|
|
}
|
2015-01-29 20:33:37 +01:00
|
|
|
}
|
|
|
|
|
}
|
2012-03-24 06:38:07 +00:00
|
|
|
else if (depth == 24) {
|
2013-08-13 07:18:07 +00:00
|
|
|
const int x_pad = x % 4;
|
2018-01-14 14:19:57 +01:00
|
|
|
for (size_t i = y; i > 0; i--) {
|
2015-01-29 20:33:37 +01:00
|
|
|
if (top_to_bottom) {
|
2020-01-29 12:52:57 +11:00
|
|
|
rect = (uchar *)&ibuf->rect[(i - 1) * x];
|
2015-01-29 20:33:37 +01:00
|
|
|
}
|
2018-01-14 14:19:57 +01:00
|
|
|
for (size_t j = x; j > 0; j--) {
|
2006-01-29 15:23:26 +00:00
|
|
|
rect[0] = bmp[2];
|
|
|
|
|
rect[1] = bmp[1];
|
|
|
|
|
rect[2] = bmp[0];
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2006-01-29 15:23:26 +00:00
|
|
|
rect[3] = 255;
|
|
|
|
|
rect += 4;
|
|
|
|
|
bmp += 3;
|
|
|
|
|
}
|
|
|
|
|
/* for 24-bit images, rows are padded to multiples of 4 */
|
2013-08-13 07:18:07 +00:00
|
|
|
bmp += x_pad;
|
2002-10-12 11:37:38 +00:00
|
|
|
}
|
2012-03-24 06:38:07 +00:00
|
|
|
}
|
|
|
|
|
else if (depth == 32) {
|
2018-01-14 14:19:57 +01:00
|
|
|
for (size_t i = y; i > 0; i--) {
|
2015-01-29 20:33:37 +01:00
|
|
|
if (top_to_bottom) {
|
2020-01-29 12:52:57 +11:00
|
|
|
rect = (uchar *)&ibuf->rect[(i - 1) * x];
|
2015-01-29 20:33:37 +01:00
|
|
|
}
|
2018-01-14 14:19:57 +01:00
|
|
|
for (size_t j = x; j > 0; j--) {
|
2015-01-29 20:33:37 +01:00
|
|
|
rect[0] = bmp[2];
|
|
|
|
|
rect[1] = bmp[1];
|
|
|
|
|
rect[2] = bmp[0];
|
|
|
|
|
rect[3] = bmp[3];
|
|
|
|
|
rect += 4;
|
|
|
|
|
bmp += 4;
|
|
|
|
|
}
|
2002-10-12 11:37:38 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2002-10-12 11:37:38 +00:00
|
|
|
if (ibuf) {
|
2013-03-13 07:16:53 +00:00
|
|
|
ibuf->ppm[0] = xppm;
|
|
|
|
|
ibuf->ppm[1] = yppm;
|
2015-07-13 13:58:17 +02:00
|
|
|
ibuf->ftype = IMB_FTYPE_BMP;
|
2002-10-12 11:37:38 +00:00
|
|
|
}
|
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
|
|
|
}
|
|
|
|
|
|
2015-01-29 20:33:37 +01:00
|
|
|
#undef CHECK_HEADER_FIELD_BMP
|
|
|
|
|
#undef CHECK_HEADER_FIELD
|
|
|
|
|
|
2004-01-09 22:04:08 +00:00
|
|
|
/* Couple of helper functions for writing our data */
|
2020-01-29 12:52:57 +11:00
|
|
|
static int putIntLSB(uint ui, FILE *ofile)
|
2011-12-17 00:52:36 +00:00
|
|
|
{
|
2012-05-16 09:26:37 +00:00
|
|
|
putc((ui >> 0) & 0xFF, ofile);
|
|
|
|
|
putc((ui >> 8) & 0xFF, ofile);
|
|
|
|
|
putc((ui >> 16) & 0xFF, ofile);
|
|
|
|
|
return putc((ui >> 24) & 0xFF, ofile);
|
2004-01-09 22:04:08 +00:00
|
|
|
}
|
|
|
|
|
|
2020-01-29 12:52:57 +11:00
|
|
|
static int putShortLSB(ushort us, FILE *ofile)
|
2011-12-17 00:52:36 +00:00
|
|
|
{
|
2012-05-16 09:26:37 +00:00
|
|
|
putc((us >> 0) & 0xFF, ofile);
|
|
|
|
|
return putc((us >> 8) & 0xFF, ofile);
|
2018-06-17 17:04:54 +02:00
|
|
|
}
|
2004-01-09 22:04:08 +00:00
|
|
|
|
|
|
|
|
/* Found write info at http://users.ece.gatech.edu/~slabaugh/personal/c/bitmapUnix.c */
|
2020-11-11 16:14:05 +11:00
|
|
|
bool imb_savebmp(ImBuf *ibuf, const char *filepath, int UNUSED(flags))
|
2011-09-28 05:53:40 +00:00
|
|
|
{
|
2005-01-03 19:53:04 +00:00
|
|
|
BMPINFOHEADER infoheader;
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2020-01-29 12:37:14 +11:00
|
|
|
const size_t bytes_per_pixel = (ibuf->planes + 7) >> 3;
|
2020-11-06 12:30:59 +11:00
|
|
|
BLI_assert(ELEM(bytes_per_pixel, 1, 3));
|
2020-01-29 12:37:14 +11:00
|
|
|
|
2020-01-29 12:52:57 +11:00
|
|
|
const size_t pad_bytes_per_scanline = (4 - ibuf->x * bytes_per_pixel % 4) % 4;
|
|
|
|
|
const size_t bytesize = (ibuf->x * bytes_per_pixel + pad_bytes_per_scanline) * ibuf->y;
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2020-01-29 12:52:57 +11:00
|
|
|
const uchar *data = (const uchar *)ibuf->rect;
|
2020-09-04 20:59:13 +02:00
|
|
|
FILE *ofile = BLI_fopen(filepath, "wb");
|
2020-01-29 12:52:57 +11:00
|
|
|
if (ofile == NULL) {
|
2012-05-16 09:26:37 +00:00
|
|
|
return 0;
|
2019-04-23 11:01:30 +10:00
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2020-01-29 12:37:14 +11:00
|
|
|
const bool is_grayscale = bytes_per_pixel == 1;
|
|
|
|
|
const size_t palette_size = is_grayscale ? 255 * 4 : 0; /* RGBA32 */
|
|
|
|
|
const size_t pixel_array_start = BMP_FILEHEADER_SIZE + sizeof(infoheader) + palette_size;
|
|
|
|
|
|
|
|
|
|
putShortLSB(19778, ofile); /* "BM" */
|
|
|
|
|
putIntLSB(bytesize + pixel_array_start, ofile); /* Total file size */
|
|
|
|
|
putShortLSB(0, ofile); /* Res1 */
|
|
|
|
|
putShortLSB(0, ofile); /* Res2 */
|
|
|
|
|
putIntLSB(pixel_array_start, ofile); /* offset to start of pixel array */
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2012-04-29 15:47:02 +00:00
|
|
|
putIntLSB(sizeof(infoheader), ofile);
|
|
|
|
|
putIntLSB(ibuf->x, ofile);
|
|
|
|
|
putIntLSB(ibuf->y, ofile);
|
|
|
|
|
putShortLSB(1, ofile);
|
2020-01-29 12:37:14 +11:00
|
|
|
putShortLSB(is_grayscale ? 8 : 24, ofile);
|
2012-04-29 15:47:02 +00:00
|
|
|
putIntLSB(0, ofile);
|
2013-09-08 03:39:45 +00:00
|
|
|
putIntLSB(bytesize, ofile);
|
2020-09-07 10:09:02 +02:00
|
|
|
putIntLSB(round(ibuf->ppm[0]), ofile);
|
|
|
|
|
putIntLSB(round(ibuf->ppm[1]), ofile);
|
2012-04-29 15:47:02 +00:00
|
|
|
putIntLSB(0, ofile);
|
|
|
|
|
putIntLSB(0, ofile);
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2020-01-29 12:37:14 +11:00
|
|
|
/* color palette table, which is just every grayscale color, full alpha */
|
|
|
|
|
if (is_grayscale) {
|
|
|
|
|
for (char i = 0; i < 255; i++) {
|
|
|
|
|
putc(i, ofile);
|
|
|
|
|
putc(i, ofile);
|
|
|
|
|
putc(i, ofile);
|
|
|
|
|
putc(0xFF, ofile);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (is_grayscale) {
|
|
|
|
|
for (size_t y = 0; y < ibuf->y; y++) {
|
|
|
|
|
for (size_t x = 0; x < ibuf->x; x++) {
|
2020-01-29 12:52:57 +11:00
|
|
|
const size_t ptr = (x + y * ibuf->x) * 4;
|
2020-01-29 12:37:14 +11:00
|
|
|
if (putc(data[ptr], ofile) == EOF) {
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
2019-04-23 11:01:30 +10:00
|
|
|
}
|
2020-01-29 12:52:57 +11:00
|
|
|
/* Add padding here. */
|
|
|
|
|
for (size_t t = 0; t < pad_bytes_per_scanline; t++) {
|
2020-01-29 12:37:14 +11:00
|
|
|
if (putc(0, ofile) == EOF) {
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
2019-04-23 11:01:30 +10:00
|
|
|
}
|
2005-01-03 19:53:04 +00:00
|
|
|
}
|
2020-01-29 12:37:14 +11:00
|
|
|
}
|
|
|
|
|
else {
|
2020-01-29 12:52:57 +11:00
|
|
|
/* Need to write out padded image data in BGR format. */
|
2020-01-29 12:37:14 +11:00
|
|
|
for (size_t y = 0; y < ibuf->y; y++) {
|
|
|
|
|
for (size_t x = 0; x < ibuf->x; x++) {
|
2020-01-29 12:52:57 +11:00
|
|
|
const size_t ptr = (x + y * ibuf->x) * 4;
|
2020-01-29 12:37:14 +11:00
|
|
|
if (putc(data[ptr + 2], ofile) == EOF) {
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
if (putc(data[ptr + 1], ofile) == EOF) {
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
if (putc(data[ptr], ofile) == EOF) {
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-01-29 12:52:57 +11:00
|
|
|
/* Add padding here. */
|
|
|
|
|
for (size_t t = 0; t < pad_bytes_per_scanline; t++) {
|
2020-01-29 12:37:14 +11:00
|
|
|
if (putc(0, ofile) == EOF) {
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
2019-04-23 11:01:30 +10:00
|
|
|
}
|
2005-01-03 19:53:04 +00:00
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
}
|
2005-01-03 19:53:04 +00:00
|
|
|
if (ofile) {
|
|
|
|
|
fflush(ofile);
|
|
|
|
|
fclose(ofile);
|
|
|
|
|
}
|
|
|
|
|
return 1;
|
2004-01-09 22:04:08 +00:00
|
|
|
}
|