ImageData.cpp 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. /**
  2. * Copyright (c) 2006-2009 LOVE Development Team
  3. *
  4. * This software is provided 'as-is', without any express or implied
  5. * warranty. In no event will the authors be held liable for any damages
  6. * arising from the use of this software.
  7. *
  8. * Permission is granted to anyone to use this software for any purpose,
  9. * including commercial applications, and to alter it and redistribute it
  10. * freely, subject to the following restrictions:
  11. *
  12. * 1. The origin of this software must not be misrepresented; you must not
  13. * claim that you wrote the original software. If you use this software
  14. * in a product, an acknowledgment in the product documentation would be
  15. * appreciated but is not required.
  16. * 2. Altered source versions must be plainly marked as such, and must not be
  17. * misrepresented as being the original software.
  18. * 3. This notice may not be removed or altered from any source distribution.
  19. **/
  20. #include "ImageData.h"
  21. // STD
  22. #include <iostream>
  23. // LOVE
  24. #include <common/Exception.h>
  25. namespace love
  26. {
  27. namespace image
  28. {
  29. namespace devil
  30. {
  31. void ImageData::load(Data * data)
  32. {
  33. // Generate DevIL image.
  34. ilGenImages(1, &image);
  35. // Bind the image.
  36. ilBindImage(image);
  37. // Try to load the image.
  38. ILboolean success = ilLoadL(IL_TYPE_UNKNOWN, (void*)data->getData(), data->getSize());
  39. // Check for errors
  40. if(!success)
  41. {
  42. throw love::Exception("Could not decode image!");
  43. return;
  44. }
  45. width = ilGetInteger(IL_IMAGE_WIDTH);
  46. height = ilGetInteger(IL_IMAGE_HEIGHT);
  47. origin = ilGetInteger(IL_IMAGE_ORIGIN);
  48. // Make sure the image is in RGBA format.
  49. ilConvertImage(IL_RGBA, IL_UNSIGNED_BYTE);
  50. // This should always be four.
  51. bpp = ilGetInteger(IL_IMAGE_BPP);
  52. if(bpp != 4)
  53. {
  54. std::cerr << "Bits per pixel != 4" << std::endl;
  55. return;
  56. }
  57. }
  58. ImageData::ImageData(Data * data)
  59. {
  60. load(data);
  61. }
  62. ImageData::ImageData(filesystem::File * file)
  63. {
  64. Data * data = file->read();
  65. load(data);
  66. data->release();
  67. }
  68. ImageData::ImageData(int width, int height)
  69. : width(width), height(height), origin(IL_ORIGIN_UPPER_LEFT), bpp(4)
  70. {
  71. // Generate DevIL image.
  72. ilGenImages(1, &image);
  73. // Bind the image.
  74. ilBindImage(image);
  75. ilTexImage(width, height, 1, bpp, IL_RGBA, IL_UNSIGNED_BYTE, 0);
  76. // Set to black.
  77. memset((void*)ilGetData(), 0, width*height*4);
  78. }
  79. ImageData::ImageData(int width, int height, void *data)
  80. : width(width), height(height), origin(IL_ORIGIN_UPPER_LEFT), bpp(4)
  81. {
  82. // Generate DevIL image.
  83. ilGenImages(1, &image);
  84. // Bind the image.
  85. ilBindImage(image);
  86. // Try to load the data.
  87. bool success = (ilTexImage(width, height, 1, bpp, IL_RGBA, IL_UNSIGNED_BYTE, data) == IL_TRUE);
  88. int err = ilGetError();
  89. if (err != IL_NO_ERROR){
  90. switch (err) {
  91. case IL_ILLEGAL_OPERATION:
  92. throw love::Exception("Error: Illegal operation");
  93. break;
  94. case IL_INVALID_PARAM:
  95. throw love::Exception("Error: invalid parameters");
  96. break;
  97. case IL_OUT_OF_MEMORY:
  98. throw love::Exception("Error: out of memory");
  99. break;
  100. default:
  101. throw love::Exception("Error: unknown error");
  102. break;
  103. }
  104. }
  105. if(!success) {
  106. throw love::Exception("Could not decode image data.");
  107. }
  108. }
  109. ImageData::~ImageData()
  110. {
  111. ilDeleteImages(1, &image);
  112. }
  113. int ImageData::getWidth() const
  114. {
  115. return width;
  116. }
  117. int ImageData::getHeight() const
  118. {
  119. return height;
  120. }
  121. void * ImageData::getData() const
  122. {
  123. ilBindImage(image);
  124. return ilGetData();
  125. }
  126. int ImageData::getSize() const
  127. {
  128. return width*height*bpp;
  129. }
  130. void ImageData::setPixel(int x, int y, pixel c)
  131. {
  132. int tx = x > width-1 ? width-1 : x;
  133. int ty = y > height-1 ? height-1 : y;
  134. pixel * pixels = (pixel *)getData();
  135. pixels[y*width+x] = c;
  136. }
  137. pixel ImageData::getPixel(int x, int y) const
  138. {
  139. int tx = x > width-1 ? width-1 : x;
  140. int ty = y > height-1 ? height-1 : y;
  141. pixel * pixels = (pixel *)getData();
  142. return pixels[y*width+x];
  143. }
  144. love::image::EncodedImageData * ImageData::encodeImageData(love::image::ImageData * d, EncodedImageData::Format f) {
  145. ILubyte * data;
  146. ILuint w = d->getWidth();
  147. int h = d->getHeight(); // has to be a signed int so we can make it negative for BMPs
  148. int headerLen, bpp, row, size, padding, filesize;
  149. switch (f) {
  150. case EncodedImageData::FORMAT_BMP:
  151. headerLen = 54;
  152. bpp = 3;
  153. row = w * bpp;
  154. padding = row & 3;
  155. size = h * (row + padding);
  156. filesize = size + headerLen;
  157. data = new ILubyte[filesize];
  158. // Here's the header for the BMP file format.
  159. data[0] = 66; // "B"
  160. data[1] = 77; // "M"
  161. data[2] = filesize & 255; // size of the file
  162. data[3] = (filesize >> 8) & 255;
  163. data[4] = (filesize >> 16) & 255;
  164. data[5] = (filesize >> 24) & 255;
  165. data[6] = data[7] = data[8] = data[9] = 0; // useless reserved values
  166. data[10] = headerLen; // offset where pixel data begins
  167. data[11] = (headerLen >> 8) & 255;
  168. data[12] = (headerLen >> 16) & 255;
  169. data[13] = (headerLen >> 24) & 255;
  170. data[14] = headerLen - 14; // length of this part of the header
  171. data[15] = ((headerLen - 14) >> 8) & 255;
  172. data[16] = ((headerLen - 14) >> 16) & 255;
  173. data[17] = ((headerLen - 14) >> 24) & 255;
  174. data[18] = w & 255; // width of the bitmap
  175. data[19] = (w >> 8) & 255;
  176. data[20] = (w >> 16) & 255;
  177. data[21] = (w >> 24) & 255;
  178. data[22] = -h & 255; // negative height of the bitmap - used so we don't have to flip the data
  179. data[23] = ((-h) >> 8) & 255;
  180. data[24] = ((-h) >> 16) & 255;
  181. data[25] = ((-h) >> 24) & 255;
  182. data[26] = 1; // number of color planes
  183. data[27] = 0;
  184. data[28] = bpp * 8; // bits per pixel
  185. data[29] = 0;
  186. data[30] = data[31] = data[32] = data[33] = 0; // RGB - no compression
  187. data[34] = (row + padding) * h; // length of the pixel data
  188. data[35] = (((row + padding) * h) >> 8) & 255;
  189. data[36] = (((row + padding) * h) >> 16) & 255;
  190. data[37] = (((row + padding) * h) >> 24) & 255;
  191. data[38] = 2835 & 255; // horizontal pixels per meter
  192. data[39] = (2835 >> 8) & 255;
  193. data[40] = (2835 >> 16) & 255;
  194. data[41] = (2835 >> 24) & 255;
  195. data[42] = 2835 & 255; // vertical pixels per meter
  196. data[43] = data[39];
  197. data[44] = data[40];
  198. data[45] = data[41];
  199. data[46] = data[47] = data[48] = data[49] = 0; // number of colors in the palette
  200. data[50] = data[51] = data[52] = data[53] = 0; // all colors are important!
  201. // Okay, header's done! Now for the pixel data...
  202. data += headerLen;
  203. d->getData(); // bind the imagedata's image
  204. for (int i = 0; i < h; i++) { // we've got to loop through the rows, adding the pixel data plus padding
  205. ilCopyPixels(0,i,0,w,1,1,IL_BGR,IL_UNSIGNED_BYTE,data);
  206. data += row;
  207. }
  208. data -= filesize;
  209. break;
  210. case EncodedImageData::FORMAT_TGA:
  211. default: // TGA is the default format
  212. headerLen = 18;
  213. bpp = 3;
  214. size = h * w * bpp;
  215. data = new ILubyte[size + headerLen];
  216. // here's the header for the Targa file format.
  217. data[0] = 0; // ID field size
  218. data[1] = 0; // colormap type
  219. data[2] = 2; // image type
  220. data[3] = data[4] = 0; // colormap start
  221. data[5] = data[6] = 0; // colormap length
  222. data[7] = 32; // colormap bits
  223. data[8] = data[9] = 0; // x origin
  224. data[10] = data[11] = 0; // y origin
  225. // Targa is little endian, so:
  226. data[12] = w & 255; // least significant byte of width
  227. data[13] = w >> 8; // most significant byte of width
  228. data[14] = h & 255; // least significant byte of height
  229. data[15] = h >> 8; // most significant byte of height
  230. data[16] = bpp * 8; // bits per pixel
  231. data[17] = 0; // descriptor bits
  232. // header done. write the pixel data to TGA:
  233. data += headerLen;
  234. d->getData(); // bind the imagedata's image
  235. ilCopyPixels(0,0,0,w,h,1,IL_BGR,IL_UNSIGNED_BYTE,data); // convert the pixels to BGR (remember, little-endian) and copy them to data
  236. // It's Targa, so we have to flip the image.
  237. row = w * bpp;
  238. ILubyte * temp = new ILubyte[row];
  239. ILubyte * src = data - row;
  240. ILubyte * dst = data + size;
  241. for (int i = 0; i < (h >> 1); i++) {
  242. memcpy(temp,src+=row,row);
  243. memcpy(src,dst-=row,row);
  244. memcpy(dst,temp,row);
  245. }
  246. data -= headerLen;
  247. delete [] temp;
  248. }
  249. return new love::image::EncodedImageData(data, f, size + headerLen);
  250. }
  251. } // devil
  252. } // image
  253. } // love