Add supprot for 24bit bitmaps.
This commit is contained in:
parent
f8ea6c3717
commit
305d65187c
6 changed files with 23 additions and 7 deletions
|
@ -1,4 +1,4 @@
|
|||
import ../images, flatty/binny, flatty/hexPrint, chroma, sequtils
|
||||
import ../images, flatty/binny, flatty/hexPrint, chroma, sequtils, print
|
||||
|
||||
# See: https://en.wikipedia.org/wiki/BMP_file_format
|
||||
|
||||
|
@ -10,21 +10,32 @@ proc decodeBmp*(data: string): Image =
|
|||
let
|
||||
width = data.readInt32(0x12).int
|
||||
height = data.readInt32(0x16).int
|
||||
# TODO: Handle masks.
|
||||
bits = data.readUint16(0x1C)
|
||||
compression = data.readUint32(0x1E)
|
||||
var
|
||||
offset = data.readUInt32(0xA).int
|
||||
|
||||
doAssert bits in {32, 24}
|
||||
doAssert compression in {0, 3}
|
||||
|
||||
result = newImage(width, height)
|
||||
|
||||
for y in 0 ..< result.height:
|
||||
for x in 0 ..< result.width:
|
||||
var rgba: ColorRGBA
|
||||
rgba.r = data.readUint8(offset+0)
|
||||
rgba.g = data.readUint8(offset+1)
|
||||
rgba.b = data.readUint8(offset+2)
|
||||
rgba.a = data.readUint8(offset+3)
|
||||
if bits == 32:
|
||||
rgba.r = data.readUint8(offset + 0)
|
||||
rgba.g = data.readUint8(offset + 1)
|
||||
rgba.b = data.readUint8(offset + 2)
|
||||
rgba.a = data.readUint8(offset + 3)
|
||||
offset += 4
|
||||
elif bits == 24:
|
||||
rgba.r = data.readUint8(offset + 2)
|
||||
rgba.g = data.readUint8(offset + 1)
|
||||
rgba.b = data.readUint8(offset + 0)
|
||||
rgba.a = 255
|
||||
offset += 3
|
||||
result[x, result.height - y - 1] = rgba
|
||||
offset += 4
|
||||
|
||||
proc encodeBmp*(image: Image): string =
|
||||
## Encodes an image into bitmap data.
|
||||
|
|
BIN
tests/images/bmp/knight.24.bmp
Normal file
BIN
tests/images/bmp/knight.24.bmp
Normal file
Binary file not shown.
After Width: | Height: | Size: 890 B |
BIN
tests/images/bmp/knight.24.master.bmp
Normal file
BIN
tests/images/bmp/knight.24.master.bmp
Normal file
Binary file not shown.
After Width: | Height: | Size: 630 B |
BIN
tests/images/bmp/knight.32.bmp
Normal file
BIN
tests/images/bmp/knight.32.bmp
Normal file
Binary file not shown.
After Width: | Height: | Size: 890 B |
BIN
tests/images/bmp/knight.32.master.bmp
Normal file
BIN
tests/images/bmp/knight.32.master.bmp
Normal file
Binary file not shown.
After Width: | Height: | Size: 890 B |
|
@ -29,3 +29,8 @@ block:
|
|||
doAssert image2.width == image.width
|
||||
doAssert image2.height == image.height
|
||||
doAssert image2.data == image.data
|
||||
|
||||
block:
|
||||
for bits in [32, 24]:
|
||||
var image = decodeBmp(readFile("images/bmp/knight." & $bits & ".master.bmp"))
|
||||
writeFile("images/bmp/knight." & $bits & ".bmp", encodeBmp(image))
|
||||
|
|
Loading…
Reference in a new issue