Mercurial > games > semicongine
annotate semiconginev2/image.nim @ 1261:6deb5ceaa3fb
merge
author | sam <sam@basx.dev> |
---|---|
date | Sun, 28 Jul 2024 21:39:45 +0700 |
parents | 01e9f41d35b1 |
children |
rev | line source |
---|---|
1242 | 1 type |
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 | 5 Image*[T: PixelType] = object |
6 width*: uint32 | |
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 | 12 data*: seq[T] |
13 vk*: VkImage | |
14 imageview*: VkImageView | |
15 sampler*: VkSampler | |
16 isRenderTarget*: bool = false | |
17 samples*: VkSampleCountFlagBits = VK_SAMPLE_COUNT_1_BIT | |
18 | |
1252 | 19 proc LoadImageData*[T: PixelType](pngData: string|seq[uint8]): Image[T] = |
1242 | 20 when T is Gray: |
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 | 23 let pngType = 6.cint |
24 | |
25 var w, h: cuint | |
26 var data: cstring | |
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 | 29 raise newException(Exception, "An error occured while loading PNG file") |
30 | |
31 let imagesize = w * h * 4 | |
32 result = Image[T](width: w, height: h, data: newSeq[T](w * h)) | |
33 copyMem(result.data.ToCPointer, data, imagesize) | |
34 nativeFree(data) | |
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 | 37 for i in 0 ..< result.data.len: |
38 swap(result.data[i][0], result.data[i][2]) | |
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] = |
1252 | 41 assert path.splitFile().ext.toLowerAscii == ".png", "Unsupported image type: " & path.splitFile().ext.toLowerAscii |
1247
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 |
1252 | 47 result = LoadImageData[T](loadResource_intern(path, package = package).readAll()) |
1247
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 | 50 proc toPNG[T: PixelType](image: Image[T]): seq[uint8] = |
51 when T is Gray: | |
52 let pngType = 0 # hardcoded in lodepng.h | |
53 else: | |
54 let pngType = 6 # hardcoded in lodepng.h | |
55 var | |
56 pngData: cstring | |
57 pngSize: csize_t | |
58 for y in 0 ..< image.height: | |
59 for x in 0 ..< image.width: | |
60 discard | |
61 let ret = lodepng_encode_memory( | |
62 addr pngData, | |
63 addr pngSize, | |
64 cast[cstring](image.imagedata.ToCPointer), | |
65 cuint(image.width), | |
66 cuint(image.height), | |
67 cint(pngType), | |
68 8, | |
69 ) | |
70 assert ret == 0, &"There was an error with generating the PNG data for image {image}, result was: {ret}" | |
71 result = newSeq[uint8](pngSize) | |
72 for i in 0 ..< pngSize: | |
73 result[i] = uint8(pngData[i]) | |
74 nativeFree(pngData) | |
75 | |
76 proc WritePNG*(image: Image, filename: string) = | |
77 let f = filename.open(mode = fmWrite) | |
78 let data = image.toPNG() | |
79 let written = f.writeBytes(data, 0, data.len) | |
80 assert written == data.len, &"There was an error while saving '{filename}': only {written} of {data.len} bytes were written" | |
81 f.close() |