feat(picture): add initial picture library

This commit is contained in:
decafcode 2023-11-25 12:57:46 -05:00 committed by decafcode
parent cb8b1eebd2
commit 59d0750be1
9 changed files with 135 additions and 0 deletions

6
.gitignore vendored
View File

@ -1,2 +1,8 @@
.cache/
.vscode/launch.json
# Important: must exclude all the files/folders under subprojects/ and not just
# subprojects/ itself, otherwise the exclusion rule for wrap files does not
# work.
subprojects/*
!subprojects/*.wrap

View File

@ -1,8 +1,11 @@
project('saltytools', 'c', version: '0.1.0')
libpng_dep = dependency('libpng', fallback: ['libpng', 'libpng_dep'])
inc = include_directories('.')
subdir('573file')
subdir('picture')
subdir('util')
subdir('ifsdump')

12
picture/meson.build Normal file
View File

@ -0,0 +1,12 @@
picture_lib = static_library(
'picture',
dependencies: [libpng_dep],
include_directories: [inc],
c_pch: '../precompiled.h',
sources: [
'picture-png.c',
'picture-png.h',
'picture.c',
'picture.h',
]
)

34
picture/picture-png.c Normal file
View File

@ -0,0 +1,34 @@
#include <assert.h>
#include <errno.h>
#include <string.h>
#include <png.h>
#include "picture/picture-png.h"
#include "picture/picture.h"
#include "util/log.h"
int picture_write_png(const struct picture *pic, const char *path) {
png_image png;
int r;
assert(pic != NULL);
assert(path != NULL);
memset(&png, 0, sizeof(png));
png.version = PNG_IMAGE_VERSION;
png.width = pic->dim.width;
png.height = pic->dim.height;
png.format = PNG_FORMAT_BGRA;
r = png_image_write_to_file(&png, path, 0, pic->pixels, 0, NULL);
if (r != 1 || (png.warning_or_error & 2)) {
log_write("libpng error writing to \"%s\": %s", path, png.message);
return -EIO;
}
return 0;
}

5
picture/picture-png.h Normal file
View File

@ -0,0 +1,5 @@
#pragma once
#include "picture/picture.h"
int picture_write_png(const struct picture *pic, const char *path);

34
picture/picture.c Normal file
View File

@ -0,0 +1,34 @@
#include <assert.h>
#include <errno.h>
#include <limits.h>
#include <stdlib.h>
#include <string.h>
#include "picture/picture.h"
int picture_alloc(struct picture **out, const struct dim *dim) {
struct picture *pic;
size_t nbytes;
assert(out != NULL);
assert(dim != NULL);
*out = NULL;
nbytes = sizeof(*pic) + dim->width * dim->height * sizeof(uint32_t);
if (nbytes > UINT_MAX) {
return -EOVERFLOW;
}
pic = malloc(nbytes);
if (pic == NULL) {
return -ENOMEM;
}
memcpy(&pic->dim, dim, sizeof(*dim));
*out = pic;
return 0;
}

27
picture/picture.h Normal file
View File

@ -0,0 +1,27 @@
#pragma once
#include <stdint.h>
typedef uint32_t pixel_t;
struct dim {
unsigned int width;
unsigned int height;
};
struct point {
unsigned int x;
unsigned int y;
};
struct rect {
struct point p1;
struct point p2;
};
struct picture {
struct dim dim;
pixel_t pixels[];
};
int picture_alloc(struct picture **out, const struct dim *dim);

View File

@ -1,5 +1,6 @@
#include <assert.h> // IWYU pragma: keep
#include <errno.h> // IWYU pragma: keep
#include <limits.h> // IWYU pragma: keep
#include <stdarg.h> // IWYU pragma: keep
#include <stdbool.h> // IWYU pragma: keep
#include <stddef.h> // IWYU pragma: keep

13
subprojects/libpng.wrap Normal file
View File

@ -0,0 +1,13 @@
[wrap-file]
directory = libpng-1.6.40
source_url = https://github.com/glennrp/libpng/archive/v1.6.40.tar.gz
source_filename = libpng-1.6.40.tar.gz
source_hash = 62d25af25e636454b005c93cae51ddcd5383c40fa14aa3dae8f6576feb5692c2
patch_filename = libpng_1.6.40-1_patch.zip
patch_url = https://wrapdb.mesonbuild.com/v2/libpng_1.6.40-1/get_patch
patch_hash = bad558070e0a82faa5c0ae553bcd12d49021fc4b628f232a8e58c3fbd281aae1
source_fallback_url = https://github.com/mesonbuild/wrapdb/releases/download/libpng_1.6.40-1/libpng-1.6.40.tar.gz
wrapdb_version = 1.6.40-1
[provide]
libpng = libpng_dep