x.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. /*
  2. * ALGO : PROJ. : Volume
  3. * RESEARCH : File : x.c
  4. * : Date : 20100531.0725UTC
  5. * : Email : mail@algoresearch.net
  6. */
  7. #include "x.h"
  8. Display *dpy;
  9. GC gc, gc_out;
  10. Colormap xcolors;
  11. Atom delWin;
  12. XEvent e;
  13. int d_depth;
  14. Window d;
  15. Pixmap pixmap;
  16. XSetWindowAttributes window_attributes;
  17. XColor color;
  18. unsigned long gray[0x100], colors[0x100], spectrum[0x400];
  19. int create_x(int scr_w, int scr_h, char title[64])
  20. {
  21. int s_number;
  22. if (!(dpy=XOpenDisplay(NULL)))
  23. {
  24. perror("XOpenDisplay");
  25. return -1;
  26. }
  27. s_number = DefaultScreen (dpy);
  28. d_depth = DefaultDepth (dpy, s_number);
  29. window_attributes.border_pixel = BlackPixel (dpy, s_number);
  30. window_attributes.background_pixel = BlackPixel (dpy, s_number);
  31. window_attributes.override_redirect = 0;
  32. d = XCreateWindow
  33. (
  34. dpy,
  35. DefaultRootWindow (dpy),
  36. 0, 0,
  37. scr_w, scr_h,
  38. 0,
  39. d_depth,
  40. InputOutput,
  41. CopyFromParent,
  42. CWBackPixel | CWBorderPixel,
  43. &window_attributes
  44. );
  45. xcolors = DefaultColormap(dpy, s_number);
  46. XSetWindowColormap(dpy, d, xcolors);
  47. gc = XCreateGC (dpy, d, 0, NULL);
  48. gc_out = XCreateGC (dpy, d, 0, NULL);
  49. XSetStandardProperties(dpy, d, title, title, None, 0, 0, None);
  50. XSelectInput
  51. (
  52. dpy, d,
  53. ExposureMask |
  54. KeyPressMask |
  55. ButtonPressMask | ButtonReleaseMask |
  56. Button1MotionMask |Button2MotionMask |
  57. StructureNotifyMask
  58. );
  59. delWin = XInternAtom(dpy, "WM_DELETE_WINDOW", False);
  60. XSetWMProtocols(dpy, d, &delWin, 1);
  61. XMapWindow (dpy, d);
  62. while(!(e.type == MapNotify)) XNextEvent(dpy, &e);
  63. pixmap = XCreatePixmap(dpy, d, scr_w, scr_h, d_depth);
  64. XSetFillStyle (dpy, gc_out, FillTiled);
  65. XSetTile (dpy, gc_out, pixmap);
  66. return 0;
  67. }
  68. void set_graymap()
  69. {
  70. int i;
  71. for(i=0;i<0x100;i++)
  72. {
  73. color.red = 0x100 * i;
  74. color.green = 0x100 * i;
  75. color.blue = 0x100 * i;
  76. XAllocColor(dpy, xcolors, &color);
  77. gray[i] = color.pixel;
  78. }
  79. }
  80. void set_colormap()
  81. {
  82. int i;
  83. for(i=0;i<0x100;i++)
  84. {
  85. color.red = 0x100 * i;
  86. color.green = 0x100 * abs(128-i);
  87. color.blue = 0x100 * (0xff-i);
  88. XAllocColor(dpy, xcolors, &color);
  89. colors[i] = color.pixel;
  90. }
  91. }
  92. void create_image_16(IMG *img, int w, int h)
  93. {
  94. img->image_16 = (unsigned short int *) malloc (w * h * 2);
  95. img->screen = XCreateImage (dpy, CopyFromParent, d_depth, ZPixmap, 0,
  96. (char*)img->image_16, w, h, 16, w * 2);
  97. memset(img->image_16, 0x00, w * h * 2);
  98. }
  99. void create_image_32(IMG *img, int w, int h)
  100. {
  101. img->image_32 = (long *) malloc (w * h * 4);
  102. img->screen = XCreateImage (dpy, CopyFromParent, d_depth, ZPixmap, 0,
  103. (char*)img->image_32, w, h, 32, w * 4);
  104. memset(img->image_32, 0x00, w * h * 4);
  105. }
  106. int create_image(IMG *img, int w, int h)
  107. {
  108. img->w = w; img->h = h;
  109. if (d_depth == 8 || d_depth == 16)
  110. {
  111. create_image_16 (img, w, h);
  112. img->size = w * h * 2;
  113. }
  114. else if(d_depth == 24 || d_depth == 32)
  115. {
  116. create_image_32 (img, w, h);
  117. img->size = w * h * 4;
  118. }
  119. else
  120. {
  121. fprintf (stderr, "This is not a supported depth. %d\n",d_depth);
  122. return -1;
  123. }
  124. return 0;
  125. }
  126. void clear_image(IMG *img)
  127. {
  128. if (d_depth == 8 || d_depth == 16)
  129. memset(img->image_16, 0x00, img->size);
  130. else if(d_depth == 24 || d_depth == 32)
  131. memset(img->image_32, 0x00, img->size);
  132. }
  133. void burn_image(IMG *img)
  134. {
  135. int i, n = img->w * img->h;
  136. unsigned long c;
  137. unsigned int r, g, b;
  138. for (i=0; i<n; i++)
  139. {
  140. c = img->image_32[i];
  141. r = c >> 16;
  142. g = (c & 0x00ff00) >> 8;
  143. b = c & 0x0000ff;
  144. if (r > 0) r--;
  145. if (g > 0) g--;
  146. if (b > 0) b--;
  147. img->image_32[i] = (r << 16) | (g << 8) | b;
  148. }
  149. }
  150. void clear_buffer(int x, int y, int w, int h)
  151. {
  152. XSetForeground(dpy, gc, 0);
  153. XFillRectangle(dpy, pixmap, gc, x, y, w, h);
  154. }
  155. void draw_buffer(int x, int y, int w, int h)
  156. {
  157. XFillRectangle (dpy, d, gc_out, x, y, w, h);
  158. XFlush(dpy);
  159. }
  160. void draw_image(IMG *img, int x, int y, int w, int h)
  161. {
  162. XPutImage(dpy, pixmap, gc, img->screen, 0, 0, x, y, w, h);
  163. }
  164. void put_pixel(IMG *img, int x, int y, unsigned long c)
  165. {
  166. if (x<0 || y<0) return;
  167. if (x>=img->w || y>=img->h) return;
  168. XPutPixel(img->screen, x, y, c);
  169. }
  170. void put_apixel(IMG *img, int x, int y, unsigned long fg, float alpha)
  171. {
  172. unsigned int fR, fG, fB, bR, bG, bB;
  173. unsigned long bg, c;
  174. float v = 1 - alpha;
  175. if (x<0 || y<0) return;
  176. if (x>=img->w || y>=img->h) return;
  177. bg = XGetPixel(img->screen, x, y);
  178. bR = bg >> 16;
  179. bG = (bg & 0x00ff00) >> 8;
  180. bB = bg & 0x0000ff;
  181. fR = fg >> 16;
  182. fG = (fg & 0x00ff00) >> 8;
  183. fB = fg & 0x0000ff;
  184. c = ((unsigned int)(fR * alpha + bR * v) << 16) |
  185. ((unsigned int)(fG * alpha + bG * v) << 8) |
  186. (unsigned int)(fB * alpha + bB * v);
  187. XPutPixel(img->screen, x, y, c);
  188. }
  189. void draw_line (IMG *img, int x0, int y0, int x1, int y1, unsigned long c)
  190. {
  191. int dy = y1 - y0;
  192. int dx = x1 - x0;
  193. int stepx, stepy;
  194. int fraction;
  195. if (dy < 0) { dy = -dy; stepy = -1; } else stepy = 1;
  196. if (dx < 0) { dx = -dx; stepx = -1; } else stepx = 1;
  197. dy <<= 1;
  198. dx <<= 1;
  199. put_pixel(img, x0, y0, c);
  200. if (dx > dy)
  201. {
  202. fraction = dy - (dx >> 1);
  203. while (x0 != x1)
  204. {
  205. if (fraction >= 0)
  206. {
  207. y0 += stepy;
  208. fraction -= dx;
  209. }
  210. x0 += stepx;
  211. fraction += dy;
  212. put_pixel(img, x0, y0, c);
  213. }
  214. }
  215. else
  216. {
  217. fraction = dx - (dy >> 1);
  218. while (y0 != y1)
  219. {
  220. if (fraction >= 0)
  221. {
  222. x0 += stepx;
  223. fraction -= dy;
  224. }
  225. y0 += stepy;
  226. fraction += dx;
  227. put_pixel(img, x0, y0, c);
  228. }
  229. }
  230. }
  231. void set_spectrum()
  232. {
  233. int i;
  234. for(i=0; i<0x100; i++)
  235. {
  236. color.red = 0xff00;
  237. color.green = 0x100 * i;
  238. color.blue = 0;
  239. XAllocColor (dpy, xcolors, &color);
  240. spectrum[i] = color.pixel;
  241. color.red = 0x100 * (0xff-i);
  242. color.green = 0xff00;
  243. color.blue = 0;
  244. XAllocColor (dpy, xcolors, &color);
  245. spectrum[i+0x100] = color.pixel;
  246. color.red = 0;
  247. color.green = 0xff00;
  248. color.blue = 0x100 * i;
  249. XAllocColor (dpy, xcolors, &color);
  250. spectrum[i+0x200] = color.pixel;
  251. color.red = 0;
  252. color.green = 0x100 * (0xff-i);
  253. color.blue = 0xff00;
  254. XAllocColor (dpy, xcolors, &color);
  255. spectrum[i+0x300] = color.pixel;
  256. }
  257. }