home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 1996 February / PCWK0296.iso / sharewar / dos / program / gs300sr1 / gs300sr1.exe / GDEVBIT.C < prev    next >
C/C++ Source or Header  |  1994-07-27  |  6KB  |  187 lines

  1. /* Copyright (C) 1991, 1994 Aladdin Enterprises.  All rights reserved.
  2.   
  3.   This file is part of Aladdin Ghostscript.
  4.   
  5.   Aladdin Ghostscript is distributed with NO WARRANTY OF ANY KIND.  No author
  6.   or distributor accepts any responsibility for the consequences of using it,
  7.   or for whether it serves any particular purpose or works at all, unless he
  8.   or she says so in writing.  Refer to the Aladdin Ghostscript Free Public
  9.   License (the "License") for full details.
  10.   
  11.   Every copy of Aladdin Ghostscript must include a copy of the License,
  12.   normally in a plain ASCII text file named PUBLIC.  The License grants you
  13.   the right to copy, modify and redistribute Aladdin Ghostscript, but only
  14.   under certain conditions described in the License.  Among other things, the
  15.   License requires that the copyright notice and this notice be preserved on
  16.   all copies.
  17. */
  18.  
  19. /* gdevbit.c */
  20. /* "Plain bits" device to estimate rendering time. */
  21. #include "gdevprn.h"
  22. #include "gsparam.h"
  23. #include "gxlum.h"
  24.  
  25. /* Define the device parameters. */
  26. #ifndef X_DPI
  27. #  define X_DPI 72
  28. #endif
  29. #ifndef Y_DPI
  30. #  define Y_DPI 72
  31. #endif
  32.  
  33. /* The device descriptor */
  34. private dev_proc_map_rgb_color(bit_map_rgb_color);
  35. private dev_proc_map_cmyk_color(bit_map_cmyk_color);
  36. private dev_proc_put_params(bit_put_params);
  37. private dev_proc_print_page(bit_print_page);
  38. private gx_device_procs bit_procs = {
  39.     gdev_prn_open,
  40.     gx_default_get_initial_matrix,
  41.     NULL,    /* sync_output */
  42.     gdev_prn_output_page,
  43.     gdev_prn_close,
  44.     bit_map_rgb_color,
  45.     gdev_prn_map_color_rgb,    /* (never called) */
  46.     NULL,    /* fill_rectangle */
  47.     NULL,    /* tile_rectangle */
  48.     NULL,    /* copy_mono */
  49.     NULL,    /* copy_color */
  50.     NULL,    /* draw_line */
  51.     NULL,    /* get_bits */
  52.     gdev_prn_get_params,
  53.     bit_put_params,
  54.     bit_map_cmyk_color,
  55.     NULL,    /* get_xfont_procs */
  56.     NULL,    /* get_xfont_device */
  57.     NULL,    /* map_rgb_alpha_color */
  58.     gx_page_device_get_page_device    /* get_page_device */
  59. };
  60. gx_device_printer far_data gs_bit_device =
  61.   prn_device(bit_procs, "bit",
  62.     DEFAULT_WIDTH_10THS, DEFAULT_HEIGHT_10THS,
  63.     X_DPI, Y_DPI,
  64.     0,0,0,0,            /* margins */
  65.     1, bit_print_page);
  66.  
  67. /* Map RGB or gray to color. */
  68. /* Note that 1-bit monochrome is a special case. */
  69. private gx_color_index
  70. bit_map_rgb_color(gx_device *dev, gx_color_value red,
  71.   gx_color_value green, gx_color_value blue)
  72. {    int ncomps = dev->color_info.num_components;
  73.     int bpc = dev->color_info.depth / ncomps;
  74.     int drop = sizeof(gx_color_value) * 8 - bpc;
  75.     if ( ncomps == 1 )
  76.       {    gx_color_value gray = 
  77.           (red * (unsigned long)lum_red_weight +
  78.            green * (unsigned long)lum_green_weight +
  79.            blue * (unsigned long)lum_blue_weight +
  80.            (lum_all_weights / 2))
  81.             / lum_all_weights;
  82.         return (bpc == 1 ? gx_max_color_value - gray : gray) >> drop;
  83.       }
  84.     else
  85.       return ((((red >> drop) << bpc) + (green >> drop)) << bpc) +
  86.         (blue >> drop);
  87. }
  88.  
  89. /* Map CMYK to color. */
  90. private gx_color_index
  91. bit_map_cmyk_color(gx_device *dev, gx_color_value cyan,
  92.   gx_color_value magenta, gx_color_value yellow, gx_color_value black)
  93. {    if ( dev->color_info.num_components < 4 )
  94.       return gx_default_map_cmyk_color(dev, cyan, magenta, yellow, black);
  95.     else
  96.       {    int bpc = dev->color_info.depth / 4;
  97.         int drop = sizeof(gx_color_value) * 8 - bpc;
  98.         return ((((((cyan >> drop) << bpc) +
  99.                (magenta >> drop)) << bpc) +
  100.              (yellow >> drop)) << bpc) +
  101.                (black >> drop);
  102.       }
  103. }
  104.  
  105. /* Set parameters.  For maximum flexibility, we allow setting */
  106. /* the number of color components and the number of bits per component. */
  107. private int
  108. bit_put_params(gx_device *pdev, gs_param_list *plist)
  109. {    int ncomps = pdev->color_info.num_components;
  110.     int bpc = pdev->color_info.depth / ncomps;
  111.     int v;
  112.     int ecode = gdev_prn_put_params(pdev, plist);
  113.     int rcode = ecode, code;
  114.     static const byte depths[4][8] = {
  115.       { 1, 2, 0, 4, 8, 0, 0, 8 },
  116.       { 0 },
  117.       { 4, 8, 0, 16, 16, 0, 0, 24 },
  118.       { 4, 8, 0, 16, 32, 0, 0, 32 }
  119.     };
  120.     const char _ds *vname;
  121.  
  122.     switch ( code = param_read_int(plist, "Colors", &ncomps) )
  123.     {
  124.     default:
  125.         ecode = code;
  126.     case 1:
  127.         break;
  128.     case 0:
  129.         if ( !(ncomps == 1 || ncomps == 3 || ncomps == 4) )
  130.           param_signal_error(plist, "Colors",
  131.                      ecode = gs_error_rangecheck);
  132.         else
  133.           rcode = 1;
  134.     }
  135.     if ( (code = param_read_int(plist, vname = "GrayValues", &v)) != 1 ||
  136.          (code = param_read_int(plist, vname = "RedValues", &v)) != 1 ||
  137.          (code = param_read_int(plist, vname = "GreenValues", &v)) != 1 ||
  138.          (code = param_read_int(plist, vname = "BlueValues", &v)) != 1
  139.        )
  140.     {    if ( code < 0 )
  141.           ecode = code;
  142.         else switch ( v )
  143.         {
  144.         case 2: bpc = 1; break;
  145.         case 4: bpc = 2; break;
  146.         case 16: bpc = 4; break;
  147.         case 32: bpc = 5; break;
  148.         case 256: bpc = 8; break;
  149.         default: param_signal_error(plist, vname,
  150.                         ecode = gs_error_rangecheck);
  151.         }
  152.         if ( ecode == 0 )
  153.           rcode = 1;
  154.     }
  155.     if ( ecode < 0 )
  156.       return ecode;
  157.     if ( rcode == 0 )
  158.       return 0;
  159.     pdev->color_info.num_components = ncomps;
  160.     pdev->color_info.depth = depths[ncomps - 1][bpc - 1];
  161.     pdev->color_info.max_gray = pdev->color_info.max_rgb =
  162.       (pdev->color_info.dither_gray = pdev->color_info.dither_rgb =
  163.        (1 << bpc)) - 1;
  164.     return rcode;
  165. }
  166.  
  167. /* Send the page to the printer. */
  168. private int
  169. bit_print_page(gx_device_printer *pdev, FILE *prn_stream)
  170. {    /* Just dump the bits on the file. */
  171.     /* If the file is 'nul', don't even do the writes. */
  172.     int line_size = gdev_mem_bytes_per_scan_line((gx_device *)pdev);
  173.     int lnum;
  174.     byte *in = (byte *)gs_malloc(line_size, 1, "bit_print_page(in)");
  175.     byte *data;
  176.     int nul = !strcmp(pdev->fname, "nul");
  177.     if ( in == 0 )
  178.         return_error(gs_error_VMerror);
  179.     for ( lnum = 0; lnum < pdev->height; lnum++ )
  180.        {    gdev_prn_get_bits(pdev, lnum, in, &data);
  181.         if ( !nul )
  182.             fwrite(data, 1, line_size, prn_stream);
  183.        }
  184.     gs_free((char *)in, line_size, 1, "bit_print_page(in)");
  185.     return 0;
  186. }
  187.