aboutsummaryrefslogtreecommitdiff
path: root/png.c
blob: 02b951a59715beba913e56436fe48c7044928e18 (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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#include <SDL2/SDL.h>
#include <string.h>
#include <png.h>

SDL_Texture* imv_load_png(SDL_Renderer *r, const char* path)
{
  FILE *fp = fopen(path, "rb");

  if(!fp) {
    return NULL;
  }

  png_structp png = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
  if(!png) {
    fclose(fp);
    return NULL;
  }

  png_infop info = png_create_info_struct(png);
  if(!info) {
    fclose(fp);
    png_destroy_read_struct(&png, NULL, NULL);
    return NULL;
  }

  png_init_io(png, fp);
  png_read_png(png, info,
      PNG_TRANSFORM_STRIP_16 | PNG_TRANSFORM_PACKING | PNG_TRANSFORM_EXPAND,
      NULL);
  png_uint_32 width, height;
  png_get_IHDR(png, info, &width, &height, NULL, NULL, NULL, NULL, NULL);
  unsigned int bytes_per_row = png_get_rowbytes(png, info);
  png_bytepp row_ptrs = png_get_rows(png, info);

  char* pixels = (char*)malloc(bytes_per_row*height);
  for (unsigned int i = 0; i < height; i++) {
    memcpy(pixels + bytes_per_row * i, row_ptrs[i], bytes_per_row);
  }
  png_destroy_read_struct(&png, &info, NULL);
  fclose(fp);

  SDL_Texture *img = SDL_CreateTexture(r,
      SDL_PIXELFORMAT_RGB24, SDL_TEXTUREACCESS_STATIC,
      width, height);

  SDL_Rect area = {0,0,width,height};
  SDL_UpdateTexture(img, &area, pixels, bytes_per_row);
  free(pixels);
  return img;
}