commit
89183e86e2
4 changed files with 24 additions and 14 deletions
|
@ -1 +1,2 @@
|
||||||
## Add private variables or functions here that you don't want to export.
|
type
|
||||||
|
PixieError* = object of ValueError ## Raised if an operation fails.
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
import ../images, flatty/binny, chroma
|
import ../images, flatty/binny, chroma, pixie/common
|
||||||
|
|
||||||
# See: https://en.wikipedia.org/wiki/BMP_file_format
|
# See: https://en.wikipedia.org/wiki/BMP_file_format
|
||||||
|
|
||||||
|
@ -6,17 +6,26 @@ proc decodeBmp*(data: string): Image =
|
||||||
## Decodes bitmap data into an Image.
|
## Decodes bitmap data into an Image.
|
||||||
|
|
||||||
# BMP Header
|
# BMP Header
|
||||||
doAssert data[0..1] == "BM"
|
if data[0..1] != "BM":
|
||||||
let
|
raise newException(PixieError, "Invalid BMP data")
|
||||||
width = data.readInt32(0x12).int
|
|
||||||
height = data.readInt32(0x16).int
|
|
||||||
bits = data.readUint16(0x1C)
|
|
||||||
compression = data.readUint32(0x1E)
|
|
||||||
var
|
|
||||||
offset = data.readUInt32(0xA).int
|
|
||||||
|
|
||||||
doAssert bits in {32, 24}
|
let
|
||||||
doAssert compression in {0, 3}
|
width = data.readInt32(18).int
|
||||||
|
height = data.readInt32(22).int
|
||||||
|
bits = data.readUint16(28).int
|
||||||
|
compression = data.readUint32(30).int
|
||||||
|
var
|
||||||
|
offset = data.readUInt32(10).int
|
||||||
|
|
||||||
|
if bits notin [32, 24]:
|
||||||
|
raise newException(PixieError, "Unsupported BMP data format")
|
||||||
|
|
||||||
|
if compression notin [0, 3]:
|
||||||
|
raise newException(PixieError, "Unsupported BMP data format")
|
||||||
|
|
||||||
|
let channels = if bits == 32: 4 else: 3
|
||||||
|
if width * height * channels + offset > data.len:
|
||||||
|
raise newException(PixieError, "Invalid BMP data size")
|
||||||
|
|
||||||
result = newImage(width, height)
|
result = newImage(width, height)
|
||||||
|
|
||||||
|
@ -38,7 +47,7 @@ proc decodeBmp*(data: string): Image =
|
||||||
result[x, result.height - y - 1] = rgba
|
result[x, result.height - y - 1] = rgba
|
||||||
|
|
||||||
proc encodeBmp*(image: Image): string =
|
proc encodeBmp*(image: Image): string =
|
||||||
## Encodes an image into bitmap data.
|
## Encodes an image into the BMP file format.
|
||||||
|
|
||||||
# BMP Header
|
# BMP Header
|
||||||
result.add("BM") # The header field used to identify the BMP
|
result.add("BM") # The header field used to identify the BMP
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
import pixie, pixie/fileformats/bmp, chroma, flatty/hexPrint
|
import pixie, pixie/fileformats/bmp, chroma
|
||||||
|
|
||||||
block:
|
block:
|
||||||
var image = newImage(4, 2)
|
var image = newImage(4, 2)
|
Loading…
Reference in a new issue