image.cpp 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952
  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. /* Merge the image with a background, taking care of the
  235. * image Alpha transparency. (background alpha is ignored).
  236. * The images is merged on position (x, y) on the
  237. * background, the background must contain the image.
  238. */
  239. #define IMG_POS_RGB(p, x) (3 * p + x)
  240. void Image::Merge_non_crop(Image* background, const int x, const int y)
  241. {
  242. int bg_w = background->Width();
  243. int bg_h = background->Height();
  244. if (x + width > bg_w || y + height > bg_h)
  245. return;
  246. double tmp;
  247. unsigned char *new_rgb = (unsigned char *)malloc(3 * bg_w * bg_h);
  248. memset(new_rgb, 0, 3 * bg_w * bg_h);
  249. const unsigned char *bg_rgb = background->getRGBData();
  250. int pnl_pos = 0;
  251. int bg_pos = 0;
  252. int pnl_w_end = x + width;
  253. int pnl_h_end = y + height;
  254. memcpy(new_rgb, bg_rgb, 3 * bg_w * bg_h);
  255. for (int j = 0; j < bg_h; j++) {
  256. for (int i = 0; i < bg_w; i++) {
  257. if (j >= y && i >= x && j < pnl_h_end && i < pnl_w_end ) {
  258. for (int k = 0; k < 3; k++) {
  259. if (png_alpha != NULL)
  260. tmp = rgb_data[IMG_POS_RGB(pnl_pos, k)]
  261. * png_alpha[pnl_pos]/255.0
  262. + bg_rgb[IMG_POS_RGB(bg_pos, k)]
  263. * (1 - png_alpha[pnl_pos]/255.0);
  264. else
  265. tmp = rgb_data[IMG_POS_RGB(pnl_pos, k)];
  266. new_rgb[IMG_POS_RGB(bg_pos, k)] = static_cast<unsigned char>(tmp);
  267. }
  268. pnl_pos++;
  269. }
  270. bg_pos++;
  271. }
  272. }
  273. width = bg_w;
  274. height = bg_h;
  275. free(rgb_data);
  276. free(png_alpha);
  277. rgb_data = new_rgb;
  278. png_alpha = NULL;
  279. }
  280. /* Tile the image growing its size to the minimum entire
  281. * multiple of w * h.
  282. * The new dimensions should be > of the current ones.
  283. * Note that this flattens image (alpha removed)
  284. */
  285. void Image::Tile(const int w, const int h) {
  286. if (w < width || h < height)
  287. return;
  288. int nx = w / width;
  289. if (w % width > 0)
  290. nx++;
  291. int ny = h / height;
  292. if (h % height > 0)
  293. ny++;
  294. int newwidth = nx*width;
  295. int newheight=ny*height;
  296. unsigned char *new_rgb = (unsigned char *) malloc(3 * newwidth * newheight);
  297. memset(new_rgb, 0, 3 * width * height * nx * ny);
  298. int ipos = 0;
  299. int opos = 0;
  300. for (int r = 0; r < ny; r++) {
  301. for (int c = 0; c < nx; c++) {
  302. for (int j = 0; j < height; j++) {
  303. for (int i = 0; i < width; i++) {
  304. opos = j*width + i;
  305. ipos = r*width*height*nx + j*newwidth + c*width +i;
  306. for (int k = 0; k < 3; k++) {
  307. new_rgb[3*ipos + k] = static_cast<unsigned char> (rgb_data[3*opos + k]);
  308. }
  309. }
  310. }
  311. }
  312. }
  313. free(rgb_data);
  314. free(png_alpha);
  315. rgb_data = new_rgb;
  316. png_alpha = NULL;
  317. width = newwidth;
  318. height = newheight;
  319. area = width * height;
  320. Crop(0,0,w,h);
  321. }
  322. /* Crop the image
  323. */
  324. void Image::Crop(const int x, const int y, const int w, const int h) {
  325. if (x+w > width || y+h > height) {
  326. return;
  327. }
  328. int x2 = x + w;
  329. int y2 = y + h;
  330. unsigned char *new_rgb = (unsigned char *) malloc(3 * w * h);
  331. memset(new_rgb, 0, 3 * w * h);
  332. unsigned char *new_alpha = NULL;
  333. if (png_alpha != NULL) {
  334. new_alpha = (unsigned char *) malloc(w * h);
  335. memset(new_alpha, 0, w * h);
  336. }
  337. int ipos = 0;
  338. int opos = 0;
  339. for (int j = 0; j < height; j++) {
  340. for (int i = 0; i < width; i++) {
  341. if (j>=y && i>=x && j<y2 && i<x2) {
  342. for (int k = 0; k < 3; k++) {
  343. new_rgb[3*ipos + k] = static_cast<unsigned char> (rgb_data[3*opos + k]);
  344. }
  345. if (png_alpha != NULL)
  346. new_alpha[ipos] = static_cast<unsigned char> (png_alpha[opos]);
  347. ipos++;
  348. }
  349. opos++;
  350. }
  351. }
  352. free(rgb_data);
  353. free(png_alpha);
  354. rgb_data = new_rgb;
  355. if (png_alpha != NULL)
  356. png_alpha = new_alpha;
  357. width = w;
  358. height = h;
  359. area = w * h;
  360. }
  361. /* Center the image in a rectangle of given width and height.
  362. * Fills the remaining space (if any) with the hex color
  363. */
  364. void Image::Center(const int w, const int h, const char *hex) {
  365. unsigned long packed_rgb;
  366. sscanf(hex, "%lx", &packed_rgb);
  367. unsigned long r = packed_rgb>>16;
  368. unsigned long g = packed_rgb>>8 & 0xff;
  369. unsigned long b = packed_rgb & 0xff;
  370. unsigned char *new_rgb = (unsigned char *) malloc(3 * w * h);
  371. memset(new_rgb, 0, 3 * w * h);
  372. int x = (w - width) / 2;
  373. int y = (h - height) / 2;
  374. if (x<0) {
  375. Crop((width - w)/2,0,w,height);
  376. x = 0;
  377. }
  378. if (y<0) {
  379. Crop(0,(height - h)/2,width,h);
  380. y = 0;
  381. }
  382. int x2 = x + width;
  383. int y2 = y + height;
  384. int ipos = 0;
  385. int opos = 0;
  386. double tmp;
  387. area = w * h;
  388. for (int i = 0; i < area; i++) {
  389. new_rgb[3*i] = r;
  390. new_rgb[3*i+1] = g;
  391. new_rgb[3*i+2] = b;
  392. }
  393. if (png_alpha != NULL) {
  394. for (int j = 0; j < h; j++) {
  395. for (int i = 0; i < w; i++) {
  396. if (j>=y && i>=x && j<y2 && i<x2) {
  397. ipos = j*w + i;
  398. for (int k = 0; k < 3; k++) {
  399. tmp = rgb_data[3*opos + k]*png_alpha[opos]/255.0
  400. + new_rgb[k]*(1-png_alpha[opos]/255.0);
  401. new_rgb[3*ipos + k] = static_cast<unsigned char> (tmp);
  402. }
  403. opos++;
  404. }
  405. }
  406. }
  407. } else {
  408. for (int j = 0; j < h; j++) {
  409. for (int i = 0; i < w; i++) {
  410. if (j>=y && i>=x && j<y2 && i<x2) {
  411. ipos = j*w + i;
  412. for (int k = 0; k < 3; k++) {
  413. tmp = rgb_data[3*opos + k];
  414. new_rgb[3*ipos + k] = static_cast<unsigned char> (tmp);
  415. }
  416. opos++;
  417. }
  418. }
  419. }
  420. }
  421. free(rgb_data);
  422. free(png_alpha);
  423. rgb_data = new_rgb;
  424. png_alpha = NULL;
  425. width = w;
  426. height = h;
  427. }
  428. /* Fill the image with the given color and adjust its dimensions
  429. * to passed values.
  430. */
  431. void Image::Plain(const int w, const int h, const char *hex) {
  432. unsigned long packed_rgb;
  433. sscanf(hex, "%lx", &packed_rgb);
  434. unsigned long r = packed_rgb>>16;
  435. unsigned long g = packed_rgb>>8 & 0xff;
  436. unsigned long b = packed_rgb & 0xff;
  437. unsigned char *new_rgb = (unsigned char *) malloc(3 * w * h);
  438. memset(new_rgb, 0, 3 * w * h);
  439. area = w * h;
  440. for (int i = 0; i < area; i++) {
  441. new_rgb[3*i] = r;
  442. new_rgb[3*i+1] = g;
  443. new_rgb[3*i+2] = b;
  444. }
  445. free(rgb_data);
  446. free(png_alpha);
  447. rgb_data = new_rgb;
  448. png_alpha = NULL;
  449. width = w;
  450. height = h;
  451. }
  452. void
  453. Image::computeShift(unsigned long mask,
  454. unsigned char &left_shift,
  455. unsigned char &right_shift) {
  456. left_shift = 0;
  457. right_shift = 8;
  458. if (mask != 0) {
  459. while ((mask & 0x01) == 0) {
  460. left_shift++;
  461. mask >>= 1;
  462. }
  463. while ((mask & 0x01) == 1) {
  464. right_shift--;
  465. mask >>= 1;
  466. }
  467. }
  468. }
  469. Pixmap
  470. Image::createPixmap(Display* dpy, int scr, Window win) {
  471. int i, j; /* loop variables */
  472. const int depth = DefaultDepth(dpy, scr);
  473. Visual *visual = DefaultVisual(dpy, scr);
  474. Colormap colormap = DefaultColormap(dpy, scr);
  475. Pixmap tmp = XCreatePixmap(dpy, win, width, height,
  476. depth);
  477. char *pixmap_data = NULL;
  478. switch (depth) {
  479. case 32:
  480. case 24:
  481. pixmap_data = new char[4 * width * height];
  482. break;
  483. case 16:
  484. case 15:
  485. pixmap_data = new char[2 * width * height];
  486. break;
  487. case 8:
  488. pixmap_data = new char[width * height];
  489. break;
  490. default:
  491. break;
  492. }
  493. XImage *ximage = XCreateImage(dpy, visual, depth, ZPixmap, 0,
  494. pixmap_data, width, height,
  495. 8, 0);
  496. int entries;
  497. XVisualInfo v_template;
  498. v_template.visualid = XVisualIDFromVisual(visual);
  499. XVisualInfo *visual_info = XGetVisualInfo(dpy, VisualIDMask,
  500. &v_template, &entries);
  501. unsigned long ipos = 0;
  502. switch (visual_info->c_class) {
  503. case PseudoColor: {
  504. XColor xc;
  505. xc.flags = DoRed | DoGreen | DoBlue;
  506. int num_colors = 256;
  507. XColor *colors = new XColor[num_colors];
  508. for (i = 0; i < num_colors; i++)
  509. colors[i].pixel = (unsigned long) i;
  510. XQueryColors(dpy, colormap, colors, num_colors);
  511. int *closest_color = new int[num_colors];
  512. for (i = 0; i < num_colors; i++) {
  513. xc.red = (i & 0xe0) << 8; /* highest 3 bits */
  514. xc.green = (i & 0x1c) << 11; /* middle 3 bits */
  515. xc.blue = (i & 0x03) << 14; /* lowest 2 bits */
  516. /* find the closest color in the colormap */
  517. double distance, distance_squared, min_distance = 0;
  518. for (int ii = 0; ii < num_colors; ii++) {
  519. distance = colors[ii].red - xc.red;
  520. distance_squared = distance * distance;
  521. distance = colors[ii].green - xc.green;
  522. distance_squared += distance * distance;
  523. distance = colors[ii].blue - xc.blue;
  524. distance_squared += distance * distance;
  525. if ((ii == 0) || (distance_squared <= min_distance)) {
  526. min_distance = distance_squared;
  527. closest_color[i] = ii;
  528. }
  529. }
  530. }
  531. for (j = 0; j < height; j++) {
  532. for (i = 0; i < width; i++) {
  533. xc.red = (unsigned short) (rgb_data[ipos++] & 0xe0);
  534. xc.green = (unsigned short) (rgb_data[ipos++] & 0xe0);
  535. xc.blue = (unsigned short) (rgb_data[ipos++] & 0xc0);
  536. xc.pixel = xc.red | (xc.green >> 3) | (xc.blue >> 6);
  537. XPutPixel(ximage, i, j,
  538. colors[closest_color[xc.pixel]].pixel);
  539. }
  540. }
  541. delete [] colors;
  542. delete [] closest_color;
  543. }
  544. break;
  545. case TrueColor: {
  546. unsigned char red_left_shift;
  547. unsigned char red_right_shift;
  548. unsigned char green_left_shift;
  549. unsigned char green_right_shift;
  550. unsigned char blue_left_shift;
  551. unsigned char blue_right_shift;
  552. computeShift(visual_info->red_mask, red_left_shift,
  553. red_right_shift);
  554. computeShift(visual_info->green_mask, green_left_shift,
  555. green_right_shift);
  556. computeShift(visual_info->blue_mask, blue_left_shift,
  557. blue_right_shift);
  558. unsigned long pixel;
  559. unsigned long red, green, blue;
  560. for (j = 0; j < height; j++) {
  561. for (i = 0; i < width; i++) {
  562. red = (unsigned long)
  563. rgb_data[ipos++] >> red_right_shift;
  564. green = (unsigned long)
  565. rgb_data[ipos++] >> green_right_shift;
  566. blue = (unsigned long)
  567. rgb_data[ipos++] >> blue_right_shift;
  568. pixel = (((red << red_left_shift) & visual_info->red_mask)
  569. | ((green << green_left_shift)
  570. & visual_info->green_mask)
  571. | ((blue << blue_left_shift)
  572. & visual_info->blue_mask));
  573. XPutPixel(ximage, i, j, pixel);
  574. }
  575. }
  576. }
  577. break;
  578. default: {
  579. logStream << "Login.app: could not load image" << endl;
  580. return(tmp);
  581. }
  582. }
  583. GC gc = XCreateGC(dpy, win, 0, NULL);
  584. XPutImage(dpy, tmp, gc, ximage, 0, 0, 0, 0, width, height);
  585. XFreeGC(dpy, gc);
  586. XFree(visual_info);
  587. delete [] pixmap_data;
  588. /* Set ximage data to NULL since pixmap data was deallocated above */
  589. ximage->data = NULL;
  590. XDestroyImage(ximage);
  591. return(tmp);
  592. }
  593. int
  594. Image::readJpeg(const char *filename, int *width, int *height,
  595. unsigned char **rgb)
  596. {
  597. int ret = 0;
  598. struct jpeg_decompress_struct cinfo;
  599. struct jpeg_error_mgr jerr;
  600. unsigned char *ptr = NULL;
  601. FILE *infile = fopen(filename, "rb");
  602. if (infile == NULL) {
  603. logStream << APPNAME << "Cannot fopen file: " << filename << endl;
  604. return ret;
  605. }
  606. cinfo.err = jpeg_std_error(&jerr);
  607. jpeg_create_decompress(&cinfo);
  608. jpeg_stdio_src(&cinfo, infile);
  609. jpeg_read_header(&cinfo, TRUE);
  610. jpeg_start_decompress(&cinfo);
  611. /* Prevent against integer overflow */
  612. if(cinfo.output_width >= MAX_DIMENSION
  613. || cinfo.output_height >= MAX_DIMENSION)
  614. {
  615. logStream << APPNAME << "Unreasonable dimension found in file: "
  616. << filename << endl;
  617. goto close_file;
  618. }
  619. *width = cinfo.output_width;
  620. *height = cinfo.output_height;
  621. rgb[0] = (unsigned char*)
  622. malloc(3 * cinfo.output_width * cinfo.output_height);
  623. if (rgb[0] == NULL) {
  624. logStream << APPNAME << ": Can't allocate memory for JPEG file."
  625. << endl;
  626. goto close_file;
  627. }
  628. if (cinfo.output_components == 3) {
  629. ptr = rgb[0];
  630. while (cinfo.output_scanline < cinfo.output_height) {
  631. jpeg_read_scanlines(&cinfo, &ptr, 1);
  632. ptr += 3 * cinfo.output_width;
  633. }
  634. } else if (cinfo.output_components == 1) {
  635. ptr = (unsigned char*) malloc(cinfo.output_width);
  636. if (ptr == NULL) {
  637. logStream << APPNAME << ": Can't allocate memory for JPEG file."
  638. << endl;
  639. goto rgb_free;
  640. }
  641. unsigned int ipos = 0;
  642. while (cinfo.output_scanline < cinfo.output_height) {
  643. jpeg_read_scanlines(&cinfo, &ptr, 1);
  644. for (unsigned int i = 0; i < cinfo.output_width; i++) {
  645. memset(rgb[0] + ipos, ptr[i], 3);
  646. ipos += 3;
  647. }
  648. }
  649. free(ptr);
  650. }
  651. jpeg_finish_decompress(&cinfo);
  652. ret = 1;
  653. goto close_file;
  654. rgb_free:
  655. free(rgb[0]);
  656. close_file:
  657. jpeg_destroy_decompress(&cinfo);
  658. fclose(infile);
  659. return(ret);
  660. }
  661. int
  662. Image::readPng(const char *filename, int *width, int *height,
  663. unsigned char **rgb, unsigned char **alpha)
  664. {
  665. int ret = 0;
  666. png_structp png_ptr;
  667. png_infop info_ptr;
  668. png_bytepp row_pointers;
  669. unsigned char *ptr = NULL;
  670. png_uint_32 w, h;
  671. int bit_depth, color_type, interlace_type;
  672. int i;
  673. FILE *infile = fopen(filename, "rb");
  674. if (infile == NULL) {
  675. logStream << APPNAME << "Can not fopen file: " << filename << endl;
  676. return ret;
  677. }
  678. png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING,
  679. (png_voidp) NULL,
  680. (png_error_ptr) NULL,
  681. (png_error_ptr) NULL);
  682. if (!png_ptr) {
  683. goto file_close;
  684. }
  685. info_ptr = png_create_info_struct(png_ptr);
  686. if (!info_ptr) {
  687. png_destroy_read_struct(&png_ptr, (png_infopp) NULL,
  688. (png_infopp) NULL);
  689. }
  690. #if PNG_LIBPNG_VER_MAJOR >= 1 && PNG_LIBPNG_VER_MINOR >= 4
  691. if (setjmp(png_jmpbuf((png_ptr)))) {
  692. #else
  693. if (setjmp(png_ptr->jmpbuf)) {
  694. #endif
  695. goto png_destroy;
  696. }
  697. png_init_io(png_ptr, infile);
  698. png_read_info(png_ptr, info_ptr);
  699. png_get_IHDR(png_ptr, info_ptr, &w, &h, &bit_depth, &color_type,
  700. &interlace_type, (int *) NULL, (int *) NULL);
  701. /* Prevent against integer overflow */
  702. if(w >= MAX_DIMENSION || h >= MAX_DIMENSION) {
  703. logStream << APPNAME << "Unreasonable dimension found in file: "
  704. << filename << endl;
  705. goto png_destroy;
  706. }
  707. *width = (int) w;
  708. *height = (int) h;
  709. if (color_type == PNG_COLOR_TYPE_RGB_ALPHA
  710. || color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
  711. {
  712. alpha[0] = (unsigned char *) malloc(*width * *height);
  713. if (alpha[0] == NULL) {
  714. logStream << APPNAME
  715. << ": Can't allocate memory for alpha channel in PNG file."
  716. << endl;
  717. goto png_destroy;
  718. }
  719. }
  720. /* Change a paletted/grayscale image to RGB */
  721. if (color_type == PNG_COLOR_TYPE_PALETTE && bit_depth <= 8)
  722. {
  723. png_set_expand(png_ptr);
  724. }
  725. /* Change a grayscale image to RGB */
  726. if (color_type == PNG_COLOR_TYPE_GRAY
  727. || color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
  728. {
  729. png_set_gray_to_rgb(png_ptr);
  730. }
  731. /* If the PNG file has 16 bits per channel, strip them down to 8 */
  732. if (bit_depth == 16) {
  733. png_set_strip_16(png_ptr);
  734. }
  735. /* use 1 byte per pixel */
  736. png_set_packing(png_ptr);
  737. row_pointers = (png_byte **) malloc(*height * sizeof(png_bytep));
  738. if (row_pointers == NULL) {
  739. logStream << APPNAME << ": Can't allocate memory for PNG file." << endl;
  740. goto png_destroy;
  741. }
  742. for (i = 0; i < *height; i++) {
  743. row_pointers[i] = (png_byte*) malloc(4 * *width);
  744. if (row_pointers == NULL) {
  745. logStream << APPNAME << ": Can't allocate memory for PNG file."
  746. << endl;
  747. goto rows_free;
  748. }
  749. }
  750. png_read_image(png_ptr, row_pointers);
  751. rgb[0] = (unsigned char *) malloc(3 * (*width) * (*height));
  752. if (rgb[0] == NULL) {
  753. logStream << APPNAME << ": Can't allocate memory for PNG file." << endl;
  754. goto rows_free;
  755. }
  756. if (alpha[0] == NULL) {
  757. ptr = rgb[0];
  758. for (i = 0; i < *height; i++) {
  759. memcpy(ptr, row_pointers[i], 3 * (*width));
  760. ptr += 3 * (*width);
  761. }
  762. } else {
  763. ptr = rgb[0];
  764. for (i = 0; i < *height; i++) {
  765. unsigned int ipos = 0;
  766. for (int j = 0; j < *width; j++) {
  767. *ptr++ = row_pointers[i][ipos++];
  768. *ptr++ = row_pointers[i][ipos++];
  769. *ptr++ = row_pointers[i][ipos++];
  770. alpha[0][i * (*width) + j] = row_pointers[i][ipos++];
  771. }
  772. }
  773. }
  774. ret = 1; /* data reading is OK */
  775. rows_free:
  776. for (i = 0; i < *height; i++) {
  777. if (row_pointers[i] != NULL ) {
  778. free(row_pointers[i]);
  779. }
  780. }
  781. free(row_pointers);
  782. png_destroy:
  783. png_destroy_read_struct(&png_ptr, &info_ptr, (png_infopp) NULL);
  784. file_close:
  785. fclose(infile);
  786. return(ret);
  787. }