virtual_image.cpp 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  1. /*
  2. XCC Utilities and Library
  3. Copyright (C) 2000 Olaf van der Spek <[email protected]>
  4. This program is free software: you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation, either version 3 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program. If not, see <https://www.gnu.org/licenses/>.
  14. */
  15. #include "stdafx.h"
  16. #include <windows.h>
  17. #include <gdiplus.h>
  18. #include <shlwapi.h>
  19. #ifndef XCC_MINIMAL_BUILD
  20. #include "dds_file.h"
  21. #include "image_file.h"
  22. #include "jpeg_file.h"
  23. #include "pcx_file.h"
  24. #include "pcx_file_write.h"
  25. #include "png_file.h"
  26. #include "tga_file.h"
  27. #endif
  28. #include "virtual_image.h"
  29. using namespace Gdiplus;
  30. Cvirtual_image::Cvirtual_image(const Cvirtual_binary& image, int cx, int cy, int cb_pixel, const t_palet_entry* palet, bool inflate)
  31. {
  32. load(image, cx, cy, cb_pixel, palet, inflate);
  33. }
  34. Cvirtual_image::Cvirtual_image(const void* image, int cx, int cy, int cb_pixel, const t_palet_entry* palet, bool inflate)
  35. {
  36. load(image, cx, cy, cb_pixel, palet, inflate);
  37. }
  38. const Cvirtual_image& Cvirtual_image::palet(const t_palet_entry* palet, bool inflate)
  39. {
  40. if (palet)
  41. {
  42. memcpy(m_palet.write_start(sizeof(t_palet)), palet, sizeof(t_palet));
  43. if (inflate)
  44. convert_palet_18_to_24(reinterpret_cast<t_palet_entry*>(m_palet.data_edit()));
  45. }
  46. else
  47. m_palet.clear();
  48. return *this;
  49. }
  50. void Cvirtual_image::load(const Cvirtual_binary& image, int cx, int cy, int cb_pixel, const t_palet_entry* p, bool inflate)
  51. {
  52. assert(cb_pixel == 1 || cb_pixel == 3 || cb_pixel == 4);
  53. m_cx = cx;
  54. m_cy = cy;
  55. mcb_pixel = cb_pixel;
  56. if (image.size() == cb_image())
  57. m_image = image;
  58. else
  59. m_image.write_start(cb_image());
  60. palet(p, inflate);
  61. }
  62. void Cvirtual_image::load(const void* image, int cx, int cy, int cb_pixel, const t_palet_entry* p, bool inflate)
  63. {
  64. assert(cb_pixel == 1 || cb_pixel == 3 || cb_pixel == 4);
  65. m_cx = cx;
  66. m_cy = cy;
  67. mcb_pixel = cb_pixel;
  68. m_image.write_start(cb_image());
  69. if (image)
  70. memcpy(m_image.data_edit(), image, cb_image());
  71. palet(p, inflate);
  72. }
  73. #ifndef XCC_MINIMAL_BUILD
  74. int Cvirtual_image::load(const Cvirtual_binary& s)
  75. {
  76. Cdds_file dds_f;
  77. Cpcx_file pcx_f;
  78. Cpng_file png_f;
  79. Ctga_file tga_f;
  80. if (dds_f.load(s), dds_f.is_valid())
  81. *this = dds_f.vimage();
  82. else if (pcx_f.load(s), pcx_f.is_valid())
  83. *this = pcx_f.vimage();
  84. else if (png_f.load(s), png_f.is_valid())
  85. return png_f.decode(*this);
  86. else if (tga_f.load(s), tga_f.is_valid())
  87. return tga_f.decode(*this);
  88. else
  89. {
  90. IStream* is = SHCreateMemStream(s.data(), s.size());
  91. Gdiplus::Bitmap bmp(is);
  92. is->Release();
  93. if (bmp.GetLastStatus() != Ok)
  94. return 1;
  95. PixelFormat pf = bmp.GetPixelFormat();
  96. if (bmp.GetPixelFormat() & PixelFormatIndexed)
  97. {
  98. load(NULL, bmp.GetWidth(), bmp.GetHeight(), 1, NULL);
  99. BitmapData d;
  100. d.Stride = bmp.GetWidth();
  101. d.Scan0 = image_edit();
  102. bmp.LockBits(NULL, ImageLockModeRead | ImageLockModeUserInputBuf, PixelFormat8bppIndexed, &d);
  103. bmp.UnlockBits(&d);
  104. }
  105. else
  106. {
  107. load(NULL, bmp.GetWidth(), bmp.GetHeight(), 3, NULL);
  108. BitmapData d;
  109. d.Stride = bmp.GetWidth() * 3;
  110. d.Scan0 = image_edit();
  111. bmp.LockBits(NULL, ImageLockModeRead | ImageLockModeUserInputBuf, PixelFormat24bppRGB, &d);
  112. bmp.UnlockBits(&d);
  113. swap_rb();
  114. }
  115. }
  116. return 0;
  117. }
  118. int Cvirtual_image::load(const Cvirtual_file& f)
  119. {
  120. return load(f.read());
  121. }
  122. int Cvirtual_image::load(const string& fname)
  123. {
  124. Cvirtual_binary s;
  125. int error = s.load(fname);
  126. if (!error)
  127. error = load(s);
  128. return error;
  129. }
  130. int Cvirtual_image::save(Cvirtual_file& f, t_file_type ft) const
  131. {
  132. return image_file_write(f, ft, image(), palet(), m_cx, m_cy);
  133. }
  134. Cvirtual_file Cvirtual_image::save(t_file_type ft) const
  135. {
  136. return image_file_write(ft, image(), palet(), m_cx, m_cy);
  137. }
  138. int Cvirtual_image::save(const string& fname, t_file_type ft) const
  139. {
  140. return image_file_write(fname, ft, image(), palet(), m_cx, m_cy);
  141. }
  142. void Cvirtual_image::swap_rb()
  143. {
  144. int count = m_cx * m_cy;
  145. t_palet_entry* r = reinterpret_cast<t_palet_entry*>(m_image.data_edit());
  146. while (count--)
  147. {
  148. swap(r->r, r->b);
  149. r++;
  150. }
  151. }
  152. static void flip_frame(const byte* s, byte* d, int cx, int cy, int cb_pixel)
  153. {
  154. int cb_line = cx * cb_pixel;
  155. const byte* r = s;
  156. byte* w = d + cb_line * cy;
  157. while (cy--)
  158. {
  159. w -= cb_line;
  160. memcpy(w, r, cb_line);
  161. r += cb_line;
  162. }
  163. }
  164. void Cvirtual_image::flip()
  165. {
  166. Cvirtual_binary t = m_image;
  167. flip_frame(t.data(), image_edit(), cx(), cy(), cb_pixel());
  168. }
  169. void Cvirtual_image::cb_pixel(int cb_pixel, const t_palet_entry* palet)
  170. {
  171. if (cb_pixel < mcb_pixel)
  172. decrease_color_depth(cb_pixel, palet);
  173. else if (cb_pixel > mcb_pixel)
  174. increase_color_depth(cb_pixel);
  175. }
  176. void Cvirtual_image::decrease_color_depth(int new_cb_pixel, const t_palet_entry* palet)
  177. {
  178. if (new_cb_pixel == 3)
  179. {
  180. remove_alpha();
  181. return;
  182. }
  183. assert(new_cb_pixel == 1);
  184. int old_cb_pixel = cb_pixel();
  185. Cvirtual_binary t = m_image;
  186. load(NULL, cx(), cy(), new_cb_pixel, palet);
  187. byte* w = image_edit();
  188. int count = m_cx * m_cy;
  189. if (old_cb_pixel == 3)
  190. {
  191. const t_palet_entry* r = reinterpret_cast<const t_palet_entry*>(t.data());
  192. while (count--)
  193. {
  194. *w++ = find_color(r->r, r->g, r->b, palet);
  195. r++;
  196. }
  197. }
  198. else
  199. {
  200. assert(old_cb_pixel == 4);
  201. const t_palet32entry* r = reinterpret_cast<const t_palet32entry*>(t.data());
  202. while (count--)
  203. {
  204. *w++ = r->a < 0x80 ? find_color(r->r, r->g, r->b, palet) : 0;
  205. r++;
  206. }
  207. }
  208. }
  209. static t_palet32entry p32e(int r, int g, int b, int a = 0)
  210. {
  211. t_palet32entry e;
  212. e.r = r;
  213. e.g = g;
  214. e.b = b;
  215. e.a = a;
  216. return e;
  217. }
  218. static t_palet32entry p32e(t_palet_entry e)
  219. {
  220. return p32e(e.r, e.g, e.b);
  221. }
  222. static t_palet32entry p32e(const t_palet palet, int i)
  223. {
  224. return i ? p32e(palet[i]) : p32e(0x80, 0x80, 0x80, 0xff);
  225. }
  226. void Cvirtual_image::increase_color_depth(int new_cb_pixel)
  227. {
  228. if (cb_pixel() == 3)
  229. {
  230. if (new_cb_pixel == 4)
  231. add_alpha();
  232. return;
  233. }
  234. assert(cb_pixel() == 1);
  235. Cvirtual_image t = *this;
  236. const byte* r = t.image();
  237. load(NULL, cx(), cy(), new_cb_pixel, NULL);
  238. int count = m_cx * m_cy;
  239. if (cb_pixel() == 3)
  240. {
  241. t_palet_entry* w = reinterpret_cast<t_palet_entry*>(image_edit());
  242. while (count--)
  243. *w++ = t.palet()[*r++];
  244. }
  245. else
  246. {
  247. assert(cb_pixel() == 4);
  248. t_palet32entry* w = reinterpret_cast<t_palet32entry*>(image_edit());
  249. while (count--)
  250. *w++ = p32e(t.palet(), *r++);
  251. }
  252. }
  253. void Cvirtual_image::add_alpha()
  254. {
  255. assert(cb_pixel() == 3);
  256. Cvirtual_binary t = m_image;
  257. load(NULL, cx(), cy(), 4, NULL);
  258. int count = m_cx * m_cy;
  259. const byte* r = t.data();
  260. byte* w = image_edit();
  261. while (count--)
  262. {
  263. *w++ = *r++;
  264. *w++ = *r++;
  265. *w++ = *r++;
  266. *w++ = 0;
  267. }
  268. }
  269. void Cvirtual_image::remove_alpha()
  270. {
  271. if (cb_pixel() != 4)
  272. return;
  273. Cvirtual_binary t = m_image;
  274. load(NULL, cx(), cy(), 3, NULL);
  275. int count = m_cx * m_cy;
  276. const byte* r = t.data();
  277. byte* w = image_edit();
  278. while (count--)
  279. {
  280. *w++ = *r++;
  281. *w++ = *r++;
  282. *w++ = *r++;
  283. r++;
  284. }
  285. }
  286. void Cvirtual_image::increase_palet_depth()
  287. {
  288. assert(false);
  289. Cvirtual_binary t = m_palet;
  290. const t_palet_entry* s = reinterpret_cast<const t_palet_entry*>(t.data());
  291. t_palet_entry* d = reinterpret_cast<t_palet_entry*>(t.data_edit());
  292. for (int i = 0; i < 256; i++)
  293. {
  294. d[i].r = (s[i].r & 63) * 255 / 63;
  295. d[i].g = (s[i].g & 63) * 255 / 63;
  296. d[i].b = (s[i].b & 63) * 255 / 63;
  297. }
  298. }
  299. #endif