Changed a way how RGB images are saving from RGBA

Before alpha channel was simply ignored causing bad looking
straight colors which is pretty much useless.

Now saving RGB would alpha-over image on top of black color,
which makes final image look really nice. It's also very
such the same what other graphics software does this.

In the future we could easily support configurable backdrop
color, which would be really the same as other SW does it.

Also, it'll probably worth adding the same mode to RGB
display of image editor.
This commit is contained in:
Sergey Sharybin
2013-02-28 14:25:26 +00:00
parent bad03bcfb1
commit 47d6d18771
2 changed files with 40 additions and 7 deletions

View File

@@ -1702,13 +1702,11 @@ ImBuf *IMB_colormanagement_imbuf_for_write(ImBuf *ibuf, int save_as_render, int
int do_colormanagement; int do_colormanagement;
int is_movie = BKE_imtype_is_movie(image_format_data->imtype); int is_movie = BKE_imtype_is_movie(image_format_data->imtype);
int requires_linear_float = BKE_imtype_requires_linear_float(image_format_data->imtype); int requires_linear_float = BKE_imtype_requires_linear_float(image_format_data->imtype);
int do_alpha_under = image_format_data->planes != R_IMF_PLANES_RGBA;
do_colormanagement = save_as_render && (is_movie || !requires_linear_float); do_colormanagement = save_as_render && (is_movie || !requires_linear_float);
if (do_colormanagement) { if (do_colormanagement || do_alpha_under) {
int make_byte = FALSE;
ImFileType *type;
if (allocate_result) { if (allocate_result) {
colormanaged_ibuf = IMB_dupImBuf(ibuf); colormanaged_ibuf = IMB_dupImBuf(ibuf);
} }
@@ -1727,6 +1725,41 @@ ImBuf *IMB_colormanagement_imbuf_for_write(ImBuf *ibuf, int save_as_render, int
ibuf->mall |= IB_rectfloat; ibuf->mall |= IB_rectfloat;
} }
} }
}
/* If we're saving from RGBA to RGB buffer then it's not
* so much useful to just ignore alpha -- it leads to bad
* artifacts especially when saving byte images.
*
* What we do here is we're overing our image on top of
* background color (which is currently black).
*
* This is quite much the same as what Gimp does and it
* seems to be what artists expects from saving.
*
* Do a conversion here, so image format writers could
* happily assume all the alpha tricks were made already.
* helps keep things locally here, not spreading it to
* all possible image writers we've got.
*/
if (do_alpha_under) {
float color[3] = {0, 0, 0};
if (colormanaged_ibuf->rect_float && colormanaged_ibuf->channels == 4) {
IMB_alpha_under_color_float(colormanaged_ibuf->rect_float, colormanaged_ibuf->x,
colormanaged_ibuf->y, color);
}
if (colormanaged_ibuf->rect) {
IMB_alpha_under_color_byte((unsigned char *)colormanaged_ibuf->rect,
colormanaged_ibuf->x, colormanaged_ibuf->y,
color);
}
}
if (do_colormanagement) {
int make_byte = FALSE;
ImFileType *type;
/* for proper check whether byte buffer is required by a format or not /* for proper check whether byte buffer is required by a format or not
* should be pretty safe since this image buffer is supposed to be used for * should be pretty safe since this image buffer is supposed to be used for

View File

@@ -367,9 +367,9 @@ void IMB_alpha_under_color_byte(unsigned char *rect, int x, int y, float backcol
else { else {
int mul = 255 - cp[3]; int mul = 255 - cp[3];
cp[0] += mul * backcol[0] / 255; cp[0] = (cp[0] * cp[3] >> 8) + mul * backcol[0] / 255;
cp[1] += mul * backcol[1] / 255; cp[1] = (cp[1] * cp[3] >> 8) + mul * backcol[1] / 255;
cp[2] += mul * backcol[2] / 255; cp[2] = (cp[2] * cp[3] >> 8) + mul * backcol[2] / 255;
} }
cp[3] = 255; cp[3] = 255;