Mercurial > games > semicongine
diff 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 |
line wrap: on
line diff
--- a/semiconginev2/image.nim Wed Jul 24 20:12:19 2024 +0700 +++ b/semiconginev2/image.nim Wed Jul 24 23:26:34 2024 +0700 @@ -1,11 +1,14 @@ type Gray* = TVec1[uint8] - RGBA* = TVec4[uint8] - PixelType* = Gray | RGBA + BGRA* = TVec4[uint8] + PixelType* = Gray | BGRA Image*[T: PixelType] = object width*: uint32 height*: uint32 - interpolation*: VkFilter = VK_FILTER_LINEAR + minInterpolation*: VkFilter = VK_FILTER_LINEAR + magInterpolation*: VkFilter = VK_FILTER_LINEAR + wrapU: VkSamplerAddressMode = VK_SAMPLER_ADDRESS_MODE_REPEAT + wrapV: VkSamplerAddressMode = VK_SAMPLER_ADDRESS_MODE_REPEAT data*: seq[T] vk*: VkImage imageview*: VkImageView @@ -13,18 +16,16 @@ isRenderTarget*: bool = false samples*: VkSampleCountFlagBits = VK_SAMPLE_COUNT_1_BIT -proc LoadImage*[T: PixelType](path: string, package = DEFAULT_PACKAGE): Image[T] = - assert path.splitFile().ext.toLowerAscii == ".png" +proc LoadImage*[T: PixelType](pngData: seq[uint8]): Image[T] = when T is Gray: let pngType = 0.cint - elif T is RGBA: + elif T is BGRA: let pngType = 6.cint - let indata = loadResource_intern(path, package = package).readAll() var w, h: cuint var data: cstring - 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: + 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: raise newException(Exception, "An error occured while loading PNG file") let imagesize = w * h * 4 @@ -32,10 +33,20 @@ copyMem(result.data.ToCPointer, data, imagesize) nativeFree(data) - when T is RGBA: # converkt to BGRA + when T is BGRA: # converkt to BGRA for i in 0 ..< result.data.len: swap(result.data[i][0], result.data[i][2]) +proc LoadImage*[T: PixelType](path: string, package = DEFAULT_PACKAGE): Image[T] = + assert path.splitFile().ext.toLowerAscii == ".png" + when T is Gray: + let pngType = 0.cint + elif T is BGRA: + let pngType = 6.cint + + result = LoadImage[T](loadResource_intern(path, package = package).readAll()) + + proc toPNG[T: PixelType](image: Image[T]): seq[uint8] = when T is Gray: let pngType = 0 # hardcoded in lodepng.h