home *** CD-ROM | disk | FTP | other *** search
/ APDL Public Domain 1 / APDL_PD1A.iso / printing / ghostscrip / source / _gs / c / gsdevice < prev    next >
Encoding:
Text File  |  1991-10-26  |  10.3 KB  |  374 lines

  1. /* Copyright (C) 1989, 1990, 1991 Aladdin Enterprises.  All rights reserved.
  2.    Distributed by Free Software Foundation, Inc.
  3.  
  4. This file is part of Ghostscript.
  5.  
  6. Ghostscript is distributed in the hope that it will be useful, but
  7. WITHOUT ANY WARRANTY.  No author or distributor accepts responsibility
  8. to anyone for the consequences of using it or for whether it serves any
  9. particular purpose or works at all, unless he says so in writing.  Refer
  10. to the Ghostscript General Public License for full details.
  11.  
  12. Everyone is granted permission to copy, modify and redistribute
  13. Ghostscript, but only under the conditions described in the Ghostscript
  14. General Public License.  A copy of this license is supposed to have been
  15. given to you along with Ghostscript so you can know your rights and
  16. responsibilities.  It should be in a file named COPYING.  Among other
  17. things, the copyright notice and this notice must be preserved on all
  18. copies.  */
  19.  
  20. /* gsdevice.c */
  21. /* Device operators for Ghostscript library */
  22. #include "memory_.h"            /* for memcpy */
  23. #include "gx.h"
  24. #include "gserrors.h"
  25. #include "gxfixed.h"            /* ditto */
  26. #include "gxmatrix.h"            /* for gzstate.h */
  27. #include "gxbitmap.h"
  28. #include "gzstate.h"
  29. #include "gzdevice.h"
  30. #include "gxdevmem.h"
  31.  
  32. /* Import the device list from gdevs.c */
  33. extern gx_device *gx_device_list[];
  34.  
  35. /* Device definitions */
  36. /* Following defines the null device */
  37. private dev_proc_fill_rectangle(null_fill_rectangle);
  38. private dev_proc_tile_rectangle(null_tile_rectangle);
  39. private dev_proc_copy_mono(null_copy_mono);
  40. private dev_proc_copy_color(null_copy_color);
  41. private dev_proc_draw_line(null_draw_line);
  42. private dev_proc_fill_trapezoid(null_fill_trapezoid);
  43. private dev_proc_tile_trapezoid(null_tile_trapezoid);
  44.  
  45. private gx_device_procs null_procs = {
  46.     gx_default_open_device,
  47.     gx_default_get_initial_matrix,
  48.     gx_default_sync_output,
  49.     gx_default_output_page,
  50.     gx_default_close_device,
  51.     gx_default_map_rgb_color,
  52.     gx_default_map_color_rgb,
  53.     null_fill_rectangle,
  54.     null_tile_rectangle,
  55.     null_copy_mono,
  56.     null_copy_color,
  57.     null_draw_line,
  58.     null_fill_trapezoid,
  59.     null_tile_trapezoid
  60. };
  61. private gx_device null_device = {
  62.     sizeof(device),
  63.     &null_procs,
  64.     "null",
  65.     0, 0,
  66.     72, 72,
  67.     no_margins,
  68.     0, 1, 1, 1
  69. };
  70.  
  71. /* The null device */
  72. gx_device *gx_device_null_p = &null_device;
  73.  
  74. /* Flush buffered output to the device */
  75. int
  76. gs_flushpage(gs_state *pgs)
  77. {    gx_device *dev = pgs->device->info;
  78.     return (*dev->procs->sync_output)(dev);
  79. }
  80.  
  81. /* Make the device output the accumulated page description */
  82. int
  83. gs_copypage(gs_state *pgs)
  84. {    gx_device *dev = pgs->device->info;
  85.     return (*dev->procs->output_page)(dev);
  86. }
  87.  
  88. /* Copy scan lines from an image device */
  89. int
  90. gs_copyscanlines(gx_device *dev, int start_y, byte *data, uint size,
  91.   int *plines_copied, uint *pbytes_copied)
  92. {    int count;
  93.     if ( !gs_device_is_memory(dev) ) return_error(gs_error_undefined);
  94.     count = gdev_mem_copy_scan_lines((gx_device_memory *)dev,
  95.                      start_y, data, size);
  96.     if ( plines_copied != NULL )
  97.         *plines_copied = count;
  98.     if ( pbytes_copied != NULL )
  99.         *pbytes_copied = count * gdev_mem_bytes_per_scan_line(dev);
  100.     return 0;
  101. }
  102.  
  103. /* Get the current device from the graphics state */
  104. gx_device *
  105. gs_currentdevice(gs_state *pgs)
  106. {    return pgs->device->info;
  107. }
  108.  
  109. /* Get the name of a device */
  110. char *
  111. gs_devicename(gx_device *dev)
  112. {    return dev->dname;
  113. }
  114.  
  115. /* Read out the current device parameters */
  116. void
  117. gs_deviceparams(gx_device *dev, gs_matrix *pmat, int *pwidth, int *pheight)
  118. {    (*dev->procs->get_initial_matrix)(dev, pmat);
  119.     *pwidth = dev->width;
  120.     *pheight = dev->height;
  121. }
  122.  
  123. /* Get the N'th device from the known device list */
  124. gx_device *
  125. gs_getdevice(int index)
  126. {    int i;
  127.     for ( i = 0; gx_device_list[i] != 0; i++ )
  128.        {    if ( i == index ) return gx_device_list[i];
  129.        }
  130.     return 0;            /* index out of range */
  131. }
  132.  
  133. /* Make a device by cloning an existing one */
  134. int
  135. gs_makedevice(gx_device **pnew_dev, gx_device *dev, gs_matrix *pmat,
  136.   uint width, uint height, proc_alloc_t palloc)
  137. {    register gx_device *new_dev;
  138.     new_dev = (gx_device *)(*palloc)(1, dev->params_size, "gs_makedevice");
  139.     if ( new_dev == 0 ) return_error(gs_error_VMerror);
  140.     if ( width <= 0 || height <= 0 ) return_error(gs_error_rangecheck);
  141.     memcpy(new_dev, dev, dev->params_size);
  142.     new_dev->width = width;
  143.     new_dev->height = height;
  144.     new_dev->is_open = 0;
  145.     *pnew_dev = new_dev;
  146.     return 0;
  147. }
  148.  
  149. /* Make a memory (image) device. */
  150. /* If num_colors = -24 or -32, this is a true-color device; */
  151. /* otherwise, num_colors is the number of colors in the palette. */
  152. int
  153. gs_makeimagedevice(gx_device **pnew_dev, gs_matrix *pmat,
  154.   uint width, uint height, float *colors, int num_colors, proc_alloc_t palloc)
  155. {    gx_device_memory *old_dev;
  156.     register gx_device_memory *new_dev;
  157.     byte *bits;
  158.     int palette_size = num_colors;
  159.     int pcount;
  160.     int bits_per_pixel;
  161.     ulong bitmap_size;
  162.     int white = -1, black = -1;
  163.     byte palette[256 * 3];
  164.     int has_color;
  165.     if ( width <= 0 || height <= 0 ) return_error(gs_error_rangecheck);
  166.     /************** TEMPORARY LIMITATION: **************/
  167.     /*** only 1-, 8-, 24-, and 32-bit are supported. ***/
  168.     switch ( num_colors )
  169.        {
  170.     case 2:
  171.         bits_per_pixel = 1; old_dev = &mem_mono_device;
  172.         break;
  173. /***    case 4:        bits_per_pixel = 2; break;    ***/
  174. /***    case 16:    bits_per_pixel = 4; break;    ***/
  175.     case 256:
  176.         bits_per_pixel = 8; old_dev = &mem_mapped8_color_device;
  177.         break;
  178.     case -24:
  179.         bits_per_pixel = 24; old_dev = &mem_true24_color_device;
  180.         palette_size = 0; break;
  181.     case -32:
  182.         bits_per_pixel = 32; old_dev = &mem_true32_color_device;
  183.         palette_size = 0; break;
  184.     default:
  185.         return_error(gs_error_rangecheck);
  186.        }
  187.     pcount = palette_size * 3;
  188.     /* Check to make sure the palette contains white and black. */
  189.     if ( bits_per_pixel <= 8 )
  190.        {    float *p;
  191.         byte *q;
  192.         int i;
  193.         has_color = 0;
  194.         for ( i = 0, p = colors, q = palette;
  195.               i < pcount; i++, p++, q++
  196.             )
  197.            {    if ( *p < -0.001 || *p > 1.001 )
  198.                 return_error(gs_error_rangecheck);
  199.             *q = (*p * 255) + 0.5;
  200.             if ( i % 3 == 2 )
  201.                {
  202.                 if ( *q == q[-1] && *q == q[-2] )
  203.                    {    if ( *q == 0 ) black = i - 2;
  204.                     else if ( *q == 255 ) white = i - 2;
  205.                    }
  206.                 else
  207.                     has_color = 1;
  208.                }
  209.            }
  210.         if ( white < 0 || black < 0 )
  211.             return_error(gs_error_rangecheck);
  212.        }
  213.     else
  214.         has_color = 1;
  215.     new_dev = (gx_device_memory *)(*palloc)(1, old_dev->params_size, "gs_makeimagedevice(device)");
  216.     if ( new_dev == 0 ) return_error(gs_error_VMerror);
  217.     *new_dev = *old_dev;
  218.     new_dev->initial_matrix = *pmat;
  219.     new_dev->width = width;
  220.     new_dev->height = height;
  221.     new_dev->has_color = has_color;
  222.     new_dev->bits_per_color_pixel = bits_per_pixel;
  223.     bitmap_size = gdev_mem_bitmap_size(new_dev);
  224.     if ( bitmap_size > max_uint )    /* can't allocate it! */
  225.         return_error(gs_error_limitcheck);
  226.     bits = (byte *)(*palloc)(1, (uint)bitmap_size + pcount,
  227.                  "gs_makeimagedevice(bits)");
  228.     if ( bits == 0 ) return_error(gs_error_VMerror);
  229.     new_dev->base = bits;
  230.     new_dev->invert = (black == 0 ? 0 : -1);
  231.     new_dev->palette_size = palette_size;
  232.     new_dev->palette = bits + bitmap_size;
  233.     memcpy(new_dev->palette, palette, pcount);
  234.     new_dev->is_open = 0;
  235.     *pnew_dev = (gx_device *)new_dev;
  236.     return 0;
  237. }
  238.  
  239. /* Set the device in the graphics state */
  240. int
  241. gs_setdevice(gs_state *pgs, gx_device *dev)
  242. {    register device *pdev = pgs->device;
  243.     int was_open = dev->is_open;
  244.     int code;
  245.     /* Initialize the device */
  246.     if ( !was_open )
  247.        {    code = (*dev->procs->open_device)(dev);
  248.         if ( code < 0 ) return code;
  249.         dev->is_open = 1;
  250.        }
  251.     /* Compute device white and black codes */
  252.     pdev->black = (*dev->procs->map_rgb_color)(dev, 0, 0, 0);
  253.     pdev->white = (*dev->procs->map_rgb_color)(dev, dev->max_rgb_value, dev->max_rgb_value, dev->max_rgb_value);
  254.     pdev->info = dev;
  255.     if (    (code = gs_initmatrix(pgs)) < 0 ||
  256.         (code = gs_initclip(pgs)) < 0
  257.        )
  258.         return code;
  259.     if ( !was_open )
  260.         if ( (code = gs_erasepage(pgs)) < 0 )
  261.             return code;
  262.     return gx_remap_color(pgs);
  263. }
  264.  
  265. /* Select the null device.  This is just a convenience. */
  266. void
  267. gs_nulldevice(gs_state *pgs)
  268. {    gs_setdevice(pgs, gx_device_null_p);
  269. }
  270.  
  271. /* Close a device.  The client is responsible for ensuring that */
  272. /* this device is not current in any graphics state. */
  273. int
  274. gs_closedevice(gx_device *dev)
  275. {    int code = 0;
  276.     if ( dev->is_open )
  277.        {    code = (dev->procs->close_device)(dev);
  278.         if ( code >= 0 ) dev->is_open = 0;
  279.        }
  280.     return code;
  281. }
  282.  
  283. /* Install enough of a null device to suppress graphics output */
  284. /* during the execution of stringwidth. */
  285. void
  286. gx_device_no_output(gs_state *pgs)
  287. {    pgs->device->info = &null_device;
  288. }
  289.  
  290. /* Read the native color space of the current device. */
  291. gs_color_space
  292. gx_device_color_space(gs_state *pgs)
  293. {    return (pgs->device->info->has_color ? gs_color_space_DeviceRGB :
  294.         gs_color_space_DeviceGray);
  295. }
  296.  
  297. /* ------ The null `device' ------ */
  298.  
  299. private int
  300. null_fill_rectangle(gx_device *dev, int x, int y, int w, int h,
  301.   gx_color_index color)
  302. {    return 0;
  303. }
  304. private int
  305. null_tile_rectangle(gx_device *dev, gx_bitmap *tile,
  306.   int x, int y, int w, int h, gx_color_index zero, gx_color_index one,
  307.   int px, int py)
  308. {    return 0;
  309. }
  310. private int
  311. null_copy_mono(gx_device *dev, byte *data,
  312.   int dx, int raster, int x, int y, int w, int h,
  313.   gx_color_index zero, gx_color_index one)
  314. {    return 0;
  315. }
  316. private int
  317. null_copy_color(gx_device *dev, byte *data,
  318.   int dx, int raster, int x, int y, int w, int h)
  319. {    return 0;
  320. }
  321. private int
  322. null_draw_line(gx_device *dev, int x0, int y0, int x1, int y1,
  323.   gx_color_index color)
  324. {    return 0;
  325. }
  326. private int
  327. null_fill_trapezoid(gx_device *dev,
  328.   int x0, int y0, int w0, int x1, int y1, int w1, gx_color_index color)
  329. {    return 0;
  330. }
  331. private int
  332. null_tile_trapezoid(gx_device *dev, gx_bitmap *tile,
  333.   int x0, int y0, int w0, int x1, int y1, int w1,
  334.   gx_color_index color0, gx_color_index color1, int px, int py)
  335. {    return 0;
  336. }
  337.  
  338. /* ------ Default device procedures ------ */
  339.  
  340. int
  341. gx_default_open_device(gx_device *dev)
  342. {    return 0;
  343. }
  344. void
  345. gx_default_get_initial_matrix(register gx_device *dev, register gs_matrix *pmat)
  346. {    pmat->xx = dev->x_pixels_per_inch / 72.0;
  347.     pmat->xy = 0;
  348.     pmat->yx = 0;
  349.     pmat->yy = dev->y_pixels_per_inch / -72.0;
  350.     pmat->tx = 0;
  351.     pmat->ty = dev->height;
  352. }
  353. int
  354. gx_default_sync_output(gx_device *dev)
  355. {    return 0;
  356. }
  357. int
  358. gx_default_output_page(gx_device *dev)
  359. {    return (*dev->procs->sync_output)(dev);
  360. }
  361. int
  362. gx_default_close_device(gx_device *dev)
  363. {    return 0;
  364. }
  365. gx_color_index
  366. gx_default_map_rgb_color(gx_device *dev, ushort r, ushort g, ushort b)
  367. {    return (gx_color_index)max(max(r, g), b);
  368. }
  369. int
  370. gx_default_map_color_rgb(gx_device *dev, gx_color_index color, ushort prgb[3])
  371. {    prgb[0] = prgb[1] = prgb[2] = (ushort)color;
  372.     return 0;
  373. }
  374.