image.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664
  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. 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 2 of the License, or
  7. (at your option) any later version.
  8. The following code has been adapted and extended from
  9. xplanet 1.0.1, Copyright (C) 2002-04 Hari Nair <hari@alumni.caltech.edu>
  10. */
  11. #include <cctype>
  12. #include <cmath>
  13. #include <cstdio>
  14. #include <cstdlib>
  15. #include <cstring>
  16. #include <iostream>
  17. using namespace std;
  18. #include "image.h"
  19. extern "C" {
  20. int
  21. read_png(const char *filename, int *width, int *height, unsigned char **rgb,
  22. unsigned char **alpha);
  23. int
  24. read_jpeg(const char *filename, int *width, int *height, unsigned char **rgb);
  25. }
  26. Image::Image() : width(0), height(0), area(0),
  27. rgb_data(NULL), png_alpha(NULL), quality_(80) {}
  28. Image::Image(const int w, const int h, const unsigned char *rgb, const unsigned char *alpha) :
  29. width(w), height(h), area(w*h), quality_(80) {
  30. width = w;
  31. height = h;
  32. area = w * h;
  33. rgb_data = (unsigned char *) malloc(3 * area);
  34. memcpy(rgb_data, rgb, 3 * area);
  35. if (alpha == NULL) {
  36. png_alpha = NULL;
  37. } else {
  38. png_alpha = (unsigned char *) malloc(area);
  39. memcpy(png_alpha, alpha, area);
  40. }
  41. }
  42. Image::~Image() {
  43. free(rgb_data);
  44. free(png_alpha);
  45. }
  46. bool
  47. Image::Read(const char *filename) {
  48. char buf[4];
  49. unsigned char *ubuf = (unsigned char *) buf;
  50. int success = 0;
  51. FILE *file;
  52. file = fopen(filename, "rb");
  53. if (file == NULL)
  54. return(false);
  55. /* see what kind of file we have */
  56. fread(buf, 1, 4, file);
  57. fclose(file);
  58. if ((ubuf[0] == 0x89) && !strncmp("PNG", buf+1, 3)) {
  59. success = read_png(filename, &width, &height, &rgb_data, &png_alpha);
  60. }
  61. else if ((ubuf[0] == 0xff) && (ubuf[1] == 0xd8)){
  62. success = read_jpeg(filename, &width, &height, &rgb_data);
  63. } else {
  64. fprintf(stderr, "Unknown image format\n");
  65. success = 0;
  66. }
  67. return(success == 1);
  68. }
  69. void
  70. Image::Reduce(const int factor) {
  71. if (factor < 1)
  72. return;
  73. int scale = 1;
  74. for (int i = 0; i < factor; i++)
  75. scale *= 2;
  76. double scale2 = scale*scale;
  77. int w = width / scale;
  78. int h = height / scale;
  79. int new_area = w * h;
  80. unsigned char *new_rgb = (unsigned char *) malloc(3 * new_area);
  81. memset(new_rgb, 0, 3 * new_area);
  82. unsigned char *new_alpha = NULL;
  83. if (png_alpha != NULL) {
  84. new_alpha = (unsigned char *) malloc(new_area);
  85. memset(new_alpha, 0, new_area);
  86. }
  87. int ipos = 0;
  88. for (int j = 0; j < height; j++) {
  89. int js = j / scale;
  90. for (int i = 0; i < width; i++) {
  91. int is = i/scale;
  92. for (int k = 0; k < 3; k++)
  93. new_rgb[3*(js * w + is) + k] += static_cast<unsigned char> ((rgb_data[3*ipos + k] + 0.5) / scale2);
  94. if (png_alpha != NULL)
  95. new_alpha[js * w + is] += static_cast<unsigned char> (png_alpha[ipos]/scale2);
  96. ipos++;
  97. }
  98. }
  99. free(rgb_data);
  100. free(png_alpha);
  101. rgb_data = new_rgb;
  102. png_alpha = new_alpha;
  103. width = w;
  104. height = h;
  105. area = w * h;
  106. }
  107. void
  108. Image::Resize(const int w, const int h) {
  109. if (width==w && height==h){
  110. return;
  111. }
  112. int new_area = w * h;
  113. unsigned char *new_rgb = (unsigned char *) malloc(3 * new_area);
  114. unsigned char *new_alpha = NULL;
  115. if (png_alpha != NULL)
  116. new_alpha = (unsigned char *) malloc(new_area);
  117. const double scale_x = ((double) w) / width;
  118. const double scale_y = ((double) h) / height;
  119. int ipos = 0;
  120. for (int j = 0; j < h; j++) {
  121. const double y = j / scale_y;
  122. for (int i = 0; i < w; i++) {
  123. const double x = i / scale_x;
  124. if (new_alpha == NULL)
  125. getPixel(x, y, new_rgb + 3*ipos);
  126. else
  127. getPixel(x, y, new_rgb + 3*ipos, new_alpha + ipos);
  128. ipos++;
  129. }
  130. }
  131. free(rgb_data);
  132. free(png_alpha);
  133. rgb_data = new_rgb;
  134. png_alpha = new_alpha;
  135. width = w;
  136. height = h;
  137. area = w * h;
  138. }
  139. // Find the color of the desired point using bilinear interpolation.
  140. // Assume the array indices refer to the denter of the pixel, so each
  141. // pixel has corners at (i - 0.5, j - 0.5) and (i + 0.5, j + 0.5)
  142. void
  143. Image::getPixel(double x, double y, unsigned char *pixel) {
  144. getPixel(x, y, pixel, NULL);
  145. }
  146. void
  147. Image::getPixel(double x, double y, unsigned char *pixel, unsigned char *alpha) {
  148. if (x < -0.5)
  149. x = -0.5;
  150. if (x >= width - 0.5)
  151. x = width - 0.5;
  152. if (y < -0.5)
  153. y = -0.5;
  154. if (y >= height - 0.5)
  155. y = height - 0.5;
  156. int ix0 = (int) (floor(x));
  157. int ix1 = ix0 + 1;
  158. if (ix0 < 0)
  159. ix0 = width - 1;
  160. if (ix1 >= width)
  161. ix1 = 0;
  162. int iy0 = (int) (floor(y));
  163. int iy1 = iy0 + 1;
  164. if (iy0 < 0)
  165. iy0 = 0;
  166. if (iy1 >= height)
  167. iy1 = height - 1;
  168. const double t = x - floor(x);
  169. const double u = 1 - (y - floor(y));
  170. double weight[4];
  171. weight[1] = t * u;
  172. weight[0] = u - weight[1];
  173. weight[2] = 1 - t - u + weight[1];
  174. weight[3] = t - weight[1];
  175. unsigned char *pixels[4];
  176. pixels[0] = rgb_data + 3 * (iy0 * width + ix0);
  177. pixels[1] = rgb_data + 3 * (iy0 * width + ix1);
  178. pixels[2] = rgb_data + 3 * (iy1 * width + ix0);
  179. pixels[3] = rgb_data + 3 * (iy1 * width + ix1);
  180. memset(pixel, 0, 3);
  181. for (int i = 0; i < 4; i++) {
  182. for (int j = 0; j < 3; j++)
  183. pixel[j] += (unsigned char) (weight[i] * pixels[i][j]);
  184. }
  185. if (alpha != NULL) {
  186. unsigned char pixels[4];
  187. pixels[0] = png_alpha[iy0 * width + ix0];
  188. pixels[1] = png_alpha[iy0 * width + ix1];
  189. pixels[2] = png_alpha[iy0 * width + ix0];
  190. pixels[3] = png_alpha[iy1 * width + ix1];
  191. for (int i = 0; i < 4; i++)
  192. *alpha = (unsigned char) (weight[i] * pixels[i]);
  193. }
  194. }
  195. /* Merge the image with a background, taking care of the
  196. * image Alpha transparency. (background alpha is ignored).
  197. * The images is merged on position (x, y) on the
  198. * background, the background must contain the image.
  199. */
  200. void Image::Merge(Image* background, const int x, const int y) {
  201. if (x + width > background->Width()|| y + height > background->Height()) {
  202. return;
  203. }
  204. if (background->Width()*background->Height() != width*height)
  205. background->Crop(x, y, width, height);
  206. double tmp;
  207. unsigned char *new_rgb = (unsigned char *) malloc(3 * width * height);
  208. memset(new_rgb, 0, 3 * width * height);
  209. const unsigned char *bg_rgb = background->getRGBData();
  210. int ipos = 0;
  211. if (png_alpha != NULL){
  212. for (int j = 0; j < height; j++) {
  213. for (int i = 0; i < width; i++) {
  214. for (int k = 0; k < 3; k++) {
  215. tmp = rgb_data[3*ipos + k]*png_alpha[ipos]/255.0
  216. + bg_rgb[3*ipos + k]*(1-png_alpha[ipos]/255.0);
  217. new_rgb[3*ipos + k] = static_cast<unsigned char> (tmp);
  218. }
  219. ipos++;
  220. }
  221. }
  222. } else {
  223. for (int j = 0; j < height; j++) {
  224. for (int i = 0; i < width; i++) {
  225. for (int k = 0; k < 3; k++) {
  226. tmp = rgb_data[3*ipos + k];
  227. new_rgb[3*ipos + k] = static_cast<unsigned char> (tmp);
  228. }
  229. ipos++;
  230. }
  231. }
  232. }
  233. free(rgb_data);
  234. free(png_alpha);
  235. rgb_data = new_rgb;
  236. png_alpha = NULL;
  237. }
  238. /* Tile the image growing its size to the minimum entire
  239. * multiple of w * h.
  240. * The new dimensions should be > of the current ones.
  241. * Note that this flattens image (alpha removed)
  242. */
  243. void Image::Tile(const int w, const int h) {
  244. if (w < width || h < height)
  245. return;
  246. int nx = w / width;
  247. if (w % width > 0)
  248. nx++;
  249. int ny = h / height;
  250. if (h % height > 0)
  251. ny++;
  252. int newwidth = nx*width;
  253. int newheight=ny*height;
  254. unsigned char *new_rgb = (unsigned char *) malloc(3 * newwidth * newheight);
  255. memset(new_rgb, 0, 3 * width * height * nx * ny);
  256. int ipos = 0;
  257. int opos = 0;
  258. for (int r = 0; r < ny; r++) {
  259. for (int c = 0; c < nx; c++) {
  260. for (int j = 0; j < height; j++) {
  261. for (int i = 0; i < width; i++) {
  262. opos = j*width + i;
  263. ipos = r*width*height*nx + j*newwidth + c*width +i;
  264. for (int k = 0; k < 3; k++) {
  265. new_rgb[3*ipos + k] = static_cast<unsigned char> (rgb_data[3*opos + k]);
  266. }
  267. }
  268. }
  269. }
  270. }
  271. free(rgb_data);
  272. free(png_alpha);
  273. rgb_data = new_rgb;
  274. png_alpha = NULL;
  275. width = newwidth;
  276. height = newheight;
  277. area = width * height;
  278. Crop(0,0,w,h);
  279. }
  280. /* Crop the image
  281. */
  282. void Image::Crop(const int x, const int y, const int w, const int h) {
  283. if (x+w > width || y+h > height) {
  284. return;
  285. }
  286. int x2 = x + w;
  287. int y2 = y + h;
  288. unsigned char *new_rgb = (unsigned char *) malloc(3 * w * h);
  289. memset(new_rgb, 0, 3 * w * h);
  290. unsigned char *new_alpha = (unsigned char *) malloc(w * h);
  291. memset(new_alpha, 0, w * h);
  292. int ipos = 0;
  293. int opos = 0;
  294. for (int j = 0; j < height; j++) {
  295. for (int i = 0; i < width; i++) {
  296. if (j>=y && i>=x && j<y2 && i<x2) {
  297. for (int k = 0; k < 3; k++) {
  298. new_rgb[3*ipos + k] = static_cast<unsigned char> (rgb_data[3*opos + k]);
  299. }
  300. if (png_alpha != NULL)
  301. new_alpha[ipos] = static_cast<unsigned char> (png_alpha[opos]);
  302. ipos++;
  303. }
  304. opos++;
  305. }
  306. }
  307. free(rgb_data);
  308. free(png_alpha);
  309. rgb_data = new_rgb;
  310. if (png_alpha != NULL)
  311. png_alpha = new_alpha;
  312. width = w;
  313. height = h;
  314. area = w * h;
  315. }
  316. /* Center the image in a rectangle of given width and height.
  317. * Fills the remaining space (if any) with the hex color
  318. */
  319. void Image::Center(const int w, const int h, const char *hex) {
  320. unsigned long packed_rgb;
  321. sscanf(hex, "%x", &packed_rgb);
  322. unsigned long r = packed_rgb>>16;
  323. unsigned long g = packed_rgb>>8 & 0xff;
  324. unsigned long b = packed_rgb & 0xff;
  325. unsigned char *new_rgb = (unsigned char *) malloc(3 * w * h);
  326. memset(new_rgb, 0, 3 * w * h);
  327. int x = (w - width) / 2;
  328. int y = (h - height) / 2;
  329. if (x<0) {
  330. Crop((width - w)/2,0,w,height);
  331. x = 0;
  332. }
  333. if (y<0) {
  334. Crop(0,(height - h)/2,width,h);
  335. y = 0;
  336. }
  337. int x2 = x + width;
  338. int y2 = y + height;
  339. int ipos = 0;
  340. int opos = 0;
  341. double tmp;
  342. area = w * h;
  343. for (int i = 0; i < area; i++) {
  344. new_rgb[3*i] = r;
  345. new_rgb[3*i+1] = g;
  346. new_rgb[3*i+2] = b;
  347. }
  348. if (png_alpha != NULL) {
  349. for (int j = 0; j < h; j++) {
  350. for (int i = 0; i < w; i++) {
  351. if (j>=y && i>=x && j<y2 && i<x2) {
  352. ipos = j*w + i;
  353. for (int k = 0; k < 3; k++) {
  354. tmp = rgb_data[3*opos + k]*png_alpha[opos]/255.0
  355. + new_rgb[k]*(1-png_alpha[opos]/255.0);
  356. new_rgb[3*ipos + k] = static_cast<unsigned char> (tmp);
  357. }
  358. opos++;
  359. }
  360. }
  361. }
  362. } else {
  363. for (int j = 0; j < h; j++) {
  364. for (int i = 0; i < w; i++) {
  365. if (j>=y && i>=x && j<y2 && i<x2) {
  366. ipos = j*w + i;
  367. for (int k = 0; k < 3; k++) {
  368. tmp = rgb_data[3*opos + k];
  369. new_rgb[3*ipos + k] = static_cast<unsigned char> (tmp);
  370. }
  371. opos++;
  372. }
  373. }
  374. }
  375. }
  376. free(rgb_data);
  377. free(png_alpha);
  378. rgb_data = new_rgb;
  379. png_alpha = NULL;
  380. width = w;
  381. height = h;
  382. }
  383. /* Fill the image with the given color and adjust its dimensions
  384. * to passed values.
  385. */
  386. void Image::Plain(const int w, const int h, const char *hex) {
  387. unsigned long packed_rgb;
  388. sscanf(hex, "%x", &packed_rgb);
  389. unsigned long r = packed_rgb>>16;
  390. unsigned long g = packed_rgb>>8 & 0xff;
  391. unsigned long b = packed_rgb & 0xff;
  392. unsigned char *new_rgb = (unsigned char *) malloc(3 * w * h);
  393. memset(new_rgb, 0, 3 * w * h);
  394. area = w * h;
  395. for (int i = 0; i < area; i++) {
  396. new_rgb[3*i] = r;
  397. new_rgb[3*i+1] = g;
  398. new_rgb[3*i+2] = b;
  399. }
  400. free(rgb_data);
  401. free(png_alpha);
  402. rgb_data = new_rgb;
  403. png_alpha = NULL;
  404. width = w;
  405. height = h;
  406. }
  407. void
  408. Image::computeShift(unsigned long mask,
  409. unsigned char &left_shift,
  410. unsigned char &right_shift) {
  411. left_shift = 0;
  412. right_shift = 8;
  413. if (mask != 0) {
  414. while ((mask & 0x01) == 0) {
  415. left_shift++;
  416. mask >>= 1;
  417. }
  418. while ((mask & 0x01) == 1) {
  419. right_shift--;
  420. mask >>= 1;
  421. }
  422. }
  423. }
  424. Pixmap
  425. Image::createPixmap(Display* dpy, int scr, Window win) {
  426. int i, j; // loop variables
  427. const int depth = DefaultDepth(dpy, scr);
  428. Visual *visual = DefaultVisual(dpy, scr);
  429. Colormap colormap = DefaultColormap(dpy, scr);
  430. Pixmap tmp = XCreatePixmap(dpy, win, width, height,
  431. depth);
  432. char *pixmap_data = NULL;
  433. switch (depth) {
  434. case 32:
  435. case 24:
  436. pixmap_data = new char[4 * width * height];
  437. break;
  438. case 16:
  439. case 15:
  440. pixmap_data = new char[2 * width * height];
  441. break;
  442. case 8:
  443. pixmap_data = new char[width * height];
  444. break;
  445. default:
  446. break;
  447. }
  448. XImage *ximage = XCreateImage(dpy, visual, depth, ZPixmap, 0,
  449. pixmap_data, width, height,
  450. 8, 0);
  451. int entries;
  452. XVisualInfo v_template;
  453. v_template.visualid = XVisualIDFromVisual(visual);
  454. XVisualInfo *visual_info = XGetVisualInfo(dpy, VisualIDMask,
  455. &v_template, &entries);
  456. unsigned long ipos = 0;
  457. switch (visual_info->c_class) {
  458. case PseudoColor: {
  459. XColor xc;
  460. xc.flags = DoRed | DoGreen | DoBlue;
  461. int num_colors = 256;
  462. XColor *colors = new XColor[num_colors];
  463. for (i = 0; i < num_colors; i++)
  464. colors[i].pixel = (unsigned long) i;
  465. XQueryColors(dpy, colormap, colors, num_colors);
  466. int *closest_color = new int[num_colors];
  467. for (i = 0; i < num_colors; i++) {
  468. xc.red = (i & 0xe0) << 8; // highest 3 bits
  469. xc.green = (i & 0x1c) << 11; // middle 3 bits
  470. xc.blue = (i & 0x03) << 14; // lowest 2 bits
  471. // find the closest color in the colormap
  472. double distance, distance_squared, min_distance = 0;
  473. for (int ii = 0; ii < num_colors; ii++) {
  474. distance = colors[ii].red - xc.red;
  475. distance_squared = distance * distance;
  476. distance = colors[ii].green - xc.green;
  477. distance_squared += distance * distance;
  478. distance = colors[ii].blue - xc.blue;
  479. distance_squared += distance * distance;
  480. if ((ii == 0) || (distance_squared <= min_distance)) {
  481. min_distance = distance_squared;
  482. closest_color[i] = ii;
  483. }
  484. }
  485. }
  486. for (j = 0; j < height; j++) {
  487. for (i = 0; i < width; i++) {
  488. xc.red = (unsigned short) (rgb_data[ipos++] & 0xe0);
  489. xc.green = (unsigned short) (rgb_data[ipos++] & 0xe0);
  490. xc.blue = (unsigned short) (rgb_data[ipos++] & 0xc0);
  491. xc.pixel = xc.red | (xc.green >> 3) | (xc.blue >> 6);
  492. XPutPixel(ximage, i, j,
  493. colors[closest_color[xc.pixel]].pixel);
  494. }
  495. }
  496. delete [] colors;
  497. delete [] closest_color;
  498. }
  499. break;
  500. case TrueColor: {
  501. unsigned char red_left_shift;
  502. unsigned char red_right_shift;
  503. unsigned char green_left_shift;
  504. unsigned char green_right_shift;
  505. unsigned char blue_left_shift;
  506. unsigned char blue_right_shift;
  507. computeShift(visual_info->red_mask, red_left_shift,
  508. red_right_shift);
  509. computeShift(visual_info->green_mask, green_left_shift,
  510. green_right_shift);
  511. computeShift(visual_info->blue_mask, blue_left_shift,
  512. blue_right_shift);
  513. unsigned long pixel;
  514. unsigned long red, green, blue;
  515. for (j = 0; j < height; j++) {
  516. for (i = 0; i < width; i++) {
  517. red = (unsigned long)
  518. rgb_data[ipos++] >> red_right_shift;
  519. green = (unsigned long)
  520. rgb_data[ipos++] >> green_right_shift;
  521. blue = (unsigned long)
  522. rgb_data[ipos++] >> blue_right_shift;
  523. pixel = (((red << red_left_shift) & visual_info->red_mask)
  524. | ((green << green_left_shift)
  525. & visual_info->green_mask)
  526. | ((blue << blue_left_shift)
  527. & visual_info->blue_mask));
  528. XPutPixel(ximage, i, j, pixel);
  529. }
  530. }
  531. }
  532. break;
  533. default: {
  534. cerr << "Login.app: could not load image" << endl;
  535. return(tmp);
  536. }
  537. }
  538. GC gc = XCreateGC(dpy, win, 0, NULL);
  539. XPutImage(dpy, tmp, gc, ximage, 0, 0, 0, 0, width, height);
  540. XFreeGC(dpy, gc);
  541. XFree(visual_info);
  542. delete [] pixmap_data;
  543. // Set ximage data to NULL since pixmap data was deallocated above
  544. ximage->data = NULL;
  545. XDestroyImage(ximage);
  546. return(tmp);
  547. }