jpg + stb for now
This commit is contained in:
parent
a8d667f879
commit
b683c751b8
8 changed files with 7830 additions and 5 deletions
|
@ -2,13 +2,13 @@
|
|||
|
||||
⚠️ WARNING: This library is still in heavy development. ⚠️
|
||||
|
||||
Pixie is a 2D graphics library similar to [Cairo](https://www.cairographics.org/) and [Skia](https://skia.org) written entirely in Nim.
|
||||
Pixie is a 2D graphics library similar to [Cairo](https://www.cairographics.org/) and [Skia](https://skia.org) written (almost) entirely in Nim.
|
||||
|
||||
Features include:
|
||||
* Drawing paths, shapes and curves
|
||||
* Complex masking
|
||||
* Shadows, glows and effects
|
||||
* Loading image file formats (PNG, BMP, + more in development)
|
||||
* Loading image file formats (PNG, BMP, JPG + more in development)
|
||||
|
||||
This library is being actively developed and is not yet ready for use. Since you've managed to stumble onto it, give it a star and check back soon!
|
||||
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
## Public interface to you library.
|
||||
|
||||
import pixie/images, pixie/masks, pixie/paths, pixie/common, pixie/blends,
|
||||
pixie/fileformats/bmp, pixie/fileformats/png, pixie/fileformats/jpg,
|
||||
pixie/fileformats/bmp, pixie/fileformats/png, pixie/fileformats/jpgstb,
|
||||
flatty/binny, os
|
||||
|
||||
export images, masks, paths, common, blends
|
||||
|
|
20
src/pixie/fileformats/jpgstb.nim
Normal file
20
src/pixie/fileformats/jpgstb.nim
Normal file
|
@ -0,0 +1,20 @@
|
|||
import pixie/images, pixie/common, pixie/fileformats/stb_image/stb_image
|
||||
|
||||
const
|
||||
jpgStartOfImage* = [0xFF.uint8, 0xD8]
|
||||
|
||||
proc decodeJpg*(data: seq[uint8]): Image =
|
||||
## Decodes the JPEG into an Image.
|
||||
var
|
||||
width: int
|
||||
height: int
|
||||
let pixels = loadFromMemory(data, width, height)
|
||||
|
||||
result = newImage(width, height)
|
||||
copyMem(result.data[0].addr, pixels[0].unsafeAddr, pixels.len)
|
||||
|
||||
proc decodeJpg*(data: string): Image {.inline.} =
|
||||
decodeJpg(cast[seq[uint8]](data))
|
||||
|
||||
proc encodeJpg*(image: Image): string =
|
||||
raise newException(PixieError, "Encoding JPG not supported yet")
|
2
src/pixie/fileformats/stb_image/stb_image.c
Normal file
2
src/pixie/fileformats/stb_image/stb_image.c
Normal file
|
@ -0,0 +1,2 @@
|
|||
#define STB_IMAGE_IMPLEMENTATION
|
||||
#include "stb_image.h"
|
7762
src/pixie/fileformats/stb_image/stb_image.h
Normal file
7762
src/pixie/fileformats/stb_image/stb_image.h
Normal file
File diff suppressed because it is too large
Load diff
41
src/pixie/fileformats/stb_image/stb_image.nim
Normal file
41
src/pixie/fileformats/stb_image/stb_image.nim
Normal file
|
@ -0,0 +1,41 @@
|
|||
import pixie/common
|
||||
|
||||
when defined(windows) and defined(vcc):
|
||||
{.pragma: stbcall, stdcall.}
|
||||
else:
|
||||
{.pragma: stbcall, cdecl.}
|
||||
|
||||
{.compile: "stb_image.c".}
|
||||
|
||||
proc stbi_image_free(retval_from_stbi_load: pointer)
|
||||
{.importc: "stbi_image_free", stbcall.}
|
||||
|
||||
proc stbi_load_from_memory(
|
||||
buffer: ptr cuchar,
|
||||
len: cint,
|
||||
x, y, channels_in_file: var cint,
|
||||
desired_channels: cint
|
||||
): ptr cuchar
|
||||
{.importc: "stbi_load_from_memory", stbcall.}
|
||||
|
||||
|
||||
proc loadFromMemory*(buffer: seq[uint8], width, height: var int): seq[uint8] =
|
||||
var outWidth, outHeight, outComponents: cint
|
||||
let data = stbi_load_from_memory(
|
||||
cast[ptr cuchar](buffer[0].unsafeAddr),
|
||||
buffer.len.cint,
|
||||
outWidth,
|
||||
outHeight,
|
||||
outComponents,
|
||||
4
|
||||
)
|
||||
if data == nil:
|
||||
raise newException(PixieError, "Loading JPG failed")
|
||||
|
||||
width = outWidth.int
|
||||
height = outHeight.int
|
||||
|
||||
result.setLen(width * height * 4)
|
||||
copyMem(result[0].addr, data, result.len)
|
||||
|
||||
stbi_image_free(data)
|
|
@ -3,7 +3,7 @@ import pixie, chroma, vmath, strutils, os
|
|||
proc writeAndCheck(image: Image, fileName: string) =
|
||||
image.writeFile(fileName)
|
||||
let masterFileName = fileName.replace("tests/images/", "tests/images/masters/")
|
||||
if not existsFile(masterFileName):
|
||||
if not fileExists(masterFileName):
|
||||
echo "Master file: " & masterFileName & " not found!"
|
||||
quit(-1)
|
||||
var master = readImage(fileName)
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
import pixie/fileformats/jpg
|
||||
import pixie/fileformats/jpgstb
|
||||
|
||||
let original = readFile("tests/images/jpg/jpeg420exif.jpg")
|
||||
|
||||
|
|
Loading…
Reference in a new issue