Add helper function to replace buffer of a single-frame image

Very similar to BKE_image_add_from_imbuf with the exception that no
new image data-block is created, but instead the given one is modified.
This commit is contained in:
Sergey Sharybin
2022-06-30 15:37:32 +02:00
parent dfa5bd689e
commit 72b9e07cf2
2 changed files with 35 additions and 13 deletions

View File

@@ -194,6 +194,14 @@ struct Image *BKE_image_add_generated(struct Main *bmain,
*/
struct Image *BKE_image_add_from_imbuf(struct Main *bmain, struct ImBuf *ibuf, const char *name);
/**
* For a non-viewer single-buffer image (single frame file, or generated image) replace its image
* buffer with the given one.
* If an unsupported image type (multi-layer, image sequence, ...) the function will assert in the
* debug mode and will have an underfined behavior in the release mode.
*/
void BKE_image_replace_imbuf(struct Image *image, struct ImBuf *ibuf);
/**
* For reload, refresh, pack.
*/

View File

@@ -1216,23 +1216,37 @@ Image *BKE_image_add_from_imbuf(Main *bmain, ImBuf *ibuf, const char *name)
return nullptr;
}
if (source == IMA_SRC_FILE) {
STRNCPY(ima->filepath, ibuf->name);
}
else if (ibuf->rect_float) {
/* For the consistency with manual image creation: when the image buffer is float reflect it in
* the generated flags. */
ima->gen_flag |= IMA_GEN_FLOAT;
}
BKE_image_replace_imbuf(ima, ibuf);
image_assign_ibuf(ima, ibuf, IMA_NO_INDEX, 0);
image_colorspace_from_imbuf(ima, ibuf);
return ima;
}
void BKE_image_replace_imbuf(Image *image, ImBuf *ibuf)
{
BLI_assert(image->type == IMA_TYPE_IMAGE &&
ELEM(image->source, IMA_SRC_FILE, IMA_SRC_GENERATED));
BKE_image_free_buffers(image);
image_assign_ibuf(image, ibuf, IMA_NO_INDEX, 0);
image_colorspace_from_imbuf(image, ibuf);
/* Keep generated image type flags consistent with the image buffer. */
if (image->source == IMA_SRC_GENERATED) {
if (ibuf->rect_float) {
image->gen_flag |= IMA_GEN_FLOAT;
}
else {
image->gen_flag &= ~IMA_GEN_FLOAT;
}
image->gen_x = ibuf->x;
image->gen_y = ibuf->y;
}
/* Consider image dirty since its content can not be re-created unless the image is explicitly
* saved. */
BKE_image_mark_dirty(ima, ibuf);
return ima;
BKE_image_mark_dirty(image, ibuf);
}
/** Pack image buffer to memory as PNG or EXR. */