Commit Graph

2422 Commits

Author SHA1 Message Date
Campbell Barton
b3dfde88f3 Cleanup: spelling in comments (check_spelling_* target)
Also uppercase acronyms: API, UTF & ASCII.
2025-05-17 10:17:37 +10:00
Brecht Van Lommel
a94a911e10 Fix: Slow USD import with certain OpenColorIO configs
As evidenced by #138868, checking all colorspaces to see if they are sRGB
can be very slow, due to loading all LUTs into memory. Instead use a simpler
check by a list of names only, which is still better than the hardcoded
single name used in Blender 4.4.

Ideally recent configs will use the ColorInterop Forum alias so we can
find the sRGB colorspace reliably.
2025-05-15 19:33:37 +02:00
Sergey Sharybin
9d4b236d13 Fix: Class memory access on the color management tear-down
Pull Request: https://projects.blender.org/blender/blender/pulls/138670
2025-05-09 16:22:57 +02:00
Sergey Sharybin
7ceb4495c5 Refactor: OpenColorIO integration
Briefly about this change:
- OpenColorIO C-API is removed.
- The information about color spaces in ImBuf module is removed.
  It was stored in global ListBase in colormanagement.cc.
- Both OpenColorIO and fallback implementation supports GPU drawing.
- Fallback implementation supports white point, RGB curves, etc.
- Removed check for support of GPU drawing in IMB.

Historically it was implemented in a separate library with C-API, this
is because way back C++ code needed to stay in intern. This causes all
sort of overheads, and even calls that are strictly considered bad
level.

This change moves OpenColorIO integration into a module within imbuf,
next to movie, and next to IMB_colormanagement which is the main user
of it. This allows to avoid copy of color spaces, displays, views etc
in the ImBuf: they were used to help quickly querying information to
be shown on the interface. With this change it can be stored in the
same data structures as what is used by the OpenColorIO integration.
While it might not be fully avoiding duplication it is now less, and
there is no need in the user code to maintain the copies.

In a lot of cases this change also avoids allocations done per access
to the OpenColorIO. For example, it is not needed anymore to allocate
image descriptor in a heap.

The bigger user-visible change is that the fallback implementation now
supports GLSL drawing, with the whole list of supported features, such
as curve mapping and white point. This should help simplifying code
which relies on color space conversion on GPU: there is no need to
figure out fallback solution in such cases. The only case when drawing
will not work is when there is some actual bug, or driver issue, and
shader has failed to compile.

The change avoids having an opaque type for color space, and instead
uses forward declaration. It is a bit verbose on declaration, but helps
avoiding unsafe type-casts. There are ways to solve this in the future,
like having a header for forward declaration, or to flatten the name
space a bit.

There should be no user-level changes under normal operation.
When building without OpenColorIO or the configuration has a typo or
is missing a fuller set of color management tools is applies (such as the
white point correction).

Pull Request: https://projects.blender.org/blender/blender/pulls/138433
2025-05-09 14:01:43 +02:00
Sergey Sharybin
24a28ea38d Cleanup: Const-correctness in ImBuf metadata drawing 2025-05-07 16:21:06 +02:00
Brecht Van Lommel
321e9e049e Fix: USD material reader uses fixed colorspace names
For other OpenColorIO configurations these may not exist or have a
different meaning.
2025-05-02 17:39:56 +02:00
Sergey Sharybin
5b29ba488f Color management: Implement shader to convert to scene linear
Previous conversion to scene linear was done in the display transform
shader. Having a separate shader to convert texture to scene linear
allows drawing input textures with different color spaces into a
viewport and apply display transform on all of them.

Currently unused, but is required for !138094. It could also be used
in the future to avoid host-side linearization in the image engine.

Internally it uses a lot of the same logic for shader caching and
binding, but the code is refactored a bit to make it easier to have
a stronger separation in the future if needed.

Ref #138094

