Sun, 20 May 2018 08:13:22 +0300
* Importing fixed grid font atlases
1004
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

1  #include "Bitmap.h" 
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

2  
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

3  #include <fstream> 
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

4  #include <algorithm> 
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

5  #include <numeric> 
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

6  
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

7  #include "../Encoding/PNG.h" 
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

8  #include "../Encoding/JPEG.h" 
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

9  #include "../IO/Stream.h" 
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

10  #include "../Utils/Assert.h" 
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

11  
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

12  namespace Gorgon { namespace Graphics { 
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

13  
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

14  void Bitmap::Prepare() { 
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

15  if(data) { 
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

16  Graphics::Texture::Set(*data); 
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

17  } 
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

18  } 
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

19  
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

20  void Bitmap::Discard() { 
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

21  if(data) { 
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

22  delete data; 
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

23  data=nullptr; 
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

24  } 
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

25  } 
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

26  
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

27  bool Bitmap::Import(const std::string &filename) { 
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

28  auto dotpos = filename.find_last_of('.'); 
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

29  if(dotpos!=1) { 
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

30  auto ext = filename.substr(dotpos+1); 
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

31  
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

32  if(String::ToLower(ext) == "png") { 
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

33  return ImportPNG(filename); 
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

34  } 
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

35  else if(String::ToLower(ext) == "jpg"  String::ToLower(ext) =="jpeg") { 
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

36  return ImportJPEG(filename); 
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

37  } 
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

38  else if(String::ToLower(ext) == "bmp") { 
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

39  return ImportBMP(filename); 
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

40  } 
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

41  } 
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

42  
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

43  std::ifstream file(filename, std::ios::binary); 
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

44  
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

45  static const uint32_t pngsig = 0x474E5089; 
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

46  static const uint32_t jpgsig1 = 0xE0FFD8FF; 
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

47  static const uint32_t jpgsig2 = 0xE1FFD8FF; 
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

48  static const uint32_t bmpsig = 0x4D42; 
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

49  
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

50  uint32_t sig = IO::ReadUInt32(file); 
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

51  
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

52  file.close(); 
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

53  
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

54  if(sig == pngsig) { 
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

55  return ImportPNG(filename); 
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

56  } 
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

57  else if(sig == jpgsig1  sig == jpgsig2) { 
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

58  return ImportJPEG(filename); 
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

59  } 
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

60  else if((sig&0xffff) == bmpsig) { 
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

61  return ImportBMP(filename); 
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

62  } 
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

63  
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

64  throw std::runtime_error("Unsuppoted file format"); 
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

65  } 
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

66  
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

67  bool Bitmap::Import(std::istream &file) { 
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

68  static const uint32_t pngsig = 0x474E5089; 
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

69  static const uint32_t jpgsig1 = 0xE0FFD8FF; 
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

70  static const uint32_t jpgsig2 = 0xE1FFD8FF; 
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

71  static const uint32_t bmpsig = 0x4D42; 
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

72  
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

73  uint32_t sig = IO::ReadUInt32(file); 
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

74  
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

75  file.seekg(4, std::ios::cur); 
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

76  
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

77  if(sig == pngsig) { 
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

78  return ImportPNG(file); 
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

79  } 
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

80  else if(sig == jpgsig1  sig == jpgsig2) { 
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

81  return ImportJPEG(file); 
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

82  } 
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

83  else if((sig&0xffff) == bmpsig) { 
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

84  return ImportBMP(file); 
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

85  } 
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

86  
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

87  throw std::runtime_error("Unsuppoted file format"); 
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

88  } 
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

89  
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

90  bool Bitmap::ImportPNG(const std::string &filename) { 
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

91  std::ifstream file(filename, std::ios::binary); 
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

92  
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

93  if(!file.is_open()  !file.good()) return false; 
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

94  
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

95  Destroy(); 
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

96  
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

97  data=new Containers::Image(); 
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

98  Encoding::Png.Decode(file, *data); 
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

99  
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

100  return true; 
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

101  } 
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

102  
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

103  bool Bitmap::ImportJPEG(const std::string &filename) { 
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

104  std::ifstream file(filename, std::ios::binary); 
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

105  
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

106  if(!file.is_open()  !file.good()) return false; 
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

107  
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

108  Destroy(); 
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

109  
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

110  data=new Containers::Image(); 
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

111  Encoding::Jpg.Decode(file, *data); 
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

112  
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

113  return true; 
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

114  } 
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

115  
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

116  bool Bitmap::ImportBMP(const std::string &filename) { 
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

117  std::ifstream file(filename, std::ios::binary); 
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

118  
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

119  if(!file.is_open()  !file.good()) return false; 
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

120  
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

121  Destroy(); 
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

122  
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

123  data=new Containers::Image(); 
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

124  return data>ImportBMP(filename); 
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

125  } 
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

126  
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

127  bool Bitmap::ImportPNG(std::istream &file) { 
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

128  Destroy(); 
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

129  
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

130  data=new Containers::Image(); 
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

131  Encoding::Png.Decode(file, *data); 
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

132  
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

133  return true; 
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

134  } 
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

135  
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

136  bool Bitmap::ImportJPEG(std::istream &file) { 
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

137  Destroy(); 
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

138  
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

139  data=new Containers::Image(); 
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

140  Encoding::Jpg.Decode(file, *data); 
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

141  
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

142  return true; 
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

143  } 
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

144  
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

145  bool Bitmap::ImportBMP(std::istream &file) { 
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

146  Destroy(); 
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

147  
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

148  data=new Containers::Image(); 
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

149  return data>ImportBMP(file); 
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

150  } 
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

151  
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

152  bool Bitmap::ExportPNG(const std::string &filename) { 
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

153  ASSERT(data, "Image data does not exists"); 
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

154  
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

155  if(GetMode()!=Graphics::ColorMode::RGB && 
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

156  GetMode()!=Graphics::ColorMode::RGBA && 
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

157  GetMode()!=Graphics::ColorMode::Grayscale && 
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

158  GetMode()!=Graphics::ColorMode::Grayscale_Alpha && 
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

159  GetMode()!=Graphics::ColorMode::Alpha) 
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

160  throw std::runtime_error("Unsupported color mode"); 
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

161  
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

162  std::ofstream file(filename, std::ios::binary); 
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

163  if(!file.is_open()) 
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

164  return false; 
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

165  
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

166  Encoding::Png.Encode(*data, file); 
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

167  
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

168  return true; 
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

169  } 
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

170  
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

171  bool Bitmap::ExportPNG(std::ostream &out) { 
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

172  ASSERT(data, "Image data does not exists"); 
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

173  
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

174  if(GetMode()!=Graphics::ColorMode::RGB && 
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

175  GetMode()!=Graphics::ColorMode::RGBA && 
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

176  GetMode()!=Graphics::ColorMode::Grayscale && 
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

177  GetMode()!=Graphics::ColorMode::Grayscale_Alpha && 
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

178  GetMode()!=Graphics::ColorMode::Alpha) 
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

179  throw std::runtime_error("Unsupported color mode"); 
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

180  
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

181  Encoding::Png.Encode(*data, out); 
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

182  
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

183  return true; 
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

184  } 
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

185  
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

186  bool Bitmap::ExportBMP(const std::string &filename) { 
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

187  ASSERT(data, "Image data does not exists"); 
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

188  
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

189  return data>ExportBMP(filename); 
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

190  } 
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

191  
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

192  bool Bitmap::ExportBMP(std::ostream &out) { 
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

193  ASSERT(data, "Image data does not exists"); 
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

194  
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

195  return data>ExportBMP(out); 
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

196  } 
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

197  
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

198  bool Bitmap::ExportJPG(const std::string &filename, int quality) { 
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

199  ASSERT(data, "Image data does not exists"); 
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

200  
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

201  if(GetMode()!=Graphics::ColorMode::RGB && 
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

202  GetMode()!=Graphics::ColorMode::Grayscale) 
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

203  throw std::runtime_error("Unsupported color mode"); 
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

204  
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

205  if(quality < 0  quality > 100) 
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

206  throw std::runtime_error("Unsupported quality"); 
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

207  
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

208  std::ofstream file(filename, std::ios::binary); 
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

209  if(!file.is_open()) 
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

210  return false; 
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

211  
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

212  Encoding::Jpg.Encode(*data, file, quality); 
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

213  
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

214  return true; 
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

215  } 
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

216  
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

217  bool Bitmap::ExportJPG(std::ostream &out, int quality) { 
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

218  ASSERT(data, "Image data does not exists"); 
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

219  
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

220  if(GetMode()!=Graphics::ColorMode::RGB && 
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

221  GetMode()!=Graphics::ColorMode::Grayscale) 
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

222  throw std::runtime_error("Unsupported color mode"); 
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

223  
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

224  if(quality < 0  quality > 100) 
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

225  throw std::runtime_error("Unsupported quality"); 
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

226  
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

227  Encoding::Jpg.Encode(*data, out, quality); 
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

228  
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

229  return true; 
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

230  } 
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

231  
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

232  void Bitmap::StripRGB() { 
1142  233  ASSERT(HasAlpha(), "Unsupported color mode"); 
1004
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

234  
1142  235  int alpha = GetAlphaIndex(); 
1004
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

236  
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

237  auto &data = *this>data; 
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

238  
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

239  Containers::Image img(data.GetSize(), ColorMode::Alpha); 
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

240  
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

241  for(int y=0; y<data.GetHeight(); y++) { 
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

242  for(int x=0; x<data.GetWidth(); x++) { 
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

243  img(x, y, 0) = data(x, y, alpha); 
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

244  } 
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

245  } 
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

246  
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

247  Assume(img); 
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

248  } 
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

249  
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

250  void Bitmap::StripAlpha() { 
1142  251  if(!HasAlpha()) return; 
1004
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

252  
1142  253  int alpha = GetAlphaIndex(); 
1004
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

254  
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

255  auto &data = *this>data; 
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

256  
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

257  Containers::Image img(data.GetSize(), (ColorMode)((int)GetMode()&~(int)ColorMode::Alpha)); 
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

258  
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

259  for(int y=0; y<data.GetHeight(); y++) { 
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

260  for(int x=0; x<data.GetWidth(); x++) { 
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

261  int cc = 0; 
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

262  for(int c=0; c<(int)data.GetBytesPerPixel(); c++) { 
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

263  if(c!=alpha) { 
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

264  img(x, y, cc++) = data(x, y, c); 
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

265  } 
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

266  } 
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

267  } 
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

268  } 
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

269  
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

270  Assume(img); 
972
40b12ffaea3e
* Animation storage system to unify image, animation, and pointer
cemkalyoncu
parents:
932
diff
changeset

271  } 
40b12ffaea3e
* Animation storage system to unify image, animation, and pointer
cemkalyoncu
parents:
932
diff
changeset

272  
1010  273  std::vector<Geometry::Bounds> Bitmap::CreateLinearAtlas(Containers::Collection<const Bitmap> list, AtlasMargin margins) { 
1004
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

274  std::vector<Geometry::Bounds> ret; 
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

275  std::map<const Bitmap *, Geometry::Bounds> mapping; 
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

276  
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

277  int N = list.GetSize(); 
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

278  
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

279  if(N == 0) return ret; 
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

280  if(N == 1) return {Geometry::Bounds(0, 0, list[0].GetSize())}; 
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

281  
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

282  int marginwidth = 0; 
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

283  
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

284  if(margins == Zero) { 
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

285  marginwidth = 1; 
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

286  } 
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

287  
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

288  auto mode = list[0].GetMode(); 
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

289  
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

290  int minw = list[0].GetWidth() + marginwidth; 
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

291  
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

292  std::vector<const Bitmap *> ordering; 
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

293  for(auto &bmp : list) { 
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

294  ordering.push_back(&bmp); 
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

295  
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

296  if(minw > bmp.GetWidth() + marginwidth) 
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

297  minw = bmp.GetWidth() + marginwidth; 
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

298  } 
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

299  
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

300  //first height based, so the items with similar heights would be next to each other 
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

301  //second sort criteria is width as wider items should be packed at start. Items 
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

302  //are reverse sorted for performance 
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

303  list.Sort([](const Bitmap &l, const Bitmap &r) { 
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

304  if(l.GetHeight() == r.GetHeight()) 
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

305  return l.GetWidth() < r.GetWidth(); 
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

306  else 
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

307  return l.GetHeight() > r.GetHeight(); 
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

308  }); 
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

309  
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

310  int totalw = std::accumulate(list.begin(), list.end(), 0, [margins](int l, const Bitmap &r) { 
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

311  return l + r.GetWidth() + (margins == Zero ? 1 : 0); 
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

312  }); 
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

313  
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

314  int avgw = (int)std::ceil(float(totalw) / N); 
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

315  
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

316  // average line height would be skewed towards high 
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

317  int avgh = list[int(N*2/3)].GetHeight(); 
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

318  if(margins == Zero) 
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

319  avgh += 1; 
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

320  
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

321  int lines = (int)std::round(std::sqrt(float(N)/avgh*avgw)); 
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

322  
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

323  int x = 0, y = 0, maxy = 0; 
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

324  int w = (int)std::ceil((float)totalw/lines); 
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

325  w += minw; 
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

326  int maxx = 0; 
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

327  
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

328  
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

329  if(margins == Zero) { 
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

330  x = marginwidth; 
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

331  y = marginwidth; 
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

332  } 
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

333  
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

334  //build positions 
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

335  for(int i=0; i<N; i++) { 
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

336  auto &bmp = *list.Last(); 
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

337  if(x + bmp.GetWidth() + marginwidth < w) { 
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

338  auto size = bmp.GetSize(); 
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

339  
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

340  mapping[&bmp] = {x, y, size}; 
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

341  
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

342  x += size.Width + marginwidth; 
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

343  
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

344  if(size.Height > maxy) maxy = size.Height; 
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

345  
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

346  list.Remove(list.GetSize()1); 
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

347  } 
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

348  else { 
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

349  bool found = false; 
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

350  
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

351  if(w  x > minw) { //search to find a smaller image 
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

352  
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

353  int maxw = w  x  1  marginwidth; 
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

354  
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

355  for(auto it = list.Last(); it.IsValid(); it.Previous()) { 
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

356  auto size = it>GetSize(); 
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

357  
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

358  if(size.Width <= maxw) { 
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

359  mapping[it.CurrentPtr()] = {x, y, size}; 
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

360  
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

361  x += size.Width + marginwidth; 
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

362  
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

363  if(size.Height > maxy) maxy = size.Height; 
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

364  
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

365  it.Remove(); 
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

366  
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

367  found = true; 
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

368  break; 
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

369  } 
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

370  } 
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

371  } 
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

372  
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

373  if(!found) { //too small 
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

374  y += maxy + marginwidth; 
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

375  maxy = 0; 
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

376  
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

377  if(maxx < x) maxx = x; 
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

378  
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

379  if(margins == Zero) 
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

380  x = 1; 
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

381  else 
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

382  x = 0; 
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

383  
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

384  i; 
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

385  } 
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

386  } 
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

387  } 
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

388  
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

389  if(x) { 
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

390  y += maxy; 
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

391  } 
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

392  
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

393  Resize({maxx, y + marginwidth}, mode); 
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

394  
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

395  if(margins == Zero) { 
1019
c72a1acd9bba
* Drag and drop system is now working, dragsource still needs testing
cemkalyoncu
parents:
1010
diff
changeset

396  data>Clear(); 
1004
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

397  } 
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

398  
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

399  //channel count 
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

400  int C = GetBytesPerPixel(); 
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

401  
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

402  for(auto p : mapping) { 
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

403  auto b = p.second; 
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

404  
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

405  if(p.first>GetMode() != mode) 
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

406  throw std::runtime_error("Color modes are not uniform in atlas generation"); 
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

407  
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

408  for(int y=0; y<b.Height(); y++) { 
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

409  for(int x=0; x<b.Width(); x++) { 
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

410  for(int c=0; c<C; c++) { 
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

411  (*data)(x+b.Left, y+b.Top, c) = (*p.first)(x, y, c); 
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

412  } 
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

413  } 
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

414  } 
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

415  } 
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

416  
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

417  ret.reserve(N); 
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

418  for(int i=0; i<N; i++) { 
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

419  ret.push_back(mapping[ordering[i]]); 
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

420  } 
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

421  
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

422  //ExportPNG("atlas.png"); 
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

423  
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

424  return ret; 
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

425  } 
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

426  
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

427  std::vector<TextureImage> Bitmap::CreateAtlasImages(std::vector<Geometry::Bounds> boundaries) const { 
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

428  if(GetID() == 0) throw std::runtime_error("Cannot map atlas from an unprepared image"); 
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

429  
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

430  std::vector<TextureImage> ret; 
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

431  ret.reserve(boundaries.size()); 
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

432  
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

433  for(auto b : boundaries) { 
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

434  ret.emplace_back(GetID(), GetMode(), GetSize(), b); 
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

435  } 
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

436  
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

437  return ret; 
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

438  } 
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

439  
1010  440  Geometry::Margin Bitmap::Trim(bool left, bool top, bool right, bool bottom) { 
1004
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

441  ASSERT(data, "Image data does not exists"); 
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

442  
1158  443  auto ret = Trim({0, 0, GetSize()}, left, top, right, bottom); 
1004
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

444  
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

445  auto &data = *this>data; 
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

446  
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

447  if(ret.TotalX() == 0 && ret.TotalY() == 0) return ret; 
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

448  
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

449  if(ret.TotalX() >= data.GetWidth()  ret.TotalY() >= data.GetHeight()) 
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

450  return ret; 
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

451  
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

452  Containers::Image img(data.GetSize()  ret.Total(), GetMode()); 
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

453  
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

454  int yy=0; 
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

455  for(int y=ret.Top; y<data.GetHeight()  ret.Bottom; y++) { 
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

456  int xx=0; 
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

457  for(int x=ret.Left; x<data.GetWidth()  ret.Right; x++) { 
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

458  for(int c=0; c<(int)data.GetBytesPerPixel(); c++) { 
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

459  img(xx, yy, c) = data(x, y, c); 
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

460  } 
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

461  xx++; 
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

462  } 
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

463  yy++; 
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

464  } 
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

465  
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

466  Assume(img); 
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

467  
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

468  return ret; 
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

469  } 
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

470  
1158  471  Geometry::Margin Bitmap::Trim(Geometry::Bounds bounds, bool left, bool top, bool right, bool bottom) { 
472  ASSERT(data, "Image data does not exists");  
473  
474  Geometry::Margin ret(0, 0, 0, 0);  
475  
476  if(!HasAlpha())  
477  return ret;  
478  
479  int alpha = GetAlphaIndex();  
480  
481  auto &data = *this>data;  
482  
483  if(left) {  
484  for(int x=bounds.Left; x<bounds.Right; x++) {  
485  bool empty = true;  
486  
487  for(int y=bounds.Top; y<bounds.Bottom; y++) {  
488  if(data(x, y, alpha) != 0) {  
489  empty = false;  
490  break;  
491  }  
492  }  
493  
494  if(empty) {  
495  ret.Left++;  
496  }  
497  else {  
498  break;  
499  }  
500  }  
501  }  
502  
503  if(top) {  
504  for(int y=bounds.Top; y<bounds.Bottom; y++) {  
505  bool empty = true;  
506  
507  for(int x=bounds.Left; x<bounds.Right; x++) {  
508  if(data(x, y, alpha) != 0) {  
509  empty = false;  
510  break;  
511  }  
512  }  
513  
514  if(empty) {  
515  ret.Top++;  
516  }  
517  else {  
518  break;  
519  }  
520  }  
521  }  
522  
523  if(right) {  
524  for(int x=bounds.Right1; x>=bounds.Left; x) {  
525  bool empty = true;  
526  
527  for(int y=bounds.Top; y<bounds.Bottom; y++) {  
528  if(data(x, y, alpha) != 0) {  
529  empty = false;  
530  break;  
531  }  
532  }  
533  
534  if(empty) {  
535  ret.Right++;  
536  }  
537  else {  
538  break;  
539  }  
540  }  
541  }  
542  
543  if(bottom) {  
544  for(int y=bounds.Bottom1; y>=bounds.Top; y) {  
545  bool empty = true;  
546  
547  for(int x=bounds.Left; x<bounds.Right; x++) {  
548  if(data(x, y, alpha) != 0) {  
549  empty = false;  
550  break;  
551  }  
552  }  
553  
554  if(empty) {  
555  ret.Bottom++;  
556  }  
557  else {  
558  break;  
559  }  
560  }  
561  }  
562  
563  return ret;  
564  }  
565  
1004
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

566  Containers::Image Bitmap::ReleaseData() { 
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

567  if(data==nullptr) { 
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

568  #ifndef NDEBUG 
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

569  ASSERT_DUMP(false, "No data to release"); 
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

570  #endif 
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

571  
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

572  return { }; 
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

573  } 
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

574  else { 
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

575  Containers::Image temp=std::move(*data); 
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

576  
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

577  delete data; 
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

578  data=nullptr; 
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

579  
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

580  return temp; 
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

581  } 
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

582  } 
1158  583  
584  bool Bitmap::IsEmpty(Geometry::Bounds bounds) const {  
585  if(bounds.GetSize().Area() == 0)  
586  return true;  
587  
588  if(!HasAlpha())  
589  return false;  
590  
591  int alpha = GetAlphaIndex();  
592  
593  auto &data = *this>data;  
594  
595  for(int y=bounds.Top; y<bounds.Bottom; y++) {  
596  for(int x=bounds.Left; x<bounds.Right; x++) {  
597  if(data(x, y, alpha) > 0)  
598  return false;  
599  }  
600  }  
601  
602  return true;  
603  }  
1004
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

604  
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

605  Graphics::Bitmap Bitmap::Rotate90() const { 
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

606  ASSERT(data, "Bitmap data is not set"); 
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

607  
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

608  Graphics::Bitmap target(GetHeight(), GetWidth(), GetMode()); 
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

609  
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

610  int h = target.GetHeight(); 
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

611  ForAllPixels([&](int x, int y, int c) { 
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

612  target(y, h  x  1, c) = (*this)(x, y, c); 
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

613  }); 
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

614  
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

615  return target; 
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

616  } 
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

617  
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

618  Graphics::Bitmap Bitmap::Rotate180() const { 
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

619  ASSERT(data, "Bitmap data is not set"); 
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

620  
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

621  Graphics::Bitmap target(GetSize(), GetMode()); 
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

622  
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

623  int h = target.GetHeight(); 
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

624  int w = target.GetWidth(); 
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

625  ForAllPixels([&](int x, int y, int c) { 
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

626  target(w  x  1, h  y  1, c) = (*this)(x, y, c); 
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

627  }); 
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

628  
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

629  return target; 
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

630  } 
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

631  
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

632  Graphics::Bitmap Bitmap::Rotate270() { 
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

633  ASSERT(data, "Bitmap data is not set"); 
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

634  
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

635  Graphics::Bitmap target(GetHeight(), GetWidth(), GetMode()); 
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

636  
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

637  int w = target.GetWidth(); 
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

638  ForAllPixels([&](int x, int y, int c) { 
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

639  target(w  y  1, x, c) = (*this)(x, y, c); 
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

640  }); 
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

641  
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

642  return target; 
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

643  } 
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

644  
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

645  
b6b47ba90e06
* MaskedObject now can work with different animation types, not perfect,
cemkalyoncu
parents:
986
diff
changeset

646  } } 