annotate semiconginev2/image.nim @ 1243:7e55fde39ca8

did: prepare for gltf importer and cleanup old engine code
author sam <sam@basx.dev>
date Mon, 22 Jul 2024 17:49:48 +0700
parents e8b3dc80e48e
children c15770761865
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1242
e8b3dc80e48e add: PNG loading and tests for it
sam <sam@basx.dev>
parents:
diff changeset
1 type
e8b3dc80e48e add: PNG loading and tests for it
sam <sam@basx.dev>
parents:
diff changeset
2 Gray* = TVec1[uint8]
e8b3dc80e48e add: PNG loading and tests for it
sam <sam@basx.dev>
parents:
diff changeset
3 RGBA* = TVec4[uint8]
e8b3dc80e48e add: PNG loading and tests for it
sam <sam@basx.dev>
parents:
diff changeset
4 PixelType* = Gray | RGBA
e8b3dc80e48e add: PNG loading and tests for it
sam <sam@basx.dev>
parents:
diff changeset
5 Image*[T: PixelType] = object
e8b3dc80e48e add: PNG loading and tests for it
sam <sam@basx.dev>
parents:
diff changeset
6 width*: uint32
e8b3dc80e48e add: PNG loading and tests for it
sam <sam@basx.dev>
parents:
diff changeset
7 height*: uint32
e8b3dc80e48e add: PNG loading and tests for it
sam <sam@basx.dev>
parents:
diff changeset
8 interpolation*: VkFilter = VK_FILTER_LINEAR
e8b3dc80e48e add: PNG loading and tests for it
sam <sam@basx.dev>
parents:
diff changeset
9 data*: seq[T]
e8b3dc80e48e add: PNG loading and tests for it
sam <sam@basx.dev>
parents:
diff changeset
10 vk*: VkImage
e8b3dc80e48e add: PNG loading and tests for it
sam <sam@basx.dev>
parents:
diff changeset
11 imageview*: VkImageView
e8b3dc80e48e add: PNG loading and tests for it
sam <sam@basx.dev>
parents:
diff changeset
12 sampler*: VkSampler
e8b3dc80e48e add: PNG loading and tests for it
sam <sam@basx.dev>
parents:
diff changeset
13 isRenderTarget*: bool = false
e8b3dc80e48e add: PNG loading and tests for it
sam <sam@basx.dev>
parents:
diff changeset
14 samples*: VkSampleCountFlagBits = VK_SAMPLE_COUNT_1_BIT
e8b3dc80e48e add: PNG loading and tests for it
sam <sam@basx.dev>
parents:
diff changeset
15
e8b3dc80e48e add: PNG loading and tests for it
sam <sam@basx.dev>
parents:
diff changeset
16 proc LoadImage*[T: PixelType](path: string, package = DEFAULT_PACKAGE): Image[T] =
e8b3dc80e48e add: PNG loading and tests for it
sam <sam@basx.dev>
parents:
diff changeset
17 assert path.splitFile().ext.toLowerAscii == ".png"
e8b3dc80e48e add: PNG loading and tests for it
sam <sam@basx.dev>
parents:
diff changeset
18 when T is Gray:
e8b3dc80e48e add: PNG loading and tests for it
sam <sam@basx.dev>
parents:
diff changeset
19 let pngType = 0.cint
e8b3dc80e48e add: PNG loading and tests for it
sam <sam@basx.dev>
parents:
diff changeset
20 elif T is RGBA:
e8b3dc80e48e add: PNG loading and tests for it
sam <sam@basx.dev>
parents:
diff changeset
21 let pngType = 6.cint
e8b3dc80e48e add: PNG loading and tests for it
sam <sam@basx.dev>
parents:
diff changeset
22
e8b3dc80e48e add: PNG loading and tests for it
sam <sam@basx.dev>
parents:
diff changeset
23 let indata = loadResource_intern(path, package = package).readAll()
e8b3dc80e48e add: PNG loading and tests for it
sam <sam@basx.dev>
parents:
diff changeset
24 var w, h: cuint
e8b3dc80e48e add: PNG loading and tests for it
sam <sam@basx.dev>
parents:
diff changeset
25 var data: cstring
e8b3dc80e48e add: PNG loading and tests for it
sam <sam@basx.dev>
parents:
diff changeset
26
e8b3dc80e48e add: PNG loading and tests for it
sam <sam@basx.dev>
parents:
diff changeset
27 if lodepng_decode_memory(out_data = addr(data), w = addr(w), h = addr(h), in_data = cstring(indata), insize = csize_t(indata.len), colorType = pngType, bitdepth = 8) != 0:
e8b3dc80e48e add: PNG loading and tests for it
sam <sam@basx.dev>
parents:
diff changeset
28 raise newException(Exception, "An error occured while loading PNG file")
e8b3dc80e48e add: PNG loading and tests for it
sam <sam@basx.dev>
parents:
diff changeset
29
e8b3dc80e48e add: PNG loading and tests for it
sam <sam@basx.dev>
parents:
diff changeset
30 let imagesize = w * h * 4
e8b3dc80e48e add: PNG loading and tests for it
sam <sam@basx.dev>
parents:
diff changeset
31 result = Image[T](width: w, height: h, data: newSeq[T](w * h))
e8b3dc80e48e add: PNG loading and tests for it
sam <sam@basx.dev>
parents:
diff changeset
32 copyMem(result.data.ToCPointer, data, imagesize)
e8b3dc80e48e add: PNG loading and tests for it
sam <sam@basx.dev>
parents:
diff changeset
33 nativeFree(data)
e8b3dc80e48e add: PNG loading and tests for it
sam <sam@basx.dev>
parents:
diff changeset
34
e8b3dc80e48e add: PNG loading and tests for it
sam <sam@basx.dev>
parents:
diff changeset
35 when T is RGBA: # converkt to BGRA
e8b3dc80e48e add: PNG loading and tests for it
sam <sam@basx.dev>
parents:
diff changeset
36 for i in 0 ..< result.data.len:
e8b3dc80e48e add: PNG loading and tests for it
sam <sam@basx.dev>
parents:
diff changeset
37 swap(result.data[i][0], result.data[i][2])
e8b3dc80e48e add: PNG loading and tests for it
sam <sam@basx.dev>
parents:
diff changeset
38
e8b3dc80e48e add: PNG loading and tests for it
sam <sam@basx.dev>
parents:
diff changeset
39 proc toPNG[T: PixelType](image: Image[T]): seq[uint8] =
e8b3dc80e48e add: PNG loading and tests for it
sam <sam@basx.dev>
parents:
diff changeset
40 when T is Gray:
e8b3dc80e48e add: PNG loading and tests for it
sam <sam@basx.dev>
parents:
diff changeset
41 let pngType = 0 # hardcoded in lodepng.h
e8b3dc80e48e add: PNG loading and tests for it
sam <sam@basx.dev>
parents:
diff changeset
42 else:
e8b3dc80e48e add: PNG loading and tests for it
sam <sam@basx.dev>
parents:
diff changeset
43 let pngType = 6 # hardcoded in lodepng.h
e8b3dc80e48e add: PNG loading and tests for it
sam <sam@basx.dev>
parents:
diff changeset
44 var
e8b3dc80e48e add: PNG loading and tests for it
sam <sam@basx.dev>
parents:
diff changeset
45 pngData: cstring
e8b3dc80e48e add: PNG loading and tests for it
sam <sam@basx.dev>
parents:
diff changeset
46 pngSize: csize_t
e8b3dc80e48e add: PNG loading and tests for it
sam <sam@basx.dev>
parents:
diff changeset
47 for y in 0 ..< image.height:
e8b3dc80e48e add: PNG loading and tests for it
sam <sam@basx.dev>
parents:
diff changeset
48 for x in 0 ..< image.width:
e8b3dc80e48e add: PNG loading and tests for it
sam <sam@basx.dev>
parents:
diff changeset
49 discard
e8b3dc80e48e add: PNG loading and tests for it
sam <sam@basx.dev>
parents:
diff changeset
50 let ret = lodepng_encode_memory(
e8b3dc80e48e add: PNG loading and tests for it
sam <sam@basx.dev>
parents:
diff changeset
51 addr pngData,
e8b3dc80e48e add: PNG loading and tests for it
sam <sam@basx.dev>
parents:
diff changeset
52 addr pngSize,
e8b3dc80e48e add: PNG loading and tests for it
sam <sam@basx.dev>
parents:
diff changeset
53 cast[cstring](image.imagedata.ToCPointer),
e8b3dc80e48e add: PNG loading and tests for it
sam <sam@basx.dev>
parents:
diff changeset
54 cuint(image.width),
e8b3dc80e48e add: PNG loading and tests for it
sam <sam@basx.dev>
parents:
diff changeset
55 cuint(image.height),
e8b3dc80e48e add: PNG loading and tests for it
sam <sam@basx.dev>
parents:
diff changeset
56 cint(pngType),
e8b3dc80e48e add: PNG loading and tests for it
sam <sam@basx.dev>
parents:
diff changeset
57 8,
e8b3dc80e48e add: PNG loading and tests for it
sam <sam@basx.dev>
parents:
diff changeset
58 )
e8b3dc80e48e add: PNG loading and tests for it
sam <sam@basx.dev>
parents:
diff changeset
59 assert ret == 0, &"There was an error with generating the PNG data for image {image}, result was: {ret}"
e8b3dc80e48e add: PNG loading and tests for it
sam <sam@basx.dev>
parents:
diff changeset
60 result = newSeq[uint8](pngSize)
e8b3dc80e48e add: PNG loading and tests for it
sam <sam@basx.dev>
parents:
diff changeset
61 for i in 0 ..< pngSize:
e8b3dc80e48e add: PNG loading and tests for it
sam <sam@basx.dev>
parents:
diff changeset
62 result[i] = uint8(pngData[i])
e8b3dc80e48e add: PNG loading and tests for it
sam <sam@basx.dev>
parents:
diff changeset
63 nativeFree(pngData)
e8b3dc80e48e add: PNG loading and tests for it
sam <sam@basx.dev>
parents:
diff changeset
64
e8b3dc80e48e add: PNG loading and tests for it
sam <sam@basx.dev>
parents:
diff changeset
65 proc WritePNG*(image: Image, filename: string) =
e8b3dc80e48e add: PNG loading and tests for it
sam <sam@basx.dev>
parents:
diff changeset
66 let f = filename.open(mode = fmWrite)
e8b3dc80e48e add: PNG loading and tests for it
sam <sam@basx.dev>
parents:
diff changeset
67 let data = image.toPNG()
e8b3dc80e48e add: PNG loading and tests for it
sam <sam@basx.dev>
parents:
diff changeset
68 let written = f.writeBytes(data, 0, data.len)
e8b3dc80e48e add: PNG loading and tests for it
sam <sam@basx.dev>
parents:
diff changeset
69 assert written == data.len, &"There was an error while saving '{filename}': only {written} of {data.len} bytes were written"
e8b3dc80e48e add: PNG loading and tests for it
sam <sam@basx.dev>
parents:
diff changeset
70 f.close()