ImageReader.cpp 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. #include "libs/lodepng/lodepng.h"
  2. #include "io/ImageReader.h"
  3. #include "utils/Cleaner.h"
  4. static void cleanRawData(ColorChannel*& c) {
  5. free(c);
  6. }
  7. static void cleanState(LodePNGState& state) {
  8. lodepng_state_cleanup(&state);
  9. }
  10. ImageReader::Image::Image() : data(nullptr) {
  11. }
  12. Error ImageReader::load(Image& image, const char* path) {
  13. Cleaner<ColorChannel*, cleanRawData> rawData(nullptr);
  14. size_t rawSize;
  15. unsigned int error = lodepng_load_file(&rawData, &rawSize, path);
  16. if(error) {
  17. Error e{"cannot load file '"};
  18. e.message.append(path).append("': ").append(lodepng_error_text(error));
  19. return e;
  20. }
  21. Cleaner<LodePNGState, cleanState> state;
  22. lodepng_state_init(&state);
  23. state->decoder.color_convert = false;
  24. error = lodepng_decode(&image.data, &image.width, &image.height, &state,
  25. rawData, rawSize);
  26. if(error) {
  27. Error e{"cannot decode file '"};
  28. e.message.append(path).append("'").append(lodepng_error_text(error));
  29. return e;
  30. }
  31. image.channels = lodepng_get_channels(&state->info_png.color);
  32. image.bitdepth = state->info_png.color.bitdepth;
  33. return {};
  34. }