diff --git a/src/calibre/utils/imageops/ordered_dither.cpp b/src/calibre/utils/imageops/ordered_dither.cpp index abf9c46c60..dcf1be5d82 100644 --- a/src/calibre/utils/imageops/ordered_dither.cpp +++ b/src/calibre/utils/imageops/ordered_dither.cpp @@ -70,9 +70,9 @@ static uint8_t QImage ordered_dither(const QImage &image) { // {{{ ScopedGILRelease PyGILRelease; QImage img = image; - QRgb *row = NULL, *pixel = NULL; int y = 0, x = 0, width = img.width(), height = img.height(); 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... if (img.format() != QImage::Format_RGB32) { @@ -81,13 +81,15 @@ QImage ordered_dither(const QImage &image) { // {{{ } for (y = 0; y < height; y++) { - row = reinterpret_cast(img.scanLine(y)); - for (x = 0, pixel = row; x < width; x++, pixel++) { + const QRgb *src_row = reinterpret_cast(img.constScanLine(y)); + 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 gray = qRed(*pixel); dithered = dither_o8x8(x, y, gray); - *pixel = qRgb(dithered, dithered, dithered); + *(dst_row + x) = dithered; } } - return img; + return dst; } // }}} diff --git a/src/calibre/utils/img.py b/src/calibre/utils/img.py index cf4b26a9d7..d66cf20753 100644 --- a/src/calibre/utils/img.py +++ b/src/calibre/utils/img.py @@ -463,10 +463,14 @@ def quantize_image(img, max_colors=256, dither=True, palette=''): def eink_dither_image(img): ''' Dither the source image down to the eInk palette of 16 shades of grey, 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) - if img.hasAlphaChannel(): - img = blend_image(img) return imageops.ordered_dither(img) # }}}