Skip to content

Commit

Permalink
Merge pull request #139 from aferust/master
Browse files Browse the repository at this point in the history
fix imwrite for single channel images
  • Loading branch information
aferust authored Nov 9, 2022
2 parents d5f4564 + 29b4c65 commit 6d18a35
Showing 1 changed file with 24 additions and 12 deletions.
36 changes: 24 additions & 12 deletions source/dcv/imageio/image.d
Original file line number Diff line number Diff line change
Expand Up @@ -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]);
Expand Down Expand Up @@ -544,4 +556,4 @@ unittest
// should enter here...
}
}
*/
*/

0 comments on commit 6d18a35

Please sign in to comment.