aboutsummaryrefslogtreecommitdiff
path: root/src/png.c
blob: c907ce323720bfa76229fc10271f52b2c8ac48cc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include "picow.h"

#define STBI_ONLY_PNG
#define STBI_MAX_DIMENSIONS (160 * 205)
#define STBI_NO_FAILURE_STRINGS
#define STBI_NO_LINEAR
#define STBI_NO_STDIO
#define STBI_NO_THREAD_LOCALS
#define STB_IMAGE_IMPLEMENTATION
#include <stb/stb_image.h>

_Static_assert(sizeof(union pw_pixel) == 4, "unexpected pixel size");
_Static_assert(sizeof(struct pw_png) == 4 * 160 * 205,
               "unexpected png image size");

bool pw_png_decode(struct pw_png *png, uint8_t const *data, size_t datasize) {
  int x, y, n;
  int ok = stbi_info_from_memory(data, datasize, &x, &y, &n);
  if (!ok || x != 160 || y != 205) {
    return false;
  }

  unsigned char *image = stbi_load_from_memory(data, datasize, &x, &y, &n, 4);
  if (!image) {
    return false;
  }

  // Using `memcpy` is OK here: we know that the images are same size.
  memcpy(png->image, image, sizeof png->image); // NOLINT
  stbi_image_free(image);

  return true;
}