Mercurial > games > semicongine
comparison semiconginev2/image.nim @ 1247:c15770761865
add: gltf loading test, gltf loading for materials
| author | sam <sam@basx.dev> | 
|---|---|
| date | Wed, 24 Jul 2024 23:26:34 +0700 | 
| parents | e8b3dc80e48e | 
| children | 01e9f41d35b1 | 
   comparison
  equal
  deleted
  inserted
  replaced
| 1246:356089365076 | 1247:c15770761865 | 
|---|---|
| 1 type | 1 type | 
| 2 Gray* = TVec1[uint8] | 2 Gray* = TVec1[uint8] | 
| 3 RGBA* = TVec4[uint8] | 3 BGRA* = TVec4[uint8] | 
| 4 PixelType* = Gray | RGBA | 4 PixelType* = Gray | BGRA | 
| 5 Image*[T: PixelType] = object | 5 Image*[T: PixelType] = object | 
| 6 width*: uint32 | 6 width*: uint32 | 
| 7 height*: uint32 | 7 height*: uint32 | 
| 8 interpolation*: VkFilter = VK_FILTER_LINEAR | 8 minInterpolation*: VkFilter = VK_FILTER_LINEAR | 
| 9 magInterpolation*: VkFilter = VK_FILTER_LINEAR | |
| 10 wrapU: VkSamplerAddressMode = VK_SAMPLER_ADDRESS_MODE_REPEAT | |
| 11 wrapV: VkSamplerAddressMode = VK_SAMPLER_ADDRESS_MODE_REPEAT | |
| 9 data*: seq[T] | 12 data*: seq[T] | 
| 10 vk*: VkImage | 13 vk*: VkImage | 
| 11 imageview*: VkImageView | 14 imageview*: VkImageView | 
| 12 sampler*: VkSampler | 15 sampler*: VkSampler | 
| 13 isRenderTarget*: bool = false | 16 isRenderTarget*: bool = false | 
| 14 samples*: VkSampleCountFlagBits = VK_SAMPLE_COUNT_1_BIT | 17 samples*: VkSampleCountFlagBits = VK_SAMPLE_COUNT_1_BIT | 
| 15 | 18 | 
| 16 proc LoadImage*[T: PixelType](path: string, package = DEFAULT_PACKAGE): Image[T] = | 19 proc LoadImage*[T: PixelType](pngData: seq[uint8]): Image[T] = | 
| 17 assert path.splitFile().ext.toLowerAscii == ".png" | |
| 18 when T is Gray: | 20 when T is Gray: | 
| 19 let pngType = 0.cint | 21 let pngType = 0.cint | 
| 20 elif T is RGBA: | 22 elif T is BGRA: | 
| 21 let pngType = 6.cint | 23 let pngType = 6.cint | 
| 22 | 24 | 
| 23 let indata = loadResource_intern(path, package = package).readAll() | |
| 24 var w, h: cuint | 25 var w, h: cuint | 
| 25 var data: cstring | 26 var data: cstring | 
| 26 | 27 | 
| 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: | 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: | 
| 28 raise newException(Exception, "An error occured while loading PNG file") | 29 raise newException(Exception, "An error occured while loading PNG file") | 
| 29 | 30 | 
| 30 let imagesize = w * h * 4 | 31 let imagesize = w * h * 4 | 
| 31 result = Image[T](width: w, height: h, data: newSeq[T](w * h)) | 32 result = Image[T](width: w, height: h, data: newSeq[T](w * h)) | 
| 32 copyMem(result.data.ToCPointer, data, imagesize) | 33 copyMem(result.data.ToCPointer, data, imagesize) | 
| 33 nativeFree(data) | 34 nativeFree(data) | 
| 34 | 35 | 
| 35 when T is RGBA: # converkt to BGRA | 36 when T is BGRA: # converkt to BGRA | 
| 36 for i in 0 ..< result.data.len: | 37 for i in 0 ..< result.data.len: | 
| 37 swap(result.data[i][0], result.data[i][2]) | 38 swap(result.data[i][0], result.data[i][2]) | 
| 39 | |
| 40 proc LoadImage*[T: PixelType](path: string, package = DEFAULT_PACKAGE): Image[T] = | |
| 41 assert path.splitFile().ext.toLowerAscii == ".png" | |
| 42 when T is Gray: | |
| 43 let pngType = 0.cint | |
| 44 elif T is BGRA: | |
| 45 let pngType = 6.cint | |
| 46 | |
| 47 result = LoadImage[T](loadResource_intern(path, package = package).readAll()) | |
| 48 | |
| 38 | 49 | 
| 39 proc toPNG[T: PixelType](image: Image[T]): seq[uint8] = | 50 proc toPNG[T: PixelType](image: Image[T]): seq[uint8] = | 
| 40 when T is Gray: | 51 when T is Gray: | 
| 41 let pngType = 0 # hardcoded in lodepng.h | 52 let pngType = 0 # hardcoded in lodepng.h | 
| 42 else: | 53 else: | 