Pull Request: https://projects.blender.org/blender/blender/pulls/138308
2025-05-02 16:51:46 +02:00
Aras Pranckevicius
8528feb7ee Cleanup: replace some spinlocks with atomics or mutexes
- ImBuf reference counting: turn that into just an atomic integer
- Cachefile safety: turn into a mutex, since work under the spinlock
  was quite heavy (hashtable creation, other memory allocations)
- Movie clip editor: turn into a mutex, since work under the spinlock
  was very heavy (reading files from disk, etc.)
- Mesh intersect: remove the previously commented out spinlock path;
  replace BLI mutex with C++ mutex for shorter code

Pull Request: https://projects.blender.org/blender/blender/pulls/137989
2025-04-29 10:42:45 +02:00
Campbell Barton
2d4290f285 Cleanup: spelling in comments (make check_spelling_*)
Also inconsistent capitalization.
2025-04-24 22:45:22 +00:00
Brecht Van Lommel
d061b00455 Refactor: Eliminate various unsafe memcpy and memset
Some of these already have warnings with clang-tidy, others are more
safe in case these structs get (copy) constructors in the future.

Pull Request: https://projects.blender.org/blender/blender/pulls/137404
2025-04-21 17:59:41 +02:00
Brecht Van Lommel
637c6497e9 Refactor: Use more typed MEM_calloc<>, avoid unnecessary size_t cast
Handle some cases that were missed in previous refactor. And eliminate
unnecessary size_t casts as these could hide issues.

Pull Request: https://projects.blender.org/blender/blender/pulls/137404
2025-04-21 17:59:41 +02:00
Campbell Barton
3933f45f52 Cleanup: move doc-strings to declarations
Move into headers or to the top of the function body for internal
implementation details, in some cases remove duplicate doc-strings.
2025-04-18 22:58:36 +10:00
Campbell Barton
217406ec42 Cleanup: move IMB_convert_rgba_to_abgr to a static function
This is a fairly specific function and was only used by the IRIS format.
2025-04-15 12:08:08 +10:00
Campbell Barton
578bf16537 Fix: integer overflow in rect buffer filling on large buffers
Other changes include:

- Replace buffer out of bounds check with an assert
  (it was only done for the byte).
- Step over pixels on each X span
  instead of calculating the offset for each pixel.
- Use const variables.
2025-04-12 05:48:04 +00:00
Campbell Barton
3b2c0b6cd5 Fix: integer overflows calculating the number of pixels
Note that moving to the templated versions of array allocation functions
in [0] caused integer overflow regressions because multiplying by the
`sizeof(type)` caused the following values to be cast to a size_t.

[0]: e85ebb24fe
2025-04-12 01:28:30 +00:00
Campbell Barton
309bdc491b Fix: integer overflow scaling large images 2025-04-12 11:04:53 +10:00
Campbell Barton
f9b9644cbb Fix: failure to save an existing Cineon file
Saving a Cineon fail a second time would fail because it would
default to 8 bits per sample, where only 10 bits is supported.
2025-04-11 22:31:05 +10:00
Campbell Barton
f6757abae2 Fix: crash saving large Cineon images
When the offset exceeded INT_MAX, the offset would wrap to a
negative number then attempt to read/write outside the buffer bounds.
2025-04-11 22:31:05 +10:00
Campbell Barton
6d6c695619 Fix: integer overflows calculating the number of pixels in imbuf
Use IMB_get_pixel_count to prevent integer overflow.
2025-04-11 18:52:38 +10:00
Campbell Barton
d256b7f688 Fix: crash loading & saving large Iris images
Integer overflow in IMB_convert_rgba_to_abgr assigned a negative value
to size_t resulting in a large value which wrote past the buffer bounds.
2025-04-11 17:40:11 +10:00
Campbell Barton
37148d6711 Fix: crash saving large WebP images
WebP images can be at most 16383x16383,
however saving sizes which overflowed an integer would crash.

- Early exit with an error when the image is too large.
- Replace integer offsets with size_t.
2025-04-11 07:33:48 +00:00
Campbell Barton
2cbb07d7ad Fix: error reading RLE compressed Iris images
Regression in [0] which used a 4x large size bounds check
to prevent a buffer read overflow reading the RLE table.

