From e2e8a917e5cd8e1049caf3a43a398f9afee03785 Mon Sep 17 00:00:00 2001 From: "Adam D. Ruppe" Date: Sun, 6 Jan 2019 17:28:34 -0500 Subject: [PATCH] lol --- png.d | 37 +++++++++++++++++++++++++++++-------- 1 file changed, 29 insertions(+), 8 deletions(-) diff --git a/png.d b/png.d index 83ff420..4c417b6 100644 --- a/png.d +++ b/png.d @@ -25,17 +25,19 @@ void writePng(string filename, MemoryImage mi) { /// enum PngType { greyscale = 0, /// The data must be `depth` bits per pixel - truecolor = 2, /// The data will be RGB triples, so `depth * 3` bits per pixel - indexed = 3, /// The data must be `depth` bits per pixel, with a palette attached. Use [writePng] with [IndexedImage] for this mode - greyscale_with_alpha = 4, /// The data must be (grey, alpha) byte pairs for each pixel. Thus `depth * 2` bits per pixel - truecolor_with_alpha = 6 /// The data must be RGBA quads for each pixel. Thus, `depth * 4` bits per pixel. + truecolor = 2, /// The data will be RGB triples, so `depth * 3` bits per pixel. Depth must be 8 or 16. + indexed = 3, /// The data must be `depth` bits per pixel, with a palette attached. Use [writePng] with [IndexedImage] for this mode. Depth must be <= 8. + greyscale_with_alpha = 4, /// The data must be (grey, alpha) byte pairs for each pixel. Thus `depth * 2` bits per pixel. Depth must be 8 or 16. + truecolor_with_alpha = 6 /// The data must be RGBA quads for each pixel. Thus, `depth * 4` bits per pixel. Depth must be 8 or 16. } -/// Saves an image from an existing array. Note that depth other than 8 may not be implemented yet. +/// Saves an image from an existing array. Note that depth other than 8 may not be implemented yet. Also note depth of 16 must be stored big endian void writePng(string filename, const ubyte[] data, int width, int height, PngType type, ubyte depth = 8) { PngHeader h; h.width = width; h.height = height; + h.type = cast(ubyte) type; + h.depth = depth; auto png = blankPNG(h); addImageDatastreamToPng(data, png); @@ -652,9 +654,28 @@ void addImageDatastreamToPng(const(ubyte)[] data, PNG* png) { PngHeader h = getHeader(png); - auto bytesPerLine = h.width * 4; - if(h.type == 3) - bytesPerLine = h.width * h.depth / 8; + int bytesPerLine; + switch(h.type) { + case 0: + // FIXME: < 8 depth not supported here but should be + bytesPerLine = h.width * 1 * h.depth / 8; + break; + case 2: + bytesPerLine = h.width * 3 * h.depth / 8; + break; + case 3: + bytesPerLine = h.width * 1 * h.depth / 8; + break; + case 4: + // FIXME: < 8 depth not supported here but should be + bytesPerLine = h.width * 2 * h.depth / 8; + break; + case 6: + bytesPerLine = h.width * 4 * h.depth / 8; + break; + default: assert(0); + + } Chunk dat; dat.type = ['I', 'D', 'A', 'T']; int pos = 0;