Mercurial > games > semicongine
comparison semiconginev2/old/resources/image.nim @ 1218:56781cc0fc7c compiletime-tests
did: renamge main package
author | sam <sam@basx.dev> |
---|---|
date | Wed, 17 Jul 2024 21:01:37 +0700 |
parents | semicongine/old/resources/image.nim@a3eb305bcac2 |
children |
comparison
equal
deleted
inserted
replaced
1217:f819a874058f | 1218:56781cc0fc7c |
---|---|
1 import std/os | |
2 # import std/syncio | |
3 import std/streams | |
4 import std/bitops | |
5 import std/strformat | |
6 | |
7 import ../core/imagetypes | |
8 import ../core/utils | |
9 | |
10 const COMPRESSION_BI_RGB = 0'u32 | |
11 const COMPRESSION_BI_BITFIELDS = 3'u32 | |
12 const COMPRESSION_BI_ALPHABITFIELDS = 6'u32 | |
13 type | |
14 BitmapFileHeader = object | |
15 magicbytes: array[2, char] | |
16 filesize: uint32 | |
17 reserved1: uint16 | |
18 reserved2: uint16 | |
19 dataStart: uint32 | |
20 DIBHeader = object | |
21 headersize: uint32 | |
22 width: int32 | |
23 height: int32 | |
24 colorPlanes: uint16 | |
25 bitsPerPixel: uint16 | |
26 compression: uint32 | |
27 imageDataSize: uint32 # unused | |
28 resolutionX: int32 # unused | |
29 resolutionY: int32 # unused | |
30 nColors: uint32 # unused | |
31 nImportantColors: uint32 # unused | |
32 bitMaskRed: uint32 | |
33 bitMaskGreen: uint32 | |
34 bitMaskBlue: uint32 | |
35 bitMaskAlpha: uint32 | |
36 colorSpace: array[4, char] # not used yet | |
37 colorSpaceEndpoints: array[36, uint8] # unused | |
38 gammaRed: uint32 # not used yet | |
39 gammaGreen: uint32 # not used yet | |
40 gammaBlue: uint32 # not used yet | |
41 | |
42 proc ReadBMP*(stream: Stream): Image[RGBAPixel] = | |
43 var | |
44 bitmapFileHeader: BitmapFileHeader | |
45 dibHeader: DIBHeader | |
46 | |
47 for name, value in fieldPairs(bitmapFileHeader): | |
48 stream.read(value) | |
49 if bitmapFileHeader.magicbytes != ['B', 'M']: | |
50 raise newException(Exception, "Cannot open image, invalid magic bytes (is this really a BMP bitmap?)") | |
51 for name, value in fieldPairs(dibHeader): | |
52 | |
53 when name in ["bitMaskRed", "bitMaskGreen", "bitMaskBlue"]: | |
54 if dibHeader.compression in [COMPRESSION_BI_BITFIELDS, COMPRESSION_BI_ALPHABITFIELDS]: | |
55 stream.read(value) | |
56 elif name == "bitMaskAlpha": | |
57 if dibHeader.compression == COMPRESSION_BI_ALPHABITFIELDS: | |
58 stream.read(value) | |
59 else: | |
60 stream.read(value) | |
61 | |
62 when name == "headersize": | |
63 if value != 124: | |
64 raise newException(Exception, "Cannot open image, only BITMAPV5 supported") | |
65 elif name == "colorPlanes": | |
66 assert value == 1 | |
67 elif name == "bitsPerPixel": | |
68 if not (value in [24'u16, 32'u16]): | |
69 raise newException(Exception, "Cannot open image, only depth of 24 and 32 supported") | |
70 elif name == "compression": | |
71 if not (value in [0'u32, 3'u32]): | |
72 raise newException(Exception, "Cannot open image, only BI_RGB and BI_BITFIELDS are supported compressions") | |
73 elif name == "colorSpace": | |
74 swap(value[0], value[3]) | |
75 swap(value[1], value[2]) | |
76 stream.setPosition(int(bitmapFileHeader.dataStart)) | |
77 var | |
78 padding = ((int32(dibHeader.bitsPerPixel div 8)) * dibHeader.width) mod 4 | |
79 data = newSeq[RGBAPixel](dibHeader.width * abs(dibHeader.height)) | |
80 if padding > 0: | |
81 padding = 4 - padding | |
82 for row in 0 ..< abs(dibHeader.height): | |
83 for col in 0 ..< dibHeader.width: | |
84 | |
85 var pixel: RGBAPixel = [0'u8, 0'u8, 0'u8, 255'u8] | |
86 # if we got channeld bitmasks | |
87 if dibHeader.compression in [COMPRESSION_BI_BITFIELDS, COMPRESSION_BI_ALPHABITFIELDS]: | |
88 var value = stream.readUint32() | |
89 pixel[0] = uint8((value and dibHeader.bitMaskRed) shr dibHeader.bitMaskRed.countTrailingZeroBits) | |
90 pixel[1] = uint8((value and dibHeader.bitMaskGreen) shr dibHeader.bitMaskGreen.countTrailingZeroBits) | |
91 pixel[2] = uint8((value and dibHeader.bitMaskBlue) shr dibHeader.bitMaskBlue.countTrailingZeroBits) | |
92 if dibHeader.compression == COMPRESSION_BI_ALPHABITFIELDS: | |
93 pixel[3] = uint8((value and dibHeader.bitMaskAlpha) shr dibHeader.bitMaskAlpha.countTrailingZeroBits) | |
94 # if we got plain RGB(A), using little endian | |
95 elif dibHeader.compression == COMPRESSION_BI_RGB: | |
96 let nChannels = int(dibHeader.bitsPerPixel) div 8 | |
97 for i in 1 .. nChannels: | |
98 stream.read(pixel[nChannels - i]) | |
99 else: | |
100 raise newException(Exception, "Cannot open image, only BI_RGB and BI_BITFIELDS are supported compressions") | |
101 | |
102 # determine whether we read top-to-bottom or bottom-to-top | |
103 var row_mult: int = (if dibHeader.height < 0: row else: dibHeader.height - row - 1) | |
104 data[row_mult * dibHeader.width + col] = pixel | |
105 stream.setPosition(stream.getPosition() + padding) | |
106 | |
107 result = NewImage(width = dibHeader.width.uint32, height = abs(dibHeader.height).uint32, imagedata = data) | |
108 | |
109 {.compile: currentSourcePath.parentDir() & "/lodepng.c".} | |
110 | |
111 proc lodepng_decode32(out_data: ptr cstring, w: ptr cuint, h: ptr cuint, in_data: cstring, insize: csize_t): cuint {.importc.} | |
112 proc lodepng_encode_memory(out_data: ptr cstring, outsize: ptr csize_t, image: cstring, w: cuint, h: cuint, colorType: cint, bitdepth: cuint): cuint {.importc.} | |
113 | |
114 proc free(p: pointer) {.importc.} # for some reason the lodepng pointer can only properly be freed with the native free | |
115 | |
116 proc ReadPNG*(stream: Stream): Image[RGBAPixel] = | |
117 let indata = stream.readAll() | |
118 var w, h: cuint | |
119 var data: cstring | |
120 | |
121 if lodepng_decode32(out_data = addr data, w = addr w, h = addr h, in_data = cstring(indata), insize = csize_t(indata.len)) != 0: | |
122 raise newException(Exception, "An error occured while loading PNG file") | |
123 | |
124 let imagesize = w * h * 4 | |
125 var imagedata = newSeq[RGBAPixel](w * h) | |
126 copyMem(addr imagedata[0], data, imagesize) | |
127 | |
128 free(data) | |
129 | |
130 result = NewImage(width = w, height = h, imagedata = imagedata) | |
131 | |
132 proc ToPNG*[T: Pixel](image: Image[T]): seq[uint8] = | |
133 when T is GrayPixel: | |
134 let pngType = 0 # hardcoded in lodepng.h | |
135 else: | |
136 let pngType = 6 # hardcoded in lodepng.h | |
137 var | |
138 pngData: cstring | |
139 pngSize: csize_t | |
140 for y in 0 ..< image.height: | |
141 for x in 0 ..< image.width: | |
142 discard | |
143 let ret = lodepng_encode_memory( | |
144 addr pngData, | |
145 addr pngSize, | |
146 cast[cstring](image.imagedata.ToCPointer), | |
147 cuint(image.width), | |
148 cuint(image.height), | |
149 cint(pngType), | |
150 8, | |
151 ) | |
152 assert ret == 0, &"There was an error with generating the PNG data for image {image}, result was: {ret}" | |
153 result = newSeq[uint8](pngSize) | |
154 for i in 0 ..< pngSize: | |
155 result[i] = uint8(pngData[i]) | |
156 free(pngData) | |
157 | |
158 proc WritePNG*[T: Pixel](image: Image[T], filename: string) = | |
159 let f = filename.open(mode = fmWrite) | |
160 let data = image.toPNG() | |
161 let written = f.writeBytes(data, 0, data.len) | |
162 assert written == data.len, &"There was an error while saving '{filename}': only {written} of {data.len} bytes were written" | |
163 f.close() |