Inline the grayscaling pass in ordered_dither

Saving one QImage pixel-loop dance in the process...
This commit is contained in:
NiLuJe 2019-05-24 20:06:51 +02:00
parent 4326bb9d7f
commit 8e7b519b8d
2 changed files with 12 additions and 6 deletions

View File

@ -109,13 +109,19 @@ QImage ordered_dither(const QImage &image) { // {{{
if (img.isNull()) throw std::bad_alloc(); if (img.isNull()) throw std::bad_alloc();
} }
const bool is_gray = img.isGrayscale();
for (y = 0; y < height; y++) { for (y = 0; y < height; y++) {
const QRgb *src_row = reinterpret_cast<const QRgb*>(img.constScanLine(y)); const QRgb *src_row = reinterpret_cast<const QRgb*>(img.constScanLine(y));
uint8_t *dst_row = dst.scanLine(y); uint8_t *dst_row = dst.scanLine(y);
for (x = 0; x < width; x++) { for (x = 0; x < width; x++) {
const QRgb pixel = *(src_row + x); const QRgb pixel = *(src_row + x);
// We're running behind grayscale_image, so R = G = B if (is_gray) {
gray = qRed(pixel); // Grayscale and RGB32, so R = G = B
gray = qRed(pixel);
} else {
gray = qGray(pixel);
}
dithered = dither_o8x8(x, y, gray); dithered = dither_o8x8(x, y, gray);
*(dst_row + x) = dithered; // ... or palette.indexOf(dithered); for Indexed8 *(dst_row + x) = dithered; // ... or palette.indexOf(dithered); for Indexed8
} }

View File

@ -227,7 +227,7 @@ def save_cover_data_to(data, path=None, bgcolor='#ffffff', resize_to=None, compr
if img.hasAlphaChannel(): if img.hasAlphaChannel():
changed = True changed = True
img = blend_image(img, bgcolor) img = blend_image(img, bgcolor)
if grayscale: if grayscale and not eink:
if not img.allGray(): if not img.allGray():
changed = True changed = True
img = grayscale_image(img) img = grayscale_image(img)
@ -464,13 +464,13 @@ def eink_dither_image(img):
''' Dither the source image down to the eInk palette of 16 shades of grey, ''' Dither the source image down to the eInk palette of 16 shades of grey,
using ImageMagick's OrderedDither algorithm. using ImageMagick's OrderedDither algorithm.
NOTE: Expects input as a grayscale image in RGB32 pixel format (as returned by grayscale_image). NOTE: No need to call grayscale_image first, as this will inline a grayscaling pass if need be.
Running blend_image if the image has an alpha channel,
or grayscale_image if it's not already grayscaled is the caller's responsibility.
Returns a QImage in Grayscale8 pixel format. Returns a QImage in Grayscale8 pixel format.
''' '''
img = image_from_data(img) img = image_from_data(img)
if img.hasAlphaChannel():
img = blend_image(img)
return imageops.ordered_dither(img) return imageops.ordered_dither(img)
# }}} # }}}