1 module jpeg_turbod.decompress; 2 3 import jpeg_turbod.libjpeg_turbo; 4 5 class Decompressor 6 { 7 private 8 { 9 tjhandle decompressor; 10 int width, height, jpegSubsamp, jpegColorspace; 11 enum int rgbPixelSize = 3; 12 } 13 14 this() 15 { 16 decompressor = tjInitDecompress(); 17 } 18 19 ~this() 20 { 21 tjDestroy(decompressor); 22 } 23 24 final char[] errorInfo() 25 { 26 import std.string : fromStringz; 27 28 return fromStringz(tjGetErrorStr2(decompressor)); 29 } 30 31 final bool decompress(in ubyte[] jpeg, ref ubyte[] pixels, out int width, out int height) 32 { 33 auto jpegLength = cast(int) jpeg.length; 34 35 auto result = tjDecompressHeader3(decompressor, jpeg.ptr, jpegLength, 36 &width, &height, &jpegSubsamp, &jpegColorspace); 37 38 if (result == -1) 39 { 40 return false; 41 } 42 43 width = width; 44 height = height; 45 46 if (pixels.length != width * height * rgbPixelSize) 47 { 48 pixels.length = width * height * rgbPixelSize; 49 } 50 51 result = tjDecompress2(decompressor, jpeg.ptr, jpegLength, pixels.ptr, 52 width, 0, height, TJPF.TJPF_RGB, TJFLAG_ACCURATEDCT); 53 54 return result == 0; 55 } 56 }