PolyImage.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569
  1. /*
  2. * PolyImage.cpp
  3. * Poly
  4. *
  5. * Created by Ivan Safrin on 3/13/08.
  6. * Copyright 2008 Ivan Safrin. All rights reserved.
  7. *
  8. */
  9. #include "PolyImage.h"
  10. using namespace Polycode;
  11. void user_read_data(png_structp png_ptr, png_bytep data, png_size_t length) {
  12. OSFILE *file = (OSFILE*)png_get_io_ptr(png_ptr);
  13. OSBasics::read(data, length, 1, file);
  14. }
  15. Image::Image(String fileName) : imageData(NULL) {
  16. setPixelType(IMAGE_RGBA);
  17. loaded = false;
  18. if(!loadImage(fileName)) {
  19. Logger::log("Error loading %s\n", fileName.c_str());
  20. } else {
  21. loaded = true;
  22. // Logger::log("image loaded!");
  23. }
  24. }
  25. void Image::setPixelType(int type) {
  26. imageType = type;
  27. switch(imageType) {
  28. case IMAGE_RGB:
  29. pixelSize = 3;
  30. break;
  31. case IMAGE_RGBA:
  32. pixelSize = 4;
  33. break;
  34. pixelSize = 4;
  35. default:
  36. break;
  37. }
  38. }
  39. bool Image::isLoaded() {
  40. return loaded;
  41. }
  42. Image::Image(int width, int height, int type) : imageData(NULL) {
  43. setPixelType(type);
  44. createEmpty(width, height);
  45. }
  46. Image::Image(Image *copyImage) {
  47. setPixelType(copyImage->getType());
  48. width = copyImage->getWidth();
  49. height = copyImage->getHeight();
  50. imageData = (char*)malloc(width*height*pixelSize);
  51. memcpy(imageData, copyImage->getPixels(), width*height*pixelSize);
  52. }
  53. Image::Image(char *data, int width, int height, int type) {
  54. setPixelType(type);
  55. imageData = (char*)malloc(width*height*pixelSize);
  56. memcpy(imageData, data, width*height*pixelSize);
  57. this->width = width;
  58. this->height = height;
  59. }
  60. Image::Image() : imageData(NULL) {
  61. }
  62. Image::~Image() {
  63. if(imageData != NULL)
  64. free(imageData);
  65. }
  66. char *Image::getPixels() {
  67. return imageData;
  68. }
  69. Color Image::getPixel(int x, int y) {
  70. if(x < 0 || x >= width || y < 0 || y >= height)
  71. return Color(0,0,0,0);
  72. unsigned int *imageData32 = (unsigned int*)imageData;
  73. return Color(imageData32[x+(y*width)]);
  74. }
  75. unsigned int Image::getWidth() {
  76. return width;
  77. }
  78. unsigned int Image::getHeight() {
  79. return height;
  80. }
  81. void Image::createEmpty(unsigned int width, unsigned int height) {
  82. if(imageData != NULL)
  83. free(imageData);
  84. imageData = (char*)malloc(width*height*pixelSize);
  85. this->width = width;
  86. this->height = height;
  87. fill(0,0,0,0);
  88. }
  89. void Image::perlinNoise(int seed, bool alpha) {
  90. Perlin *perlin = new Perlin(12,33,1,seed);
  91. unsigned int *imageData32 = (unsigned int*)imageData;
  92. Color pixelColor;
  93. Number noiseVal;
  94. for(int i=0; i < width*height;i++) {
  95. noiseVal = fabs(1.0f/perlin->Get( 0.1+(0.9f/((Number)width)) * (i%width), (1.0f/((Number)height)) * (i - (i%width))));
  96. if(alpha)
  97. pixelColor.setColor(noiseVal, noiseVal, noiseVal, noiseVal);
  98. else
  99. pixelColor.setColor(noiseVal, noiseVal, noiseVal, 1.0f);
  100. imageData32[i] = pixelColor.getUint();
  101. }
  102. }
  103. void Image::writeBMP(String fileName) {
  104. // SDL_Surface *image;
  105. // image = SDL_CreateRGBSurface(SDL_SWSURFACE, width, height, 32, 0x0000FF, 0x00FF00, 0xFF0000, 0x000000);
  106. // memcpy(image->pixels,imageData,width * height * 4);
  107. // SDL_SaveBMP(image, fileName.c_str());
  108. }
  109. void Image::drawRect(int x, int y, int w, int h, Color col) {
  110. for(int i=0; i < w; i++) {
  111. for(int j=0; j < h; j++) {
  112. setPixel(x+i,y+j,col);
  113. }
  114. }
  115. }
  116. void Image::setPixel(int x, int y, Color col) {
  117. if(x < 0 || x >= width || y < 0 || y >= height)
  118. return;
  119. unsigned int *imageData32 = (unsigned int*)imageData;
  120. imageData32[x+(y*width)] = col.getUint();
  121. }
  122. void Image::setAAPixel(int x, int y, Color col) {
  123. if(x < 0 || x > width || y < 0 || y > height)
  124. return;
  125. setPixel(x,y,col);
  126. col.a *= 0.2;
  127. setPixel(x+1,y,col);
  128. setPixel(x-1,y,col);
  129. setPixel(x,y+1,col);
  130. setPixel(x,y-1,col);
  131. }
  132. void Image::move(int x, int y) {
  133. brushPosX += x;
  134. brushPosY += y;
  135. }
  136. void Image::moveTo(int x, int y) {
  137. brushPosX = x;
  138. brushPosY = y;
  139. }
  140. int Image::getBrushX() {
  141. return brushPosX;
  142. }
  143. int Image::getBrushY() {
  144. return brushPosY;
  145. }
  146. void Image::lineTo(int x, int y, Color col) {
  147. line(brushPosX, brushPosY, brushPosX+x, brushPosY+y, col);
  148. }
  149. void Image::setPixel(int x, int y, Number r, Number g, Number b, Number a) {
  150. if(x < 0 || x > width || y < 0 || y > height)
  151. return;
  152. Color color;
  153. color.setColor(r,g,b,a);
  154. unsigned int *imageData32 = (unsigned int*)imageData;
  155. imageData32[x+(y*width)] = color.getUint();
  156. }
  157. void Image::multiply(Number amt, bool color, bool alpha) {
  158. int startIndex = 0;
  159. int endIndex = 3;
  160. if(!color)
  161. startIndex = 3;
  162. if(!alpha)
  163. endIndex = 2;
  164. for (int i = 0; i < height*width*pixelSize; i+=pixelSize) {
  165. for(int j = startIndex; j < endIndex+1;j++) {
  166. if(((Number)imageData[i+j]) * amt< 0)
  167. imageData[i+j] = 0;
  168. else if(((Number)imageData[i+j]) * amt > 255)
  169. imageData[i+j] = 255;
  170. else
  171. imageData[i+j] = (char)(((Number)imageData[i+j]) * amt);
  172. }
  173. }
  174. }
  175. void Image::darken(Number amt, bool color, bool alpha) {
  176. char decAmt = 255.0f * amt;
  177. int startIndex = 0;
  178. int endIndex = 3;
  179. if(!color)
  180. startIndex = 3;
  181. if(!alpha)
  182. endIndex = 2;
  183. for (int i = 0; i < height*width*pixelSize; i+=pixelSize) {
  184. for(int j = startIndex; j < endIndex+1;j++) {
  185. if(imageData[i+j]-decAmt < 0)
  186. imageData[i+j] = 0;
  187. else
  188. imageData[i+j] -= decAmt;
  189. }
  190. }
  191. }
  192. void Image::lighten(Number amt, bool color, bool alpha) {
  193. char decAmt = 255.0f * amt;
  194. int startIndex = 0;
  195. int endIndex = 3;
  196. if(!color)
  197. startIndex = 3;
  198. if(!alpha)
  199. endIndex = 2;
  200. for (int i = 0; i < height*width*pixelSize; i+=pixelSize) {
  201. for(int j = startIndex; j < endIndex+1;j++) {
  202. if(imageData[i+j]+decAmt > 255)
  203. imageData[i+j] = 255;
  204. else
  205. imageData[i+j] += decAmt;
  206. }
  207. }
  208. }
  209. void Image::fastBlurHor(int blurSize) {
  210. if(blurSize == 0)
  211. return;
  212. unsigned char *blurImage = (unsigned char*)malloc(width*height*pixelSize);
  213. int total_r;
  214. int total_g;
  215. int total_b;
  216. int total_a;
  217. unsigned int *imageData32 = (unsigned int*)imageData;
  218. unsigned char *pixel;
  219. int amt;
  220. for (int y = 1; y < height; y++) {
  221. for (int x = 0; x < width; x++) {
  222. total_r = 0;
  223. total_g = 0;
  224. total_b = 0;
  225. total_a = 0;
  226. amt = 0;
  227. for (int kx = -blurSize; kx <= blurSize; kx++) {
  228. if((x+kx > 0 && x+kx < width) && (x+kx+((y)*width) > 0 && x+kx+((y)*width) < width*height)) {
  229. pixel = (unsigned char*)&(imageData32[(x+kx)+((y)*width)]);
  230. total_r += pixel[0];
  231. total_g += pixel[1];
  232. total_b += pixel[2];
  233. total_a += pixel[3];
  234. amt++;
  235. }
  236. }
  237. // Logger::log("%d / %d = %d\n",total_r, amt, (total_r/amt));
  238. blurImage[((x+(y*width))*pixelSize)] = (total_r/amt);
  239. blurImage[((x+(y*width))*pixelSize)+1] = (total_g / amt);
  240. blurImage[((x+(y*width))*pixelSize)+2] = (total_b / amt);
  241. blurImage[((x+(y*width))*pixelSize)+3] = (total_a / amt);
  242. }
  243. }
  244. free(imageData);
  245. imageData = (char*)blurImage;
  246. // free(imageData32);
  247. }
  248. void Image::fastBlurVert(int blurSize) {
  249. if(blurSize == 0)
  250. return;
  251. unsigned char *blurImage = (unsigned char*)malloc(width*height*pixelSize);
  252. int total_r;
  253. int total_g;
  254. int total_b;
  255. int total_a;
  256. unsigned int *imageData32 = (unsigned int*)imageData;
  257. unsigned char *pixel;
  258. int amt;
  259. for (int y = 0; y < height; y++) {
  260. for (int x = 0; x < width; x++) {
  261. total_r = 0;
  262. total_g = 0;
  263. total_b = 0;
  264. total_a = 0;
  265. amt = 0;
  266. for (int ky = -blurSize; ky <= blurSize; ky++) {
  267. if((y+ky > 0 && y+ky < height) && (x+((y+ky)*width) > 0 && x+((y+ky)*width) < width*height)) {
  268. pixel = (unsigned char*)&(imageData32[(x)+((y+ky)*width)]);
  269. total_r += pixel[0];
  270. total_g += pixel[1];
  271. total_b += pixel[2];
  272. total_a += pixel[3];
  273. amt++;
  274. }
  275. }
  276. // Logger::log("%d / %d = %d\n",total_r, amt, (total_r/amt));
  277. blurImage[((x+(y*width))*pixelSize)] = (total_r/amt);
  278. blurImage[((x+(y*width))*pixelSize)+1] = (total_g / amt);
  279. blurImage[((x+(y*width))*pixelSize)+2] = (total_b / amt);
  280. blurImage[((x+(y*width))*pixelSize)+3] = (total_a / amt);
  281. }
  282. }
  283. free(imageData);
  284. imageData = (char*)blurImage;
  285. // free(imageData32);
  286. }
  287. void Image::fastBlur(int blurSize) {
  288. fastBlurHor(blurSize);
  289. fastBlurVert(blurSize);
  290. /*
  291. unsigned char *blurImage = (unsigned char*)malloc(width*height*4);
  292. int total_r;
  293. int total_g;
  294. int total_b;
  295. int total_a;
  296. unsigned int *imageData32 = (unsigned int*)imageData;
  297. unsigned char *pixel;
  298. int amt;
  299. for (int y = 0; y < height; y++) {
  300. for (int x = 0; x < width; x++) {
  301. total_r = 0;
  302. total_g = 0;
  303. total_b = 0;
  304. total_a = 0;
  305. amt = 0;
  306. for (int ky = -blurSize; ky <= blurSize; ky++) {
  307. for (int kx = -blurSize; kx <= blurSize ; kx++) {
  308. if(x+kx+((y+ky)*width) > 0 && x+kx+((y+ky)*width) < width*height) {
  309. pixel = (unsigned char*)&(imageData32[(x+kx)+((y+ky)*width)]);
  310. total_r += pixel[0];
  311. total_g += pixel[1];
  312. total_b += pixel[2];
  313. total_a += pixel[3];
  314. amt++;
  315. }
  316. }
  317. }
  318. // Logger::log("%d / %d = %d\n",total_r, amt, (total_r/amt));
  319. blurImage[((x+(y*width))*4)] = (total_r/amt);
  320. blurImage[((x+(y*width))*4)+1] = (total_g / amt);
  321. blurImage[((x+(y*width))*4)+2] = (total_b / amt);
  322. blurImage[((x+(y*width))*4)+3] = (total_a / amt);
  323. }
  324. }
  325. imageData = (char*)blurImage;
  326. // free(imageData32);
  327. */
  328. }
  329. void Image::swap(int *v1, int *v2) {
  330. int tv = *v1;
  331. *v1 = *v2;
  332. *v2 = tv;
  333. }
  334. void Image::line(int x0, int y0, int x1, int y1, Color col) {
  335. bool steep = abs(y1 - y0) > abs(x1 - x0);
  336. if(steep) {
  337. swap(&x0, &y0);
  338. swap(&x1, &y1);
  339. }
  340. if(x0 > x1) {
  341. swap(&x0, &x1);
  342. swap(&y0, &y1);
  343. }
  344. int deltax = x1 - x0;
  345. int deltay = abs(y1 - y0);
  346. Number error = 0;
  347. Number deltaerr = ((Number)deltay) / ((Number)deltax);
  348. int ystep;
  349. int y = y0;
  350. if(y0 < y1)
  351. ystep = 1;
  352. else
  353. ystep = -1;
  354. for(int x=x0; x < x1;x++) {
  355. if(steep) {
  356. setPixel(y,x,col);
  357. } else {
  358. setPixel(x,y,col);
  359. }
  360. error = error + ((Number)deltaerr);
  361. if(error >= 0.5) {
  362. y = y + ystep;
  363. error = error - 1.0;
  364. }
  365. }
  366. }
  367. void Image::fill(Number r, Number g, Number b, Number a) {
  368. Color *color = new Color(r,g,b,a);
  369. unsigned int val = color->getUint();
  370. unsigned int *imageData32 = (unsigned int*) imageData;
  371. for(int i=0; i< width*height; i++) {
  372. imageData32[i] = val;
  373. }
  374. }
  375. bool Image::loadImage(String fileName) {
  376. return loadPNG(fileName);
  377. }
  378. bool Image::loadPNG(String fileName) {
  379. OSFILE *infile;
  380. png_structp png_ptr;
  381. png_infop info_ptr;
  382. char *image_data;
  383. char sig[8];
  384. int bit_depth;
  385. int color_type;
  386. unsigned long width;
  387. unsigned long height;
  388. unsigned int rowbytes;
  389. image_data = NULL;
  390. int i;
  391. png_bytepp row_pointers = NULL;
  392. infile = OSBasics::open(fileName.c_str(), "rb");
  393. if (!infile) {
  394. Logger::log("Error opening png file\n");
  395. return false;
  396. }
  397. OSBasics::read(sig, 1, 8, infile);
  398. if (!png_check_sig((unsigned char *) sig, 8)) {
  399. Logger::log("Error reading png signature\n");
  400. OSBasics::close(infile);
  401. return false;
  402. }
  403. png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
  404. if (!png_ptr) {
  405. Logger::log("Error creating png struct\n");
  406. OSBasics::close(infile);
  407. return false; /* out of memory */
  408. }
  409. info_ptr = png_create_info_struct(png_ptr);
  410. if (!info_ptr) {
  411. Logger::log("Error creating info struct\n");
  412. png_destroy_read_struct(&png_ptr, (png_infopp) NULL, (png_infopp) NULL);
  413. OSBasics::close(infile);
  414. return false; /* out of memory */
  415. }
  416. if (setjmp(png_jmpbuf(png_ptr))) {
  417. Logger::log("Error setting jump thingie\n");
  418. png_destroy_read_struct(&png_ptr, &info_ptr, NULL);
  419. OSBasics::close(infile);
  420. return false;
  421. }
  422. //png_init_io(png_ptr, infile);
  423. png_set_read_fn(png_ptr, (png_voidp)infile, user_read_data);
  424. png_set_sig_bytes(png_ptr, 8);
  425. png_read_info(png_ptr, info_ptr);
  426. png_get_IHDR(png_ptr, info_ptr, &width, &height, &bit_depth,
  427. &color_type, NULL, NULL, NULL);
  428. this->width = width;
  429. this->height = height;
  430. /* Set up some transforms. */
  431. if (color_type & PNG_COLOR_MASK_ALPHA) {
  432. // png_set_strip_alpha(png_ptr);
  433. }
  434. if (bit_depth > 8) {
  435. png_set_strip_16(png_ptr);
  436. }
  437. if (color_type == PNG_COLOR_TYPE_GRAY ||
  438. color_type == PNG_COLOR_TYPE_GRAY_ALPHA) {
  439. png_set_gray_to_rgb(png_ptr);
  440. }
  441. if (color_type == PNG_COLOR_TYPE_PALETTE) {
  442. png_set_palette_to_rgb(png_ptr);
  443. }
  444. if (color_type == PNG_COLOR_TYPE_RGB || color_type == PNG_COLOR_TYPE_RGB_ALPHA) {
  445. png_set_add_alpha(png_ptr, 0xffffff, PNG_FILLER_AFTER);
  446. }
  447. /* Update the png info struct.*/
  448. png_read_update_info(png_ptr, info_ptr);
  449. /* Rowsize in bytes. */
  450. rowbytes = png_get_rowbytes(png_ptr, info_ptr);
  451. /* Allocate the image_data buffer. */
  452. if ((image_data = (char *) malloc(rowbytes * height))==NULL) {
  453. Logger::log("Error allocating image memory\n");
  454. png_destroy_read_struct(&png_ptr, &info_ptr, NULL);
  455. return false;
  456. }
  457. if ((row_pointers = (png_bytepp)malloc(height*sizeof(png_bytep))) == NULL) {
  458. Logger::log("Error allocating image memory\n");
  459. png_destroy_read_struct(&png_ptr, &info_ptr, NULL);
  460. free(image_data);
  461. image_data = NULL;
  462. return false;
  463. }
  464. for (i = 0; i < height; ++i)
  465. row_pointers[height - 1 - i] = (png_byte*)image_data + i*rowbytes;
  466. png_read_image(png_ptr, row_pointers);
  467. free(row_pointers);
  468. png_destroy_read_struct(&png_ptr, &info_ptr, NULL);
  469. OSBasics::close(infile);
  470. imageData = image_data;
  471. return true;
  472. }