Blame img.ldr.inc.c

ea5919
ea5919
#include "img.ldr.tga.inc.c"
ea5919
ea5919
#ifdef PNG_LIBPNG_VER_STRING
ea5919
#include "img.ldr.png.inc.c"
ea5919
#endif
ea5919
ea5919
ea5919
typedef int (*ImageLoadFunc)(const char *path, int *outWidth, int *outHeight, unsigned char **pixels);
ea5919
typedef int (*ImageSaveFunc)(const char *path, int width, int height, const unsigned char *pixels);
ea5919
ea5919
ea5919
int checkExt(const char *path, const char *ext) {
ea5919
  int lp = strlen(path);
ea5919
  int le = strlen(ext);
ea5919
  if (lp <= le) return FALSE;
ea5919
  if (path[lp-le-1] != '.') return FALSE;
ea5919
  for(const char *a = path+lp-le, *b = ext; *b; ++a, ++b)
ea5919
    if (tolower(*a) != tolower(*b)) return FALSE;
ea5919
  return TRUE;
ea5919
}
ea5919
ea5919
ea5919
int imageLoadAuto(const char *path, int *outWidth, int *outHeight, unsigned char **pixels) {
ea5919
  struct {
ea5919
    const char *ext;
ea5919
    ImageLoadFunc func;
ea5919
  } formats[] = {
ea5919
    { "tga", &imageLoadTga },
ea5919
    #if defined(HELI_HELIANTUS_H)
ea5919
    { "png", &imageLoad },
ea5919
    #elif defined(PNG_LIBPNG_VER_STRING)
ea5919
    { "png", &imageLoadPng },
ea5919
    #endif
ea5919
  };
ea5919
ea5919
  int cnt = sizeof(formats)/sizeof(*formats);
ea5919
  for(int i = 0; i < cnt; ++i)
ea5919
    if (checkExt(path, formats[i].ext))
ea5919
      return formats[i].func(path, outWidth, outHeight, pixels);
ea5919
ea5919
  fprintf(stderr, "Cannot load this image format (supported formats:");
ea5919
  for(int i = 0; i < cnt; ++i)
ea5919
    fprintf(stderr, " %s", formats[i].ext);
ea5919
  fprintf(stderr, "): %s\n", path);
ea5919
  fflush(stderr);
ea5919
  return FALSE;
ea5919
}
ea5919
ea5919
ea5919
int imageSaveAuto(const char *path, int width, int height, const unsigned char *pixels) {
ea5919
  struct {
ea5919
    const char *ext;
ea5919
    ImageSaveFunc func;
ea5919
  } formats[] = {
ea5919
    { "tga", &imageSaveTga },
ea5919
    #if defined(HELI_HELIANTUS_H)
ea5919
    { "png", (ImageSaveFunc)&imageSave },
ea5919
    #elif defined(PNG_LIBPNG_VER_STRING)
ea5919
    { "png", &imageSavePng },
ea5919
    #endif
ea5919
  };
ea5919
ea5919
  int cnt = sizeof(formats)/sizeof(*formats);
ea5919
  for(int i = 0; i < cnt; ++i)
ea5919
    if (checkExt(path, formats[i].ext))
ea5919
      return formats[i].func(path, width, height, pixels);
ea5919
ea5919
  fprintf(stderr, "Cannot save this image format (supported formats:");
ea5919
  for(int i = 0; i < cnt; ++i)
ea5919
    fprintf(stderr, " %s", formats[i].ext);
ea5919
  fprintf(stderr, "): %s\n", path);
ea5919
  fflush(stderr);
ea5919
  return FALSE;
ea5919
}
ea5919
ea5919
ea5919
int imgLoad(Img *img, const char *path) {
ea5919
  imgDestroy(img);
ea5919
  int w, h;
ea5919
  unsigned char *data;
ea5919
  if (!imageLoadAuto(path, &w, &h, &data))
ea5919
    { free(data); return FALSE; }
ea5919
  imgFromInt(img, w, h, data);
ea5919
  free(data);
ea5919
  return TRUE;
ea5919
}
ea5919
ea5919
ea5919
int imgSave(Img *img, const char *path) {
ea5919
  unsigned char *data = imgToInt(img);
ea5919
  if (!imageSaveAuto(path, img->w, img->h, data))
ea5919
    { free(data); return FALSE; }
ea5919
  free(data);
ea5919
  return TRUE;
ea5919
}
ea5919
ea5919