summaryrefslogtreecommitdiff
path: root/image.c
diff options
context:
space:
mode:
Diffstat (limited to 'image.c')
-rw-r--r--image.c59
1 files changed, 34 insertions, 25 deletions
diff --git a/image.c b/image.c
index ac5387a..ec29751 100644
--- a/image.c
+++ b/image.c
@@ -20,7 +20,6 @@
* Web: http://ni.fr.eu.org/
* Email: <nico at ni.fr.eu.org>
*/
-#include <assert.h>
#include <stdlib.h>
#include <string.h>
#include <png.h>
@@ -31,6 +30,12 @@
typedef uint8_t u8x16 __attribute__((vector_size(16)));
typedef uint16_t u16x8 __attribute__((vector_size(16)));
+GQuark
+image_error_quark(void)
+{
+ return g_quark_from_static_string("image-error-quark");
+}
+
static void
image_free(struct image *image, void *user_data)
{
@@ -44,9 +49,8 @@ image_new(int width, int height, int stride, enum image_format format)
size_t image_size_aligned = ((height * stride) + 15) / 16 * 16;
uint8_t *m = aligned_alloc(16,
struct_size_aligned + image_size_aligned);
- if (!m) {
- utils_fatal("memory exhausted");
- }
+ if (!m)
+ g_error("aligned_alloc: failed to allocate");
struct image *image = (struct image *) m;
image->width = width;
image->height = height;
@@ -76,9 +80,9 @@ image_unref(struct image *image)
static void
image_histogram_sgrbg8(const struct image *image, uint32_t histogram[3][256])
{
- assert(image->width % 2 == 0);
- assert(image->height % 2 == 0);
- assert(image->stride == image->width);
+ g_assert(image->width % 2 == 0);
+ g_assert(image->height % 2 == 0);
+ g_assert(image->stride == image->width);
memset(histogram, 0, 3 * 256 * sizeof(uint32_t));
const uint8_t *p = image->pixels;
for (int i = 0; i < image->height; i += 2) {
@@ -100,7 +104,7 @@ image_histogram_sgrbg8(const struct image *image, uint32_t histogram[3][256])
static void
image_histogram_xrgb32(const struct image *image, uint32_t histogram[3][256])
{
- assert(image->stride == image->width * 4);
+ g_assert(image->stride == image->width * 4);
memset(histogram, 0, 3 * 256 * sizeof(uint32_t));
const uint8_t *p = image->pixels;
const uint8_t *pend = image->pixels + image->height * image->stride;
@@ -126,7 +130,7 @@ image_histogram(const struct image *image, uint32_t histogram[3][256])
image_histogram_xrgb32(image, histogram);
break;
default:
- assert(0);
+ g_assert_not_reached();
}
}
@@ -178,9 +182,9 @@ static void
image_white_balance_sgrbg8(struct image *image,
const struct image_white_balance_reference *reference)
{
- assert(image->width % 16 == 0);
- assert(image->height % 2 == 0);
- assert(image->stride == image->width);
+ g_assert(image->width % 16 == 0);
+ g_assert(image->height % 2 == 0);
+ g_assert(image->stride == image->width);
uint16_t fr, fg, fb;
image_white_balance_factors(reference, &fr, &fg, &fb);
u8x16 *p = (u8x16 *) image->pixels;
@@ -227,8 +231,8 @@ static void
image_white_balance_xbgr32(struct image *image,
const struct image_white_balance_reference *reference)
{
- assert(image->width % 4 == 0);
- assert(image->stride == image->width * 4);
+ g_assert(image->width % 4 == 0);
+ g_assert(image->stride == image->width * 4);
uint16_t fr, fg, fb;
image_white_balance_factors(reference, &fr, &fg, &fb);
u8x16 *p = (u8x16 *) image->pixels;
@@ -268,7 +272,7 @@ image_white_balance(struct image *image,
image_white_balance_xbgr32(image, reference);
break;
default:
- assert(0);
+ g_assert_not_reached();
}
}
@@ -284,7 +288,7 @@ image_convert_to_xbgr32_from_sgrbg8(struct image *dst,
* G R G R G R
* B G B G B G
*/
- assert(dst->width == src->width
+ g_assert(dst->width == src->width
&& dst->height == src->height);
const int in_stride = src->stride;
const int out_stride = dst->stride;
@@ -361,10 +365,10 @@ image_convert_to_xbgr32_from_sgrbg8(struct image *dst,
void
image_convert(struct image *dst, const struct image *src)
{
- assert(dst->width == src->width
+ g_assert(dst->width == src->width
&& dst->height == src->height);
if (dst->format == src->format) {
- assert(dst->stride == src->stride);
+ g_assert(dst->stride == src->stride);
memcpy(dst->pixels, src->pixels, src->stride * src->height);
} else {
switch (src->format) {
@@ -374,18 +378,19 @@ image_convert(struct image *dst, const struct image *src)
image_convert_to_xbgr32_from_sgrbg8(dst, src);
break;
default:
- assert(0);
+ g_assert_not_reached();
}
break;
default:
- assert(0);
+ g_assert_not_reached();
}
}
}
-void
-image_save(const struct image *image, const char *name)
+bool
+image_save(const struct image *image, const char *name, GError **error)
{
+ g_return_val_if_fail(error == NULL || *error == NULL, false);
png_image pimage;
memset(&pimage, 0, sizeof(pimage));
pimage.version = PNG_IMAGE_VERSION;
@@ -399,10 +404,14 @@ image_save(const struct image *image, const char *name)
pimage.format = PNG_FORMAT_BGRA;
break;
default:
- assert(0);
+ g_assert_not_reached();
}
int r = png_image_write_to_file(&pimage, name, 0,
image->pixels, image->stride, NULL);
- if (r == 0)
- utils_fatal("can not write image: %s", pimage.message);
+ if (r == 0) {
+ g_set_error(error, IMAGE_ERROR, IMAGE_ERROR_SAVE,
+ "can not write image: %s", pimage.message);
+ return false;
+ }
+ return true;
}