annotate semiconginev2/image.nim @ 1251:3f98ad20a9d3

add: render by-node instead of by-mesh
author sam <sam@basx.dev>
date Fri, 26 Jul 2024 20:34:02 +0700
parents c15770761865
children 01e9f41d35b1
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]
1247
c15770761865 add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents: 1242
diff changeset
3 BGRA* = TVec4[uint8]
c15770761865 add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents: 1242
diff changeset
4 PixelType* = Gray | BGRA
1242
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
1247
c15770761865 add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents: 1242
diff changeset
8 minInterpolation*: VkFilter = VK_FILTER_LINEAR
c15770761865 add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents: 1242
diff changeset
9 magInterpolation*: VkFilter = VK_FILTER_LINEAR
c15770761865 add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents: 1242
diff changeset
10 wrapU: VkSamplerAddressMode = VK_SAMPLER_ADDRESS_MODE_REPEAT
c15770761865 add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents: 1242
diff changeset
11 wrapV: VkSamplerAddressMode = VK_SAMPLER_ADDRESS_MODE_REPEAT
1242
e8b3dc80e48e add: PNG loading and tests for it
sam <sam@basx.dev>
parents:
diff changeset
12 data*: seq[T]
e8b3dc80e48e add: PNG loading and tests for it
sam <sam@basx.dev>
parents:
diff changeset
13 vk*: VkImage
e8b3dc80e48e add: PNG loading and tests for it
sam <sam@basx.dev>
parents:
diff changeset
14 imageview*: VkImageView
e8b3dc80e48e add: PNG loading and tests for it
sam <sam@basx.dev>
parents:
diff changeset
15 sampler*: VkSampler
e8b3dc80e48e add: PNG loading and tests for it
sam <sam@basx.dev>
parents:
diff changeset
16 isRenderTarget*: bool = false
e8b3dc80e48e add: PNG loading and tests for it
sam <sam@basx.dev>
parents:
diff changeset
17 samples*: VkSampleCountFlagBits = VK_SAMPLE_COUNT_1_BIT
e8b3dc80e48e add: PNG loading and tests for it
sam <sam@basx.dev>
parents:
diff changeset
18
1247
c15770761865 add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents: 1242
diff changeset
19 proc LoadImage*[T: PixelType](pngData: seq[uint8]): Image[T] =
1242
e8b3dc80e48e add: PNG loading and tests for it
sam <sam@basx.dev>
parents:
diff changeset
20 when T is Gray:
e8b3dc80e48e add: PNG loading and tests for it
sam <sam@basx.dev>
parents:
diff changeset
21 let pngType = 0.cint
1247
c15770761865 add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents: 1242
diff changeset
22 elif T is BGRA:
1242
e8b3dc80e48e add: PNG loading and tests for it
sam <sam@basx.dev>
parents:
diff changeset
23 let pngType = 6.cint
e8b3dc80e48e add: PNG loading and tests for it
sam <sam@basx.dev>
parents:
diff changeset
24
e8b3dc80e48e add: PNG loading and tests for it
sam <sam@basx.dev>
parents:
diff changeset
25 var w, h: cuint
e8b3dc80e48e add: PNG loading and tests for it
sam <sam@basx.dev>
parents:
diff changeset
26 var data: cstring
e8b3dc80e48e add: PNG loading and tests for it
sam <sam@basx.dev>
parents:
diff changeset
27
1247
c15770761865 add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents: 1242
diff changeset
28 if lodepng_decode_memory(out_data = addr(data), w = addr(w), h = addr(h), in_data = cast[cstring](pngData.ToCPointer), insize = csize_t(pngData.len), colorType = pngType, bitdepth = 8) != 0:
1242
e8b3dc80e48e add: PNG loading and tests for it
sam <sam@basx.dev>
parents:
diff changeset
29 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
30
e8b3dc80e48e add: PNG loading and tests for it
sam <sam@basx.dev>
parents:
diff changeset
31 let imagesize = w * h * 4
e8b3dc80e48e add: PNG loading and tests for it
sam <sam@basx.dev>
parents:
diff changeset
32 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
33 copyMem(result.data.ToCPointer, data, imagesize)
e8b3dc80e48e add: PNG loading and tests for it
sam <sam@basx.dev>
parents:
diff changeset
34 nativeFree(data)
e8b3dc80e48e add: PNG loading and tests for it
sam <sam@basx.dev>
parents:
diff changeset
35
1247
c15770761865 add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents: 1242
diff changeset
36 when T is BGRA: # converkt to BGRA
1242
e8b3dc80e48e add: PNG loading and tests for it
sam <sam@basx.dev>
parents:
diff changeset
37 for i in 0 ..< result.data.len:
e8b3dc80e48e add: PNG loading and tests for it
sam <sam@basx.dev>
parents:
diff changeset
38 swap(result.data[i][0], result.data[i][2])
e8b3dc80e48e add: PNG loading and tests for it
sam <sam@basx.dev>
parents:
diff changeset
39
1247
c15770761865 add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents: 1242
diff changeset
40 proc LoadImage*[T: PixelType](path: string, package = DEFAULT_PACKAGE): Image[T] =
c15770761865 add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents: 1242
diff changeset
41 assert path.splitFile().ext.toLowerAscii == ".png"
c15770761865 add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents: 1242
diff changeset
42 when T is Gray:
c15770761865 add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents: 1242
diff changeset
43 let pngType = 0.cint
c15770761865 add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents: 1242
diff changeset
44 elif T is BGRA:
c15770761865 add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents: 1242
diff changeset
45 let pngType = 6.cint
c15770761865 add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents: 1242
diff changeset
46
c15770761865 add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents: 1242
diff changeset
47 result = LoadImage[T](loadResource_intern(path, package = package).readAll())
c15770761865 add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents: 1242
diff changeset
48
c15770761865 add: gltf loading test, gltf loading for materials
sam <sam@basx.dev>
parents: 1242
diff changeset
49
1242
e8b3dc80e48e add: PNG loading and tests for it
sam <sam@basx.dev>
parents:
diff changeset
50 proc toPNG[T: PixelType](image: Image[T]): seq[uint8] =
e8b3dc80e48e add: PNG loading and tests for it
sam <sam@basx.dev>
parents:
diff changeset
51 when T is Gray:
e8b3dc80e48e add: PNG loading and tests for it
sam <sam@basx.dev>
parents:
diff changeset
52 let pngType = 0 # hardcoded in lodepng.h
e8b3dc80e48e add: PNG loading and tests for it
sam <sam@basx.dev>
parents:
diff changeset
53 else:
e8b3dc80e48e add: PNG loading and tests for it
sam <sam@basx.dev>
parents:
diff changeset
54 let pngType = 6 # hardcoded in lodepng.h
e8b3dc80e48e add: PNG loading and tests for it
sam <sam@basx.dev>
parents:
diff changeset
55 var
e8b3dc80e48e add: PNG loading and tests for it
sam <sam@basx.dev>
parents:
diff changeset
56 pngData: cstring
e8b3dc80e48e add: PNG loading and tests for it
sam <sam@basx.dev>
parents:
diff changeset
57 pngSize: csize_t
e8b3dc80e48e add: PNG loading and tests for it
sam <sam@basx.dev>
parents:
diff changeset
58 for y in 0 ..< image.height:
e8b3dc80e48e add: PNG loading and tests for it
sam <sam@basx.dev>
parents:
diff changeset
59 for x in 0 ..< image.width:
e8b3dc80e48e add: PNG loading and tests for it
sam <sam@basx.dev>
parents:
diff changeset
60 discard
e8b3dc80e48e add: PNG loading and tests for it
sam <sam@basx.dev>
parents:
diff changeset
61 let ret = lodepng_encode_memory(
e8b3dc80e48e add: PNG loading and tests for it
sam <sam@basx.dev>
parents:
diff changeset
62 addr pngData,
e8b3dc80e48e add: PNG loading and tests for it
sam <sam@basx.dev>
parents:
diff changeset
63 addr pngSize,
e8b3dc80e48e add: PNG loading and tests for it
sam <sam@basx.dev>
parents:
diff changeset
64 cast[cstring](image.imagedata.ToCPointer),
e8b3dc80e48e add: PNG loading and tests for it
sam <sam@basx.dev>
parents:
diff changeset
65 cuint(image.width),
e8b3dc80e48e add: PNG loading and tests for it
sam <sam@basx.dev>
parents:
diff changeset
66 cuint(image.height),
e8b3dc80e48e add: PNG loading and tests for it
sam <sam@basx.dev>
parents:
diff changeset
67 cint(pngType),
e8b3dc80e48e add: PNG loading and tests for it
sam <sam@basx.dev>
parents:
diff changeset
68 8,
e8b3dc80e48e add: PNG loading and tests for it
sam <sam@basx.dev>
parents:
diff changeset
69 )
e8b3dc80e48e add: PNG loading and tests for it
sam <sam@basx.dev>
parents:
diff changeset
70 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
71 result = newSeq[uint8](pngSize)
e8b3dc80e48e add: PNG loading and tests for it
sam <sam@basx.dev>
parents:
diff changeset
72 for i in 0 ..< pngSize:
e8b3dc80e48e add: PNG loading and tests for it
sam <sam@basx.dev>
parents:
diff changeset
73 result[i] = uint8(pngData[i])
e8b3dc80e48e add: PNG loading and tests for it
sam <sam@basx.dev>
parents:
diff changeset
74 nativeFree(pngData)
e8b3dc80e48e add: PNG loading and tests for it
sam <sam@basx.dev>
parents:
diff changeset
75
e8b3dc80e48e add: PNG loading and tests for it
sam <sam@basx.dev>
parents:
diff changeset
76 proc WritePNG*(image: Image, filename: string) =
e8b3dc80e48e add: PNG loading and tests for it
sam <sam@basx.dev>
parents:
diff changeset
77 let f = filename.open(mode = fmWrite)
e8b3dc80e48e add: PNG loading and tests for it
sam <sam@basx.dev>
parents:
diff changeset
78 let data = image.toPNG()
e8b3dc80e48e add: PNG loading and tests for it
sam <sam@basx.dev>
parents:
diff changeset
79 let written = f.writeBytes(data, 0, data.len)
e8b3dc80e48e add: PNG loading and tests for it
sam <sam@basx.dev>
parents:
diff changeset
80 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
81 f.close()