jpg + stb for now

This commit is contained in:
Ryan Oldenburg 2020-11-27 15:51:40 -06:00
parent a8d667f879
commit b683c751b8
8 changed files with 7830 additions and 5 deletions

View file

@ -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!

View file

@ -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

View 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")

View file

@ -0,0 +1,2 @@
#define STB_IMAGE_IMPLEMENTATION
#include "stb_image.h"

File diff suppressed because it is too large Load diff

View 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)

View file

@ -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)

View file

@ -1,4 +1,4 @@
import pixie/fileformats/jpg
import pixie/fileformats/jpgstb
let original = readFile("tests/images/jpg/jpeg420exif.jpg")