AnimEngine
Image.hpp
1 #pragma once
2 #ifndef LVPM_IMAGE_H_
3 #define LVPM_IMAGE_H_
4 
5 #include <string>
6 #include <stdint.h>
7 #include "../common.h"
8 
9 using namespace std;
10 
11 class Image{
12 public:
13  Image(void* const data, int w, int h, int channels, size_t component_size = sizeof(uint8_t));
14  Image(int w, int h, int channels, size_t component_size = sizeof(uint8_t));
15  virtual ~Image() {_free_image_data();}
16  virtual void _free_image_data(){
17  if(data != nullptr){
18  delete[](data);
19  }
20  data = nullptr;
21  }
22  int getWidth() const {return(width);}
23  int getHeight() const {return(height);}
24  int getChannels() const {return(channels);}
25  size_t getComponentSize() const {return(component_size);}
26  size_t getTotalDataSize() const {return(component_size*channels*width*height);}
27 
28  enum class ImageWriteFormat {PNG,HDR,JPEG};
29 
30  int writeFile(const string& path) const;
31  int writeFile(const char* path) const {return(writeFile(string(path)));}
32  int writeFileFormat(const string& path, ImageWriteFormat format) const;
33  int writeFileFormat(const char* path, ImageWriteFormat format) const {return(writeFileFormat(string(path), format));}
34 
35  uint8_t* data = nullptr;
36 protected:
37  Image() {}
38 
39  int channels;
40  int width, height;
41  size_t component_size;
42 };
43 
44 class ExternalImage : public Image{
45 public:
46  ExternalImage(const string& path, bool delay_load = false, int expected_channels=-1);
47  virtual ~ExternalImage(){
48  _free_image_data();
49  }
50  virtual void _free_image_data();
51 
52  bool load(int expected_channels=-1) {
53  if(loaded){fprintf(stderr, "Warning! Tried to load already loaded image.\n"); return(false);}
54  else{return(pload(expected_channels));}
55  };
56 
57  const string& getPath() const {return(path);}
58  const bool isLoaded() const {return(loaded);}
59 
60  const char* const getErrorMsg() const {if(stb_fail_reason){return(stb_fail_reason);} else {return(nullptr);}}
61  virtual void printErrorMsg() const {if(stb_fail_reason){fprintf(stderr, "Image load failed: '%s'\n", stb_fail_reason);}}
62 protected:
63  virtual bool pload(int expected_channels);
64  bool loaded = false;
65  string path;
66  const char* stb_fail_reason = nullptr;
67 };
68 
70 public:
71  ExternalImageHDR(string path, bool delay_load = false, int expected_channels=-1);
72  ExternalImageHDR(const char* const path, bool delay_load = false, int expected_channels=-1);
73  void printErrorMsg() const override {if(stb_fail_reason){fprintf(stderr, "HDR Image load failed: '%s'\n", stb_fail_reason);}}
74 protected:
75  bool pload(int expected_channels) override;
76 };
77 
78 #endif
Definition: Image.hpp:69
Definition: Image.hpp:11
Definition: Image.hpp:44