image.cpp 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898
  1. /* SLiM - Simple Login Manager
  2. Copyright (C) 2004-06 Simone Rota <sip@varlock.com>
  3. Copyright (C) 2004-06 Johannes Winkelmann <jw@tks6.net>
  4. Copyright (C) 2012 Nobuhiro Iwamatsu <iwamatsu@nigauri.org>
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.
  9. The following code has been adapted and extended from
  10. xplanet 1.0.1, Copyright (C) 2002-04 Hari Nair <hari@alumni.caltech.edu>
  11. */
  12. #include <cctype>
  13. #include <cmath>
  14. #include <cstdio>
  15. #include <cstdlib>
  16. #include <cstring>
  17. #include <iostream>
  18. using namespace std;
  19. #include "image.h"
  20. extern "C" {
  21. #include <jpeglib.h>
  22. #include <png.h>
  23. }
  24. Image::Image() : width(0), height(0), area(0),
  25. rgb_data(NULL), png_alpha(NULL), quality_(80) {}
  26. Image::Image(const int w, const int h, const unsigned char *rgb, const unsigned char *alpha) :
  27. width(w), height(h), area(w*h), quality_(80) {
  28. width = w;
  29. height = h;
  30. area = w * h;
  31. rgb_data = (unsigned char *) malloc(3 * area);
  32. memcpy(rgb_data, rgb, 3 * area);
  33. if (alpha == NULL) {
  34. png_alpha = NULL;
  35. } else {
  36. png_alpha = (unsigned char *) malloc(area);
  37. memcpy(png_alpha, alpha, area);
  38. }
  39. }
  40. Image::~Image() {
  41. free(rgb_data);
  42. free(png_alpha);
  43. }
  44. bool
  45. Image::Read(const char *filename) {
  46. char buf[4];
  47. unsigned char *ubuf = (unsigned char *) buf;
  48. int success = 0;
  49. FILE *file;
  50. file = fopen(filename, "rb");
  51. if (file == NULL)
  52. return(false);
  53. /* see what kind of file we have */
  54. fread(buf, 1, 4, file);
  55. fclose(file);
  56. if ((ubuf[0] == 0x89) && !strncmp("PNG", buf+1, 3))
  57. success = readPng(filename, &width, &height, &rgb_data, &png_alpha);
  58. else if ((ubuf[0] == 0xff) && (ubuf[1] == 0xd8))
  59. success = readJpeg(filename, &width, &height, &rgb_data);
  60. else {
  61. fprintf(stderr, "Unknown image format\n");
  62. success = 0;
  63. }
  64. return(success == 1);
  65. }
  66. void
  67. Image::Reduce(const int factor) {
  68. if (factor < 1)
  69. return;
  70. int scale = 1;
  71. for (int i = 0; i < factor; i++)
  72. scale *= 2;
  73. double scale2 = scale*scale;
  74. int w = width / scale;
  75. int h = height / scale;
  76. int new_area = w * h;
  77. unsigned char *new_rgb = (unsigned char *) malloc(3 * new_area);
  78. memset(new_rgb, 0, 3 * new_area);
  79. unsigned char *new_alpha = NULL;
  80. if (png_alpha != NULL) {
  81. new_alpha = (unsigned char *) malloc(new_area);
  82. memset(new_alpha, 0, new_area);
  83. }
  84. int ipos = 0;
  85. for (int j = 0; j < height; j++) {
  86. int js = j / scale;
  87. for (int i = 0; i < width; i++) {
  88. int is = i/scale;
  89. for (int k = 0; k < 3; k++)
  90. new_rgb[3*(js * w + is) + k] += static_cast<unsigned char> ((rgb_data[3*ipos + k] + 0.5) / scale2);
  91. if (png_alpha != NULL)
  92. new_alpha[js * w + is] += static_cast<unsigned char> (png_alpha[ipos]/scale2);
  93. ipos++;
  94. }
  95. }
  96. free(rgb_data);
  97. free(png_alpha);
  98. rgb_data = new_rgb;
  99. png_alpha = new_alpha;
  100. width = w;
  101. height = h;
  102. area = w * h;
  103. }
  104. void
  105. Image::Resize(const int w, const int h) {
  106. if (width==w && height==h){
  107. return;
  108. }
  109. int new_area = w * h;
  110. unsigned char *new_rgb = (unsigned char *) malloc(3 * new_area);
  111. unsigned char *new_alpha = NULL;
  112. if (png_alpha != NULL)
  113. new_alpha = (unsigned char *) malloc(new_area);
  114. const double scale_x = ((double) w) / width;
  115. const double scale_y = ((double) h) / height;
  116. int ipos = 0;
  117. for (int j = 0; j < h; j++) {
  118. const double y = j / scale_y;
  119. for (int i = 0; i < w; i++) {
  120. const double x = i / scale_x;
  121. if (new_alpha == NULL)
  122. getPixel(x, y, new_rgb + 3*ipos);
  123. else
  124. getPixel(x, y, new_rgb + 3*ipos, new_alpha + ipos);
  125. ipos++;
  126. }
  127. }
  128. free(rgb_data);
  129. free(png_alpha);
  130. rgb_data = new_rgb;
  131. png_alpha = new_alpha;
  132. width = w;
  133. height = h;
  134. area = w * h;
  135. }
  136. /* Find the color of the desired point using bilinear interpolation. */
  137. /* Assume the array indices refer to the denter of the pixel, so each */
  138. /* pixel has corners at (i - 0.5, j - 0.5) and (i + 0.5, j + 0.5) */
  139. void
  140. Image::getPixel(double x, double y, unsigned char *pixel) {
  141. getPixel(x, y, pixel, NULL);
  142. }
  143. void
  144. Image::getPixel(double x, double y, unsigned char *pixel, unsigned char *alpha) {
  145. if (x < -0.5)
  146. x = -0.5;
  147. if (x >= width - 0.5)
  148. x = width - 0.5;
  149. if (y < -0.5)
  150. y = -0.5;
  151. if (y >= height - 0.5)
  152. y = height - 0.5;
  153. int ix0 = (int) (floor(x));
  154. int ix1 = ix0 + 1;
  155. if (ix0 < 0)
  156. ix0 = width - 1;
  157. if (ix1 >= width)
  158. ix1 = 0;
  159. int iy0 = (int) (floor(y));
  160. int iy1 = iy0 + 1;
  161. if (iy0 < 0)
  162. iy0 = 0;
  163. if (iy1 >= height)
  164. iy1 = height - 1;
  165. const double t = x - floor(x);
  166. const double u = 1 - (y - floor(y));
  167. double weight[4];
  168. weight[1] = t * u;
  169. weight[0] = u - weight[1];
  170. weight[2] = 1 - t - u + weight[1];
  171. weight[3] = t - weight[1];
  172. unsigned char *pixels[4];
  173. pixels[0] = rgb_data + 3 * (iy0 * width + ix0);
  174. pixels[1] = rgb_data + 3 * (iy0 * width + ix1);
  175. pixels[2] = rgb_data + 3 * (iy1 * width + ix0);
  176. pixels[3] = rgb_data + 3 * (iy1 * width + ix1);
  177. memset(pixel, 0, 3);
  178. for (int i = 0; i < 4; i++) {
  179. for (int j = 0; j < 3; j++)
  180. pixel[j] += (unsigned char) (weight[i] * pixels[i][j]);
  181. }
  182. if (alpha != NULL) {
  183. unsigned char pixels[4];
  184. pixels[0] = png_alpha[iy0 * width + ix0];
  185. pixels[1] = png_alpha[iy0 * width + ix1];
  186. pixels[2] = png_alpha[iy0 * width + ix0];
  187. pixels[3] = png_alpha[iy1 * width + ix1];
  188. for (int i = 0; i < 4; i++)
  189. *alpha = (unsigned char) (weight[i] * pixels[i]);
  190. }
  191. }
  192. /* Merge the image with a background, taking care of the
  193. * image Alpha transparency. (background alpha is ignored).
  194. * The images is merged on position (x, y) on the
  195. * background, the background must contain the image.
  196. */
  197. void Image::Merge(Image* background, const int x, const int y) {
  198. if (x + width > background->Width()|| y + height > background->Height())
  199. return;
  200. if (background->Width()*background->Height() != width*height)
  201. background->Crop(x, y, width, height);
  202. double tmp;
  203. unsigned char *new_rgb = (unsigned char *) malloc(3 * width * height);
  204. memset(new_rgb, 0, 3 * width * height);
  205. const unsigned char *bg_rgb = background->getRGBData();
  206. int ipos = 0;
  207. if (png_alpha != NULL){
  208. for (int j = 0; j < height; j++) {
  209. for (int i = 0; i < width; i++) {
  210. for (int k = 0; k < 3; k++) {
  211. tmp = rgb_data[3*ipos + k]*png_alpha[ipos]/255.0
  212. + bg_rgb[3*ipos + k]*(1-png_alpha[ipos]/255.0);
  213. new_rgb[3*ipos + k] = static_cast<unsigned char> (tmp);
  214. }
  215. ipos++;
  216. }
  217. }
  218. } else {
  219. for (int j = 0; j < height; j++) {
  220. for (int i = 0; i < width; i++) {
  221. for (int k = 0; k < 3; k++) {
  222. tmp = rgb_data[3*ipos + k];
  223. new_rgb[3*ipos + k] = static_cast<unsigned char> (tmp);
  224. }
  225. ipos++;
  226. }
  227. }
  228. }
  229. free(rgb_data);
  230. free(png_alpha);
  231. rgb_data = new_rgb;
  232. png_alpha = NULL;
  233. }
  234. /* Tile the image growing its size to the minimum entire
  235. * multiple of w * h.
  236. * The new dimensions should be > of the current ones.
  237. * Note that this flattens image (alpha removed)
  238. */
  239. void Image::Tile(const int w, const int h) {
  240. if (w < width || h < height)
  241. return;
  242. int nx = w / width;
  243. if (w % width > 0)
  244. nx++;
  245. int ny = h / height;
  246. if (h % height > 0)
  247. ny++;
  248. int newwidth = nx*width;
  249. int newheight=ny*height;
  250. unsigned char *new_rgb = (unsigned char *) malloc(3 * newwidth * newheight);
  251. memset(new_rgb, 0, 3 * width * height * nx * ny);
  252. int ipos = 0;
  253. int opos = 0;
  254. for (int r = 0; r < ny; r++) {
  255. for (int c = 0; c < nx; c++) {
  256. for (int j = 0; j < height; j++) {
  257. for (int i = 0; i < width; i++) {
  258. opos = j*width + i;
  259. ipos = r*width*height*nx + j*newwidth + c*width +i;
  260. for (int k = 0; k < 3; k++) {
  261. new_rgb[3*ipos + k] = static_cast<unsigned char> (rgb_data[3*opos + k]);
  262. }
  263. }
  264. }
  265. }
  266. }
  267. free(rgb_data);
  268. free(png_alpha);
  269. rgb_data = new_rgb;
  270. png_alpha = NULL;
  271. width = newwidth;
  272. height = newheight;
  273. area = width * height;
  274. Crop(0,0,w,h);
  275. }
  276. /* Crop the image
  277. */
  278. void Image::Crop(const int x, const int y, const int w, const int h) {
  279. if (x+w > width || y+h > height) {
  280. return;
  281. }
  282. int x2 = x + w;
  283. int y2 = y + h;
  284. unsigned char *new_rgb = (unsigned char *) malloc(3 * w * h);
  285. memset(new_rgb, 0, 3 * w * h);
  286. unsigned char *new_alpha = NULL;
  287. if (png_alpha != NULL) {
  288. new_alpha = (unsigned char *) malloc(w * h);
  289. memset(new_alpha, 0, w * h);
  290. }
  291. int ipos = 0;
  292. int opos = 0;
  293. for (int j = 0; j < height; j++) {
  294. for (int i = 0; i < width; i++) {
  295. if (j>=y && i>=x && j<y2 && i<x2) {
  296. for (int k = 0; k < 3; k++) {
  297. new_rgb[3*ipos + k] = static_cast<unsigned char> (rgb_data[3*opos + k]);
  298. }
  299. if (png_alpha != NULL)
  300. new_alpha[ipos] = static_cast<unsigned char> (png_alpha[opos]);
  301. ipos++;
  302. }
  303. opos++;
  304. }
  305. }
  306. free(rgb_data);
  307. free(png_alpha);
  308. rgb_data = new_rgb;
  309. if (png_alpha != NULL)
  310. png_alpha = new_alpha;
  311. width = w;
  312. height = h;
  313. area = w * h;
  314. }
  315. /* Center the image in a rectangle of given width and height.
  316. * Fills the remaining space (if any) with the hex color
  317. */
  318. void Image::Center(const int w, const int h, const char *hex) {
  319. unsigned long packed_rgb;
  320. sscanf(hex, "%lx", &packed_rgb);
  321. unsigned long r = packed_rgb>>16;
  322. unsigned long g = packed_rgb>>8 & 0xff;
  323. unsigned long b = packed_rgb & 0xff;
  324. unsigned char *new_rgb = (unsigned char *) malloc(3 * w * h);
  325. memset(new_rgb, 0, 3 * w * h);
  326. int x = (w - width) / 2;
  327. int y = (h - height) / 2;
  328. if (x<0) {
  329. Crop((width - w)/2,0,w,height);
  330. x = 0;
  331. }
  332. if (y<0) {
  333. Crop(0,(height - h)/2,width,h);
  334. y = 0;
  335. }
  336. int x2 = x + width;
  337. int y2 = y + height;
  338. int ipos = 0;
  339. int opos = 0;
  340. double tmp;
  341. area = w * h;
  342. for (int i = 0; i < area; i++) {
  343. new_rgb[3*i] = r;
  344. new_rgb[3*i+1] = g;
  345. new_rgb[3*i+2] = b;
  346. }
  347. if (png_alpha != NULL) {
  348. for (int j = 0; j < h; j++) {
  349. for (int i = 0; i < w; i++) {
  350. if (j>=y && i>=x && j<y2 && i<x2) {
  351. ipos = j*w + i;
  352. for (int k = 0; k < 3; k++) {
  353. tmp = rgb_data[3*opos + k]*png_alpha[opos]/255.0
  354. + new_rgb[k]*(1-png_alpha[opos]/255.0);
  355. new_rgb[3*ipos + k] = static_cast<unsigned char> (tmp);
  356. }
  357. opos++;
  358. }
  359. }
  360. }
  361. } else {
  362. for (int j = 0; j < h; j++) {
  363. for (int i = 0; i < w; i++) {
  364. if (j>=y && i>=x && j<y2 && i<x2) {
  365. ipos = j*w + i;
  366. for (int k = 0; k < 3; k++) {
  367. tmp = rgb_data[3*opos + k];
  368. new_rgb[3*ipos + k] = static_cast<unsigned char> (tmp);
  369. }
  370. opos++;
  371. }
  372. }
  373. }
  374. }
  375. free(rgb_data);
  376. free(png_alpha);
  377. rgb_data = new_rgb;
  378. png_alpha = NULL;
  379. width = w;
  380. height = h;
  381. }
  382. /* Fill the image with the given color and adjust its dimensions
  383. * to passed values.
  384. */
  385. void Image::Plain(const int w, const int h, const char *hex) {
  386. unsigned long packed_rgb;
  387. sscanf(hex, "%lx", &packed_rgb);
  388. unsigned long r = packed_rgb>>16;
  389. unsigned long g = packed_rgb>>8 & 0xff;
  390. unsigned long b = packed_rgb & 0xff;
  391. unsigned char *new_rgb = (unsigned char *) malloc(3 * w * h);
  392. memset(new_rgb, 0, 3 * w * h);
  393. area = w * h;
  394. for (int i = 0; i < area; i++) {
  395. new_rgb[3*i] = r;
  396. new_rgb[3*i+1] = g;
  397. new_rgb[3*i+2] = b;
  398. }
  399. free(rgb_data);
  400. free(png_alpha);
  401. rgb_data = new_rgb;
  402. png_alpha = NULL;
  403. width = w;
  404. height = h;
  405. }
  406. void
  407. Image::computeShift(unsigned long mask,
  408. unsigned char &left_shift,
  409. unsigned char &right_shift) {
  410. left_shift = 0;
  411. right_shift = 8;
  412. if (mask != 0) {
  413. while ((mask & 0x01) == 0) {
  414. left_shift++;
  415. mask >>= 1;
  416. }
  417. while ((mask & 0x01) == 1) {
  418. right_shift--;
  419. mask >>= 1;
  420. }
  421. }
  422. }
  423. Pixmap
  424. Image::createPixmap(Display* dpy, int scr, Window win) {
  425. int i, j; /* loop variables */
  426. const int depth = DefaultDepth(dpy, scr);
  427. Visual *visual = DefaultVisual(dpy, scr);
  428. Colormap colormap = DefaultColormap(dpy, scr);
  429. Pixmap tmp = XCreatePixmap(dpy, win, width, height,
  430. depth);
  431. char *pixmap_data = NULL;
  432. switch (depth) {
  433. case 32:
  434. case 24:
  435. pixmap_data = new char[4 * width * height];
  436. break;
  437. case 16:
  438. case 15:
  439. pixmap_data = new char[2 * width * height];
  440. break;
  441. case 8:
  442. pixmap_data = new char[width * height];
  443. break;
  444. default:
  445. break;
  446. }
  447. XImage *ximage = XCreateImage(dpy, visual, depth, ZPixmap, 0,
  448. pixmap_data, width, height,
  449. 8, 0);
  450. int entries;
  451. XVisualInfo v_template;
  452. v_template.visualid = XVisualIDFromVisual(visual);
  453. XVisualInfo *visual_info = XGetVisualInfo(dpy, VisualIDMask,
  454. &v_template, &entries);
  455. unsigned long ipos = 0;
  456. switch (visual_info->c_class) {
  457. case PseudoColor: {
  458. XColor xc;
  459. xc.flags = DoRed | DoGreen | DoBlue;
  460. int num_colors = 256;
  461. XColor *colors = new XColor[num_colors];
  462. for (i = 0; i < num_colors; i++)
  463. colors[i].pixel = (unsigned long) i;
  464. XQueryColors(dpy, colormap, colors, num_colors);
  465. int *closest_color = new int[num_colors];
  466. for (i = 0; i < num_colors; i++) {
  467. xc.red = (i & 0xe0) << 8; /* highest 3 bits */
  468. xc.green = (i & 0x1c) << 11; /* middle 3 bits */
  469. xc.blue = (i & 0x03) << 14; /* lowest 2 bits */
  470. /* find the closest color in the colormap */
  471. double distance, distance_squared, min_distance = 0;
  472. for (int ii = 0; ii < num_colors; ii++) {
  473. distance = colors[ii].red - xc.red;
  474. distance_squared = distance * distance;
  475. distance = colors[ii].green - xc.green;
  476. distance_squared += distance * distance;
  477. distance = colors[ii].blue - xc.blue;
  478. distance_squared += distance * distance;
  479. if ((ii == 0) || (distance_squared <= min_distance)) {
  480. min_distance = distance_squared;
  481. closest_color[i] = ii;
  482. }
  483. }
  484. }
  485. for (j = 0; j < height; j++) {
  486. for (i = 0; i < width; i++) {
  487. xc.red = (unsigned short) (rgb_data[ipos++] & 0xe0);
  488. xc.green = (unsigned short) (rgb_data[ipos++] & 0xe0);
  489. xc.blue = (unsigned short) (rgb_data[ipos++] & 0xc0);
  490. xc.pixel = xc.red | (xc.green >> 3) | (xc.blue >> 6);
  491. XPutPixel(ximage, i, j,
  492. colors[closest_color[xc.pixel]].pixel);
  493. }
  494. }
  495. delete [] colors;
  496. delete [] closest_color;
  497. }
  498. break;
  499. case TrueColor: {
  500. unsigned char red_left_shift;
  501. unsigned char red_right_shift;
  502. unsigned char green_left_shift;
  503. unsigned char green_right_shift;
  504. unsigned char blue_left_shift;
  505. unsigned char blue_right_shift;
  506. computeShift(visual_info->red_mask, red_left_shift,
  507. red_right_shift);
  508. computeShift(visual_info->green_mask, green_left_shift,
  509. green_right_shift);
  510. computeShift(visual_info->blue_mask, blue_left_shift,
  511. blue_right_shift);
  512. unsigned long pixel;
  513. unsigned long red, green, blue;
  514. for (j = 0; j < height; j++) {
  515. for (i = 0; i < width; i++) {
  516. red = (unsigned long)
  517. rgb_data[ipos++] >> red_right_shift;
  518. green = (unsigned long)
  519. rgb_data[ipos++] >> green_right_shift;
  520. blue = (unsigned long)
  521. rgb_data[ipos++] >> blue_right_shift;
  522. pixel = (((red << red_left_shift) & visual_info->red_mask)
  523. | ((green << green_left_shift)
  524. & visual_info->green_mask)
  525. | ((blue << blue_left_shift)
  526. & visual_info->blue_mask));
  527. XPutPixel(ximage, i, j, pixel);
  528. }
  529. }
  530. }
  531. break;
  532. default: {
  533. logStream << "Login.app: could not load image" << endl;
  534. return(tmp);
  535. }
  536. }
  537. GC gc = XCreateGC(dpy, win, 0, NULL);
  538. XPutImage(dpy, tmp, gc, ximage, 0, 0, 0, 0, width, height);
  539. XFreeGC(dpy, gc);
  540. XFree(visual_info);
  541. delete [] pixmap_data;
  542. /* Set ximage data to NULL since pixmap data was deallocated above */
  543. ximage->data = NULL;
  544. XDestroyImage(ximage);
  545. return(tmp);
  546. }
  547. int
  548. Image::readJpeg(const char *filename, int *width, int *height,
  549. unsigned char **rgb)
  550. {
  551. int ret = 0;
  552. struct jpeg_decompress_struct cinfo;
  553. struct jpeg_error_mgr jerr;
  554. unsigned char *ptr = NULL;
  555. FILE *infile = fopen(filename, "rb");
  556. if (infile == NULL) {
  557. logStream << APPNAME << "Cannot fopen file: " << filename << endl;
  558. return ret;
  559. }
  560. cinfo.err = jpeg_std_error(&jerr);
  561. jpeg_create_decompress(&cinfo);
  562. jpeg_stdio_src(&cinfo, infile);
  563. jpeg_read_header(&cinfo, TRUE);
  564. jpeg_start_decompress(&cinfo);
  565. /* Prevent against integer overflow */
  566. if(cinfo.output_width >= MAX_DIMENSION
  567. || cinfo.output_height >= MAX_DIMENSION)
  568. {
  569. logStream << APPNAME << "Unreasonable dimension found in file: "
  570. << filename << endl;
  571. goto close_file;
  572. }
  573. *width = cinfo.output_width;
  574. *height = cinfo.output_height;
  575. rgb[0] = (unsigned char*)
  576. malloc(3 * cinfo.output_width * cinfo.output_height);
  577. if (rgb[0] == NULL) {
  578. logStream << APPNAME << ": Can't allocate memory for JPEG file."
  579. << endl;
  580. goto close_file;
  581. }
  582. if (cinfo.output_components == 3) {
  583. ptr = rgb[0];
  584. while (cinfo.output_scanline < cinfo.output_height) {
  585. jpeg_read_scanlines(&cinfo, &ptr, 1);
  586. ptr += 3 * cinfo.output_width;
  587. }
  588. } else if (cinfo.output_components == 1) {
  589. ptr = (unsigned char*) malloc(cinfo.output_width);
  590. if (ptr == NULL) {
  591. logStream << APPNAME << ": Can't allocate memory for JPEG file."
  592. << endl;
  593. goto rgb_free;
  594. }
  595. unsigned int ipos = 0;
  596. while (cinfo.output_scanline < cinfo.output_height) {
  597. jpeg_read_scanlines(&cinfo, &ptr, 1);
  598. for (unsigned int i = 0; i < cinfo.output_width; i++) {
  599. memset(rgb[0] + ipos, ptr[i], 3);
  600. ipos += 3;
  601. }
  602. }
  603. free(ptr);
  604. }
  605. jpeg_finish_decompress(&cinfo);
  606. ret = 1;
  607. goto close_file;
  608. rgb_free:
  609. free(rgb[0]);
  610. close_file:
  611. jpeg_destroy_decompress(&cinfo);
  612. fclose(infile);
  613. return(ret);
  614. }
  615. int
  616. Image::readPng(const char *filename, int *width, int *height,
  617. unsigned char **rgb, unsigned char **alpha)
  618. {
  619. int ret = 0;
  620. png_structp png_ptr;
  621. png_infop info_ptr;
  622. png_bytepp row_pointers;
  623. unsigned char *ptr = NULL;
  624. png_uint_32 w, h;
  625. int bit_depth, color_type, interlace_type;
  626. int i;
  627. FILE *infile = fopen(filename, "rb");
  628. if (infile == NULL) {
  629. logStream << APPNAME << "Can not fopen file: " << filename << endl;
  630. return ret;
  631. }
  632. png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING,
  633. (png_voidp) NULL,
  634. (png_error_ptr) NULL,
  635. (png_error_ptr) NULL);
  636. if (!png_ptr) {
  637. goto file_close;
  638. }
  639. info_ptr = png_create_info_struct(png_ptr);
  640. if (!info_ptr) {
  641. png_destroy_read_struct(&png_ptr, (png_infopp) NULL,
  642. (png_infopp) NULL);
  643. }
  644. #if PNG_LIBPNG_VER_MAJOR >= 1 && PNG_LIBPNG_VER_MINOR >= 4
  645. if (setjmp(png_jmpbuf((png_ptr)))) {
  646. #else
  647. if (setjmp(png_ptr->jmpbuf)) {
  648. #endif
  649. goto png_destroy;
  650. }
  651. png_init_io(png_ptr, infile);
  652. png_read_info(png_ptr, info_ptr);
  653. png_get_IHDR(png_ptr, info_ptr, &w, &h, &bit_depth, &color_type,
  654. &interlace_type, (int *) NULL, (int *) NULL);
  655. /* Prevent against integer overflow */
  656. if(w >= MAX_DIMENSION || h >= MAX_DIMENSION) {
  657. logStream << APPNAME << "Unreasonable dimension found in file: "
  658. << filename << endl;
  659. goto png_destroy;
  660. }
  661. *width = (int) w;
  662. *height = (int) h;
  663. if (color_type == PNG_COLOR_TYPE_RGB_ALPHA
  664. || color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
  665. {
  666. alpha[0] = (unsigned char *) malloc(*width * *height);
  667. if (alpha[0] == NULL) {
  668. logStream << APPNAME
  669. << ": Can't allocate memory for alpha channel in PNG file."
  670. << endl;
  671. goto png_destroy;
  672. }
  673. }
  674. /* Change a paletted/grayscale image to RGB */
  675. if (color_type == PNG_COLOR_TYPE_PALETTE && bit_depth <= 8)
  676. {
  677. png_set_expand(png_ptr);
  678. }
  679. /* Change a grayscale image to RGB */
  680. if (color_type == PNG_COLOR_TYPE_GRAY
  681. || color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
  682. {
  683. png_set_gray_to_rgb(png_ptr);
  684. }
  685. /* If the PNG file has 16 bits per channel, strip them down to 8 */
  686. if (bit_depth == 16) {
  687. png_set_strip_16(png_ptr);
  688. }
  689. /* use 1 byte per pixel */
  690. png_set_packing(png_ptr);
  691. row_pointers = (png_byte **) malloc(*height * sizeof(png_bytep));
  692. if (row_pointers == NULL) {
  693. logStream << APPNAME << ": Can't allocate memory for PNG file." << endl;
  694. goto png_destroy;
  695. }
  696. for (i = 0; i < *height; i++) {
  697. row_pointers[i] = (png_byte*) malloc(4 * *width);
  698. if (row_pointers == NULL) {
  699. logStream << APPNAME << ": Can't allocate memory for PNG file."
  700. << endl;
  701. goto rows_free;
  702. }
  703. }
  704. png_read_image(png_ptr, row_pointers);
  705. rgb[0] = (unsigned char *) malloc(3 * (*width) * (*height));
  706. if (rgb[0] == NULL) {
  707. logStream << APPNAME << ": Can't allocate memory for PNG file." << endl;
  708. goto rows_free;
  709. }
  710. if (alpha[0] == NULL) {
  711. ptr = rgb[0];
  712. for (i = 0; i < *height; i++) {
  713. memcpy(ptr, row_pointers[i], 3 * (*width));
  714. ptr += 3 * (*width);
  715. }
  716. } else {
  717. ptr = rgb[0];
  718. for (i = 0; i < *height; i++) {
  719. unsigned int ipos = 0;
  720. for (int j = 0; j < *width; j++) {
  721. *ptr++ = row_pointers[i][ipos++];
  722. *ptr++ = row_pointers[i][ipos++];
  723. *ptr++ = row_pointers[i][ipos++];
  724. alpha[0][i * (*width) + j] = row_pointers[i][ipos++];
  725. }
  726. }
  727. }
  728. ret = 1; /* data reading is OK */
  729. rows_free:
  730. for (i = 0; i < *height; i++) {
  731. if (row_pointers[i] != NULL ) {
  732. free(row_pointers[i]);
  733. }
  734. }
  735. free(row_pointers);
  736. png_destroy:
  737. png_destroy_read_struct(&png_ptr, &info_ptr, (png_infopp) NULL);
  738. file_close:
  739. fclose(infile);
  740. return(ret);
  741. }