From 29b4c65afd5a7792f853b3a2309b2efeb6e878d0 Mon Sep 17 00:00:00 2001 From: aferust Date: Wed, 9 Nov 2022 21:35:51 +0300 Subject: [PATCH] fix imwrite for single channel images --- source/dcv/imageio/image.d | 36 ++++++++++++++++++++++++------------ 1 file changed, 24 insertions(+), 12 deletions(-) diff --git a/source/dcv/imageio/image.d b/source/dcv/imageio/image.d index ceaa57a..e3f1c21 100644 --- a/source/dcv/imageio/image.d +++ b/source/dcv/imageio/image.d @@ -141,20 +141,32 @@ bool imwrite(in string path, size_t width, size_t height, ImageFormat format, Bi if (depth == BitDepth.BD_8) { GamutImage gimage = GamutImage(cast(int)width, cast(int)height, gamutPixelTypeFromFormat[format]); - - size_t kk; - for (int y = 0; y < gimage.height(); ++y) - { - ubyte* scan = cast(ubyte*) gimage.scanline(y); - for (int x = 0; x < gimage.width(); ++x) + + if(data.length == width * height){ // single channel + size_t kk; + for (int y = 0; y < gimage.height(); ++y){ + ubyte* scan = cast(ubyte*) gimage.scanline(y); + for (int x = 0; x < gimage.width(); ++x){ + scan[x] = data[kk++]; + } + } + }else if(data.length == width * height * 3){ // 3 channels + size_t kk; + for (int y = 0; y < gimage.height(); ++y) { - scan[3*x + 0] = data[kk++]; - scan[3*x + 1] = data[kk++]; - scan[3*x + 2] = data[kk++]; - + ubyte* scan = cast(ubyte*) gimage.scanline(y); + for (int x = 0; x < gimage.width(); ++x) + { + scan[3*x + 0] = data[kk++]; + scan[3*x + 1] = data[kk++]; + scan[3*x + 2] = data[kk++]; + + } } + }else { + throw new Exception("Only 1 and 3 channel images can be written."); } - + if (!gimage.saveToFile(path)) throw new Exception("Writing " ~ path ~ " failed"); // write_image(path, cast(long)width, cast(long)height, data, imageFormatChannelCount[format]); @@ -544,4 +556,4 @@ unittest // should enter here... } } -*/ \ No newline at end of file +*/