Blame img.ldr.png.inc.c

ea5919
ea5919
ea5919
int imageLoadPng(const char *path, int *outWidth, int *outHeight, unsigned char **pixels) {
ea5919
  png_structp png = png_create_read_struct(PNG_LIBPNG_VER_STRING, 0, 0, 0);
ea5919
  png_infop info = png ? png_create_info_struct(png) : NULL;
ea5919
  if (!png) {
ea5919
    fprintf(stderr, "Cannot initialize PNG library\n");
ea5919
    fflush(stderr);
ea5919
    return FALSE;
ea5919
  }
ea5919
ea5919
  FILE *f = fopen(path, "rb");
ea5919
  if (!f) {
ea5919
    fprintf(stderr, "Cannot open for read: %s\n", path);
ea5919
    fflush(stderr);
ea5919
    return FALSE;
ea5919
  }
ea5919
ea5919
  unsigned char header[8];
ea5919
  fread(header, sizeof(header), 1, f);
ea5919
  if (png_sig_cmp(header, 0, sizeof(header))) {
ea5919
    fclose(f);
ea5919
    fprintf(stderr, "Seems it is not PNG image: %s\n", path);
ea5919
    fflush(stderr);
ea5919
    return FALSE;
ea5919
  }
ea5919
ea5919
  png_init_io(png, f);
ea5919
  png_set_sig_bytes(png, sizeof(header));
ea5919
  png_read_info(png, info);
ea5919
ea5919
  unsigned int width, height;
ea5919
  int bitdepth, colortype;
ea5919
  png_get_IHDR(png,info, &width, &height, &bitdepth, &colortype, 0, 0, 0);
ea5919
  if (colortype == PNG_COLOR_TYPE_PALETTE)
ea5919
    png_set_palette_to_rgb(png);
ea5919
  if (colortype == PNG_COLOR_TYPE_GRAY && bitdepth < 8)
ea5919
    png_set_expand_gray_1_2_4_to_8(png);
ea5919
  if (colortype == PNG_COLOR_TYPE_GRAY || colortype == PNG_COLOR_TYPE_GRAY_ALPHA)
ea5919
    png_set_gray_to_rgb(png);
ea5919
  if (png_get_valid(png, info, PNG_INFO_tRNS))
ea5919
    png_set_tRNS_to_alpha(png);
ea5919
  if (colortype == PNG_COLOR_TYPE_RGB)
ea5919
    png_set_filler(png, 0xff, PNG_FILLER_AFTER);
ea5919
  if (bitdepth == 16)
ea5919
    png_set_strip_16(png);
ea5919
  if (bitdepth < 8)
ea5919
    png_set_packing(png);
ea5919
ea5919
  png_read_update_info(png, info);
ea5919
  png_get_IHDR(png, info, &width, &height, 0, 0, 0, 0, 0);
ea5919
ea5919
  unsigned char *data = (unsigned char*)calloc(1, 4*width*height);
ea5919
  png_bytep *rows = (png_bytep*)malloc(height*sizeof(png_bytep));
ea5919
  for(unsigned int i = 0; i < height; i++)
ade35a
    rows[i] = data + i*4*width;
ea5919
  png_read_image(png, rows);
ea5919
  png_read_end(png, 0);
ea5919
  free(rows);
ea5919
ea5919
  png_destroy_read_struct(&png, &info, 0);
ea5919
  fclose(f);
ea5919
ea5919
  *outWidth = (int)width;
ea5919
  *outHeight = (int)height;
ea5919
  *pixels = data;
ea5919
  return TRUE;
ea5919
}
ea5919
ea5919
ea5919
int imageSavePng(const char *path, int width, int height, const unsigned char *pixels) {
ea5919
  png_structp png = png_create_write_struct(PNG_LIBPNG_VER_STRING, 0, 0, 0);
ea5919
  png_infop info = png ? png_create_info_struct(png) : NULL;
ea5919
  if (!png) {
ea5919
    fprintf(stderr, "Cannot initialize PNG library\n");
ea5919
    fflush(stderr);
ea5919
    return FALSE;
ea5919
  }
ea5919
ea5919
  FILE *f = fopen(path, "wb");
ea5919
  if (!f) {
ea5919
    fprintf(stderr, "Cannot open for write: %s\n", path);
ea5919
    fflush(stderr);
ea5919
    return FALSE;
ea5919
  }
ea5919
ea5919
  png_init_io(png, f);
ea5919
  png_set_IHDR( png, info, width, height, 8, PNG_COLOR_TYPE_RGBA,
ea5919
                PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE );
ea5919
  png_write_info(png, info);
ea5919
  png_set_packing(png);
ea5919
ea5919
  png_bytep *rows = (png_bytep*)malloc(height*sizeof(png_bytep));
ea5919
  for(int i = 0; i < height; i++)
ade35a
    rows[i] = (png_bytep)(pixels + i*4*width);
ea5919
  png_write_image(png, rows);
ea5919
  png_write_end(png, info);
ea5919
  free(rows);
ea5919
ea5919
  png_destroy_write_struct(&png, &info);
ea5919
ea5919
  fclose(f);
ea5919
  return TRUE;
ea5919
}
ea5919
ea5919
ea5919