[0]: 829916f4e5
2025-04-11 15:22:17 +10:00
Campbell Barton
b2dbfa7d77 Cleanup: spelling in comments, use doxygen comments 2025-04-10 13:02:29 +10:00
Campbell Barton
24f8819c78 Fix: correct check for Iris image dimensions
Mistake in [0], missed width check.

[0]: 7a23d7aba4
2025-04-10 10:50:10 +10:00
Campbell Barton
12f0164eb0 CMake: match defines & CMake variable names
Avoid mistakes with these variables getting mixed up
(see: bf03a2684b).

Ref !137183
2025-04-09 08:44:36 +00:00
Campbell Barton
b024fe041e Cleanup: use more descriptive names for the IRIS format
Also add some code-comments.
2025-04-09 01:47:02 +00:00
Campbell Barton
6d8d37558a Cleanup: replace outdated function names in __func__ in error messages 2025-04-09 11:32:21 +10:00
Campbell Barton
7a23d7aba4 Fix: error writing & loading corrupt IRIS files
- Files cannot exceed 65535 width or height,
  attempting to write 65536 would write a zero sized image.
- Ensure that zero sized imbufs are never loaded.
2025-04-09 11:28:58 +10:00
Campbell Barton
35f5877ac3 Fix: incorrect iris image header detection
The first two bytes were being checked in the wrong order,
if files using this header were loaded they would detect as IRIS files
from imb_is_a_iris which would then fail to load (printing errors).
2025-04-09 11:05:47 +10:00
Campbell Barton
2dc4a94e8b Cleanup: use consistent "format_" prefix for imbuf image formats 2025-04-09 00:16:20 +00:00
Campbell Barton
7657afc8cb Correct error in 5d3496625d 2025-04-08 15:43:38 +10:00
Campbell Barton
5d3496625d Fix: regression storing density in BMP I/O
Regression in [0] which broke support for writing DPI.

[0]: aa3bdfd76a

Ref: !137122
2025-04-08 05:40:43 +00:00
Campbell Barton
01dc87d841 ImBuf: write the aspect ratio to EXR images
Only rendered EXR's had the aspect ratio written.
2025-04-08 10:14:36 +10:00
Campbell Barton
c9c59cfa8e Cleanup: rename IMB_FTYPE_{IMAGIC=>IRIS} to confirm with other defines
Use the term IRIS to match the file naming for this format as well as
R_IMF_IMTYPE_IRIS.

Ref !137073
2025-04-07 09:23:49 +00:00
Campbell Barton
6f09def038 Docs: correction to ".tx" description 2025-04-07 15:42:03 +10:00
Campbell Barton
15066403de Docs: cross reference imb_ext_image with known eImbFileType values
Justify inclusion of the extensions.
2025-04-07 08:09:44 +10:00
Campbell Barton
96181c5550 Revert "ImBuf: remove ".tx" extension from the list of supported images"
This reverts commit cf55a0acdc.

While this was added with the commit that added support for tile cache,
keep the extension check as Blender can load them.
2025-04-06 22:07:07 +00:00
Campbell Barton
cf55a0acdc ImBuf: remove ".tx" extension from the list of supported images
Likely an oversight in [0] which removed support for tile-cache.

[0]: 38573d515e
2025-04-06 12:57:11 +10:00
Campbell Barton
af1110fb3c Render: support pixel density in the render pipeline
Add a "Pixel Density" sub-panel to render output settings which
can be used to set the density (as pixels per inch for example).

This is then written to images that support pixel density.

Details:

- The scene has two values a PPM factor and a and base unit.
- The base unit defaults to pixels per inch as this is the most
  common unit used.
- Unit presets for pixels per inch/centimeter/meter are included.
- The pixel density is stored in the render result & EXR cache.
- For non 1:1 aspect renders, the density increases on the axis
  which looks "stretched", so the PPM will print the correct
  aspect with non-square pixels.

