gdevvglb.c 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  1. /* Copyright (C) 1992, 1993, 1994, 1996, 1997, 1998, 1999 Aladdin Enterprises. All rights reserved.
  2. This file is part of AFPL Ghostscript.
  3. AFPL Ghostscript is distributed with NO WARRANTY OF ANY KIND. No author or
  4. distributor accepts any responsibility for the consequences of using it, or
  5. for whether it serves any particular purpose or works at all, unless he or
  6. she says so in writing. Refer to the Aladdin Free Public License (the
  7. "License") for full details.
  8. Every copy of AFPL Ghostscript must include a copy of the License, normally
  9. in a plain ASCII text file named PUBLIC. The License grants you the right
  10. to copy, modify and redistribute AFPL Ghostscript, but only under certain
  11. conditions described in the License. Among other things, the License
  12. requires that the copyright notice and this notice be preserved on all
  13. copies.
  14. */
  15. /*$Id: gdevvglb.c,v 1.3 2000/09/19 19:00:23 lpd Exp $ */
  16. /*
  17. * This is a driver for 386 PCs using vgalib for graphics on the console
  18. * display. Note that this driver only works with 16-color modes.
  19. *
  20. * Written by Sigfrid Lundberg, siglun@euler.teorekol.lu.se.
  21. * Modified by Erik Talvola, talvola@gnu.ai.mit.edu
  22. * Updated 9/28/96 by L. Peter Deutsch, ghost@aladdin.com: allow setting
  23. * the display mode as a device parameter.
  24. * Updated 2/13/97 by ghost@aladdin.com: make the device identify itself
  25. * as a page device.
  26. * Updated 5/2/97 by ghost@aladdin.com: copy_mono computed some parameters
  27. * before doing fit_copy.
  28. * Update 1997-06-28 by ghost@aladdin.com: get_bits wasn't implemented.
  29. */
  30. #include "gx.h"
  31. #include "gserrors.h"
  32. #include "gsparam.h"
  33. #include "gxdevice.h"
  34. #include "gdevpccm.h"
  35. #include <errno.h>
  36. #include <vga.h>
  37. typedef struct gx_device_vgalib {
  38. gx_device_common;
  39. int display_mode;
  40. } gx_device_vgalib;
  41. #define vga_dev ((gx_device_vgalib *)dev)
  42. #define XDPI 60 /* to get a more-or-less square aspect ratio */
  43. #define YDPI 60
  44. #ifndef A4 /*Letter size */
  45. #define YSIZE (20.0 * YDPI / 2.5)
  46. #define XSIZE (8.5 / 11)*YSIZE /* 8.5 x 11 inch page, by default */
  47. #else /* A4 paper */
  48. #define XSIZE 8.27
  49. #define YSIZE 11.69
  50. #endif
  51. private dev_proc_open_device(vgalib_open);
  52. private dev_proc_close_device(vgalib_close);
  53. private dev_proc_map_rgb_color(vgalib_map_rgb_color);
  54. private dev_proc_map_color_rgb(vgalib_map_color_rgb);
  55. private dev_proc_fill_rectangle(vgalib_fill_rectangle);
  56. private dev_proc_tile_rectangle(vgalib_tile_rectangle);
  57. private dev_proc_copy_mono(vgalib_copy_mono);
  58. private dev_proc_copy_color(vgalib_copy_color);
  59. private dev_proc_get_bits(vgalib_get_bits);
  60. private dev_proc_get_params(vgalib_get_params);
  61. private dev_proc_put_params(vgalib_put_params);
  62. const gx_device_vgalib gs_vgalib_device =
  63. {
  64. std_device_std_body(gx_device_vgalib, 0, "vgalib",
  65. 0, 0, 1, 1),
  66. {vgalib_open,
  67. NULL, /* get_initial_matrix */
  68. NULL, /* sync_output */
  69. NULL, /* output_page */
  70. vgalib_close,
  71. vgalib_map_rgb_color,
  72. vgalib_map_color_rgb,
  73. vgalib_fill_rectangle,
  74. vgalib_tile_rectangle,
  75. vgalib_copy_mono,
  76. vgalib_copy_color,
  77. NULL, /* draw_line (obsolete) */
  78. vgalib_get_bits,
  79. vgalib_get_params,
  80. vgalib_put_params,
  81. NULL, /* map_cmyk_color */
  82. NULL, /* get_xfont_procs */
  83. NULL, /* get_xfont_device */
  84. NULL, /* map_rgb_alpha_color */
  85. gx_page_device_get_page_device
  86. },
  87. -1 /* display_mode */
  88. };
  89. private int
  90. vgalib_open(gx_device * dev)
  91. {
  92. int VGAMODE = vga_dev->display_mode;
  93. int width = dev->width, height = dev->height;
  94. if (VGAMODE == -1)
  95. VGAMODE = vga_getdefaultmode();
  96. if (VGAMODE == -1)
  97. vga_setmode(G640x480x16);
  98. else
  99. vga_setmode(VGAMODE);
  100. vga_clear();
  101. if (width == 0)
  102. width = vga_getxdim() + 1;
  103. if (height == 0)
  104. height = vga_getydim() + 1;
  105. /*vgalib provides no facilities for finding out aspect ratios */
  106. if (dev->y_pixels_per_inch == 1) {
  107. dev->y_pixels_per_inch = height / 11.0;
  108. dev->x_pixels_per_inch = dev->y_pixels_per_inch;
  109. }
  110. gx_device_set_width_height(dev, width, height);
  111. /* Find out if the device supports color */
  112. /* (default initialization is monochrome). */
  113. /* We only recognize 16-color devices right now. */
  114. if (vga_getcolors() > 1) {
  115. int index;
  116. static const gx_device_color_info vgalib_16color = dci_pc_4bit;
  117. dev->color_info = vgalib_16color;
  118. for (index = 0; index < 16; ++index) {
  119. gx_color_value rgb[3];
  120. (*dev_proc(dev, map_color_rgb)) (dev, (gx_color_index) index, rgb);
  121. #define cv2pv(cv) ((cv) >> (gx_color_value_bits - 8))
  122. vga_setpalette(index, cv2pv(rgb[0]), cv2pv(rgb[1]), cv2pv(rgb[2]));
  123. #undef cv2pv
  124. }
  125. }
  126. return 0;
  127. }
  128. private int
  129. vgalib_close(gx_device * dev)
  130. {
  131. vga_setmode(TEXT);
  132. return 0;
  133. }
  134. private gx_color_index
  135. vgalib_map_rgb_color(gx_device * dev, gx_color_value red,
  136. gx_color_value green, gx_color_value blue)
  137. {
  138. return pc_4bit_map_rgb_color(dev, red, green, blue);
  139. }
  140. private int
  141. vgalib_map_color_rgb(gx_device * dev, gx_color_index index,
  142. unsigned short rgb[3])
  143. {
  144. return pc_4bit_map_color_rgb(dev, index, rgb);
  145. }
  146. private int
  147. vgalib_tile_rectangle(gx_device * dev, const gx_tile_bitmap * tile,
  148. int x, int y, int w, int h, gx_color_index czero,
  149. gx_color_index cone, int px, int py)
  150. {
  151. if (czero != gx_no_color_index && cone != gx_no_color_index) {
  152. vgalib_fill_rectangle(dev, x, y, w, h, czero);
  153. czero = gx_no_color_index;
  154. }
  155. return gx_default_tile_rectangle(dev, tile, x, y, w, h, czero, cone, px,
  156. py);
  157. }
  158. private int
  159. vgalib_fill_rectangle(gx_device * dev, int x, int y, int w, int h,
  160. gx_color_index color)
  161. {
  162. int i, j;
  163. fit_fill(dev, x, y, w, h);
  164. vga_setcolor((int)color);
  165. if ((w | h) > 3) { /* Draw larger rectangles as lines. */
  166. if (w > h)
  167. for (i = y; i < y + h; ++i)
  168. vga_drawline(x, i, x + w - 1, i);
  169. else
  170. for (j = x; j < x + w; ++j)
  171. vga_drawline(j, y, j, y + h - 1);
  172. } else { /* Draw small rectangles point-by-point. */
  173. for (i = y; i < y + h; i++)
  174. for (j = x; j < x + w; j++)
  175. vga_drawpixel(j, i);
  176. }
  177. return 0;
  178. }
  179. private int
  180. vgalib_copy_mono(gx_device * dev, const byte * base, int sourcex,
  181. int raster, gx_bitmap_id id, int x, int y, int width,
  182. int height, gx_color_index zero, gx_color_index one)
  183. {
  184. const byte *ptr_line;
  185. int left_bit, dest_y, end_x;
  186. int invert = 0;
  187. int color;
  188. fit_copy(dev, base, sourcex, raster, id, x, y, width, height);
  189. ptr_line = base + (sourcex >> 3);
  190. left_bit = 0x80 >> (sourcex & 7);
  191. dest_y = y, end_x = x + width;
  192. if (zero == gx_no_color_index) {
  193. if (one == gx_no_color_index)
  194. return 0;
  195. color = (int)one;
  196. } else {
  197. if (one == gx_no_color_index) {
  198. color = (int)zero;
  199. invert = -1;
  200. } else { /* Pre-clear the rectangle to zero */
  201. vgalib_fill_rectangle(dev, x, y, width, height, zero);
  202. color = (int)one;
  203. }
  204. }
  205. vga_setcolor(color);
  206. while (height--) { /* for each line */
  207. const byte *ptr_source = ptr_line;
  208. register int dest_x = x;
  209. register int bit = left_bit;
  210. while (dest_x < end_x) { /* for each bit in the line */
  211. if ((*ptr_source ^ invert) & bit)
  212. vga_drawpixel(dest_x, dest_y);
  213. dest_x++;
  214. if ((bit >>= 1) == 0)
  215. bit = 0x80, ptr_source++;
  216. }
  217. dest_y++;
  218. ptr_line += raster;
  219. }
  220. return 0;
  221. }
  222. /* Copy a color pixel map. This is just like a bitmap, except that */
  223. /* each pixel takes 4 bits instead of 1 when device driver has color. */
  224. private int
  225. vgalib_copy_color(gx_device * dev, const byte * base, int sourcex,
  226. int raster, gx_bitmap_id id, int x, int y,
  227. int width, int height)
  228. {
  229. fit_copy(dev, base, sourcex, raster, id, x, y, width, height);
  230. if (gx_device_has_color(dev)) { /* color device, four bits per pixel */
  231. const byte *line = base + (sourcex >> 1);
  232. int dest_y = y, end_x = x + width;
  233. if (width <= 0)
  234. return 0;
  235. while (height--) { /* for each line */
  236. const byte *source = line;
  237. register int dest_x = x;
  238. if (sourcex & 1) { /* odd nibble first */
  239. int color = *source++ & 0xf;
  240. vga_setcolor(color);
  241. vga_drawpixel(dest_x, dest_y);
  242. dest_x++;
  243. }
  244. /* Now do full bytes */
  245. while (dest_x < end_x) {
  246. int color = *source >> 4;
  247. vga_setcolor(color);
  248. vga_drawpixel(dest_x, dest_y);
  249. dest_x++;
  250. if (dest_x < end_x) {
  251. color = *source++ & 0xf;
  252. vga_setcolor(color);
  253. vga_drawpixel(dest_x, dest_y);
  254. dest_x++;
  255. }
  256. }
  257. dest_y++;
  258. line += raster;
  259. }
  260. } else { /* monochrome device: one bit per pixel */
  261. /* bitmap is the same as bgi_copy_mono: one bit per pixel */
  262. vgalib_copy_mono(dev, base, sourcex, raster, id, x, y, width, height,
  263. (gx_color_index) 0, (gx_color_index) 7);
  264. }
  265. return 0;
  266. }
  267. /* Read bits back from the device. */
  268. private int
  269. vgalib_get_bits(gx_device * dev, int y, byte * data, byte ** actual_data)
  270. {
  271. int x;
  272. byte *dest = data;
  273. int b = 0;
  274. int depth = dev->color_info.depth; /* 1 or 4 */
  275. int mask = (1 << depth) - 1;
  276. int left = 8;
  277. if (actual_data)
  278. *actual_data = data;
  279. for (x = 0; x < dev->width; ++x) {
  280. int color = vga_getpixel(x, y);
  281. if ((left -= depth) < 0)
  282. *dest++ = b, b = 0, left += 8;
  283. b += (color & mask) << left;
  284. }
  285. if (left < 8)
  286. *dest = b;
  287. return 0;
  288. }
  289. /* Get/put the display mode parameter. */
  290. private int
  291. vgalib_get_params(gx_device * dev, gs_param_list * plist)
  292. {
  293. int code = gx_default_get_params(dev, plist);
  294. if (code < 0)
  295. return code;
  296. return param_write_int(plist, "DisplayMode", &vga_dev->display_mode);
  297. }
  298. private int
  299. vgalib_put_params(gx_device * dev, gs_param_list * plist)
  300. {
  301. int ecode = 0;
  302. int code;
  303. int imode = vga_dev->display_mode;
  304. const char *param_name;
  305. switch (code = param_read_int(plist, (param_name = "DisplayMode"), &imode)) {
  306. default:
  307. ecode = code;
  308. param_signal_error(plist, param_name, ecode);
  309. case 0:
  310. case 1:
  311. break;
  312. }
  313. if (ecode < 0)
  314. return ecode;
  315. code = gx_default_put_params(dev, plist);
  316. if (code < 0)
  317. return code;
  318. if (imode != vga_dev->display_mode) {
  319. if (dev->is_open)
  320. gs_closedevice(dev);
  321. vga_dev->display_mode = imode;
  322. }
  323. return 0;
  324. }