Return an Indexed8 QImage

Ensures it'll be encoded as such when saved as a PNG
This commit is contained in:
NiLuJe 2019-05-24 01:14:47 +02:00
parent 7e6347486b
commit 90ab7573d9
2 changed files with 13 additions and 7 deletions

View File

@ -70,9 +70,9 @@ static uint8_t
QImage ordered_dither(const QImage &image) { // {{{ QImage ordered_dither(const QImage &image) { // {{{
ScopedGILRelease PyGILRelease; ScopedGILRelease PyGILRelease;
QImage img = image; QImage img = image;
QRgb *row = NULL, *pixel = NULL;
int y = 0, x = 0, width = img.width(), height = img.height(); int y = 0, x = 0, width = img.width(), height = img.height();
uint8_t gray = 0, dithered = 0; uint8_t gray = 0, dithered = 0;
QImage dst(width, height, QImage::Format_Indexed8);
// We're running behind blend_image, so, we should only ever be fed RGB32 as input... // We're running behind blend_image, so, we should only ever be fed RGB32 as input...
if (img.format() != QImage::Format_RGB32) { if (img.format() != QImage::Format_RGB32) {
@ -81,13 +81,15 @@ QImage ordered_dither(const QImage &image) { // {{{
} }
for (y = 0; y < height; y++) { for (y = 0; y < height; y++) {
row = reinterpret_cast<QRgb*>(img.scanLine(y)); const QRgb *src_row = reinterpret_cast<const QRgb*>(img.constScanLine(y));
for (x = 0, pixel = row; x < width; x++, pixel++) { uint8_t *dst_row = dst.scanLine(r);
for (x = 0; x < width; x++) {
const QRgb pixel = *(src_row + x);
// We're running behind grayscale_image, so R = G = B // We're running behind grayscale_image, so R = G = B
gray = qRed(*pixel); gray = qRed(*pixel);
dithered = dither_o8x8(x, y, gray); dithered = dither_o8x8(x, y, gray);
*pixel = qRgb(dithered, dithered, dithered); *(dst_row + x) = dithered;
} }
} }
return img; return dst;
} // }}} } // }}}

View File

@ -463,10 +463,14 @@ def quantize_image(img, max_colors=256, dither=True, palette=''):
def eink_dither_image(img): 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).
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 Indexed8 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)
# }}} # }}}