Ref !127831
2025-04-05 08:49:22 +00:00
Brecht Van Lommel
e2d58e33a0 Cleanup: Remove unused OpenEXR code 2025-04-02 15:15:27 +02:00
Brecht Van Lommel
ac5dc94c40 Fix #136619: Stereo image saving has wrong colorspace
The logic about which buffer it's going to use for saving is quite confusing.
This simply restores the logic from before 7584ccc28d while still guarding
against accessing null buffers.

Pull Request: https://projects.blender.org/blender/blender/pulls/136659
2025-03-28 13:58:03 +01:00
Campbell Barton
b252a6c7fb Cleanup: various non-functional changes for C++ 2025-03-28 00:59:15 +00:00
Brecht Van Lommel
a02e0fa147 Refactor: Improve image buffer save/load function names and arguments 2025-03-27 22:07:51 +01:00
Brecht Van Lommel
124c0f1692 OpenColorIO: Support using file rules to detect colorspace
The Blender config.ocio does not have any rules, but custom ones can.

The default file rule is ignored if default_byte or default_float roles
exist. These roles are Blender specific, so would not be found in a typical
OCIO config. But when they are set appropriately, they help provide better
default guesses than what is possible with standard OCIO rules.

Pull Request: https://projects.blender.org/blender/blender/pulls/136516
2025-03-27 22:07:50 +01:00
Brecht Van Lommel
ccd7bc2078 Refactor: Modify colorspace handling for image buffer reading
The file formats now fill in ImColorSpaceInfo with the metadata colorspace
and a boolean saying if the pixels have HDR colors. And then the actual
colorspace is decided in imb_handle_colorspace_and_alpha.

This centralizes the logic in one place to make it possible to add
OpenColorIO file rules.

Pull Request: https://projects.blender.org/blender/blender/pulls/136516
2025-03-27 22:07:50 +01:00
David Murmann
afee81753e OpenEXR: Preserve compression and depth settings when re-saving file
Set the flags on the image buffer when loading an EXR file, so they can be
used when saving.

This also removes IB_halffloat and replaces it by the file options flag.

Pull Request: https://projects.blender.org/blender/blender/pulls/135656
2025-03-26 21:35:20 +01:00
Brecht Van Lommel
f5c3b8be5c OpenEXR: Improve handling of ACES2065-1 chromaticities
Use aces_interchange role in OpenColorIO to identify the correct colorspace
for each config. Also use acesImageContainerFlag attribute from the ACES
container format to identify the colorspace.

Write ACES2065-1 chromaticities in EXR files when appropriate. This gets us
closer to supporting output of the ACES container format, though we don't
write acesImageContainerFlag. There are various restrictions that must be met
which are not very practical, and even exr2aces doesn't write it.

Pull Request: https://projects.blender.org/blender/blender/pulls/135823
2025-03-24 15:41:22 +01:00
Martin-Vignali
f7054a2d2f OpenEXR: Automatically set colorspace for XYZ E and ACES2065-1 chromaticities
The OpenEXR format has a chromaticities attribute that identifies the scene
referred linear color space. Detect these two cases now, and assign the
corresponding color space from the OpenColorIO configuration.

In general these chromaticities are known to be unreliable and missing support
in most software, with ideas to deprecate them entirely. However the ACES
container format standard requires them, and with this change we'll be able to
read such images with the right color space set by default.

Co-authored-by: Brecht Van Lommel <brecht@blender.org>

Pull Request: https://projects.blender.org/blender/blender/pulls/135823
2025-03-24 15:41:22 +01:00
Bastien Montagne
dd98cede18 Merge branch 'blender-v4.4-release' 2025-03-14 18:20:26 +01:00
Bastien Montagne
8a61555a46 Fix: IMBuf: Potential buffer overflow in JPEG metadata writer.
Looks like a logic inversion mistake.

Not clear how bad this issue is, as other code related to image metadata
seems to expect 1024 char max size too (e.g. `MAX_METADATA_STR` define
in `ed_draw.cc`. But this is potentially a very bad issue, and the fix
seems safe enough for 4.4 still.

Should also be backported to active LTSs.

Pull Request: https://projects.blender.org/blender/blender/pulls/135983
2025-03-14 18:17:21 +01:00