home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 1996 February / PCWK0296.iso / sharewar / dos / program / gs300sr1 / gs300sr1.exe / GDEVPM.C < prev    next >
C/C++ Source or Header  |  1994-08-01  |  35KB  |  1,169 lines

  1. /* Copyright (C) 1992, 1993, 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. /* gdevpm.c */
  20. /*
  21.  * OS/2 Presentation manager driver
  22.  * By Russell Lang (based on gdevmswn.c and gdevwdib.c)
  23.  *
  24.  * If Ghostscript is a PM application, stdin/stdout are not
  25.  * provided and so no text window is available.
  26.  * If Ghostscript is a windowed text application, a message queue
  27.  * can't be created so a PM window for graphics can't be created.
  28.  * The solution used here is to have two programs - gsos2.exe is a 
  29.  * text application and the outboard PM driver is gspmdrv.exe.
  30.  * Another solution may be to make Ghostscript a PM application
  31.  * and use VIO calls to provide a text window.
  32.  *
  33.  * If PM GSview starts Ghostscript, PM GSview displays the
  34.  * bitmap instead of the PM driver (gspmdrv.exe).
  35.  *
  36.  * Since Ghostscript is not a PM application, this driver creates a
  37.  * BMP bitmap in a named shared memory block and a second process
  38.  * gspmdrv.exe reads this memory block and provides the PM window.  
  39.  * Communication to gspmdrv.exe is via the shared memory block
  40.  * and semaphores.
  41.  */
  42.  
  43. #define INCL_DOS
  44. #define INCL_DOSERRORS
  45. #define INCL_WINWINDOWMGR
  46. #define INCL_DEV
  47. #define INCL_GPIBITMAPS
  48.  
  49. #include <os2.h>
  50. #include "string_.h"
  51. #include <stdlib.h>
  52. #include "gx.h"
  53. #include "gserrors.h"
  54. #include "gsexit.h"            /* for gs_exit_status */
  55. #include "gxdevice.h"
  56.  
  57. #include "gp.h"
  58. #include "gpcheck.h"
  59. #include "gsparam.h"
  60. #include "gdevpccm.h"
  61.  
  62. #include "gxdevmem.h"
  63. #include "gdevpm.h"
  64. #ifdef __DLL__
  65. #include "gsdll.h"
  66. #endif
  67.  
  68. #define MIN_COMMIT 4096    /* memory is committed in these size chunks */
  69. #define ID_NAME "GSPMDRV_%u_%u"
  70.  
  71. /* Initial values for width and height */
  72. #define INITIAL_RESOLUTION 96
  73. #define INITIAL_WIDTH (INITIAL_RESOLUTION * 85 / 10 + 1)
  74. #define INITIAL_HEIGHT (INITIAL_RESOLUTION * 11 + 1)
  75.  
  76. /* A macro for casting the device argument */
  77. #define pmdev ((gx_device_pm *)dev)
  78.  
  79.  
  80. #define pm_gsview_sizeof 80
  81. typedef struct gx_device_pm_s gx_device_pm;
  82.  
  83. #define gx_device_pm_common\
  84.     int BitsPerPixel;\
  85.     int UpdateInterval;\
  86.     char GSVIEW[pm_gsview_sizeof];\
  87.     BOOL dll;\
  88.     int nColors;\
  89.     BOOL updating;\
  90.     HTIMER update_timer;\
  91.     HEV sync_event;\
  92.     HEV next_event;\
  93.     HMTX bmp_mutex;\
  94.     HQUEUE drv_queue;\
  95.     HQUEUE term_queue;\
  96.     ULONG session_id;\
  97.     PID process_id;\
  98.     PID gspid;\
  99.     unsigned char *bitmap;\
  100.     ULONG committed;\
  101.     PBITMAPINFO2 bmi
  102.  
  103. /* The device descriptor */
  104. struct gx_device_pm_s {
  105.     gx_device_common;
  106.     gx_device_pm_common;
  107.     gx_device_memory mdev;
  108. };
  109.  
  110. /* Device procedures */
  111.  
  112. /* See gxdevice.h for the definitions of the procedures. */
  113. private dev_proc_open_device(pm_open);
  114. private dev_proc_get_initial_matrix(pm_get_initial_matrix);
  115. private dev_proc_sync_output(pm_sync_output);
  116. private dev_proc_output_page(pm_output_page);
  117. private dev_proc_close_device(pm_close);
  118. private dev_proc_map_rgb_color(pm_map_rgb_color);
  119. private dev_proc_map_color_rgb(pm_map_color_rgb);
  120. private dev_proc_fill_rectangle(pm_fill_rectangle);
  121. private dev_proc_copy_mono(pm_copy_mono);
  122. private dev_proc_copy_color(pm_copy_color);
  123. private dev_proc_get_params(pm_get_params);
  124. private dev_proc_put_params(pm_put_params);
  125.  
  126. private gx_device_procs pm_procs = {
  127.     pm_open,
  128.     pm_get_initial_matrix,
  129.     pm_sync_output,
  130.     pm_output_page,
  131.     pm_close,
  132.     pm_map_rgb_color,
  133.     pm_map_color_rgb,
  134.     pm_fill_rectangle,
  135.     gx_default_tile_rectangle,
  136.     pm_copy_mono,
  137.     pm_copy_color,
  138.     gx_default_draw_line,
  139.     gx_default_get_bits,
  140.     pm_get_params,
  141.     pm_put_params,
  142.     gx_default_map_cmyk_color,
  143.     gx_default_get_xfont_procs
  144. };
  145. #ifdef __DLL__
  146. gx_device_pm gs_os2dll_device = {
  147.     sizeof(gx_device_pm),
  148.     &pm_procs,
  149.     "os2dll",
  150.     INITIAL_WIDTH, INITIAL_HEIGHT,
  151.     INITIAL_RESOLUTION, INITIAL_RESOLUTION,
  152.     no_margins,
  153.     dci_black_and_white,
  154.     dev_init_misc, { 0 },    /* not open yet */
  155.     8,        /* BitsPerPixel */
  156.     5000,        /* UpdateInterval */
  157.     "\0",        /* GSVIEW */
  158.     1        /* is DLL device */
  159. };
  160. #endif
  161. gx_device_pm gs_os2pm_device = {
  162.     sizeof(gx_device_pm),
  163.     &pm_procs,
  164.     "os2pm",
  165.     INITIAL_WIDTH, INITIAL_HEIGHT,
  166.     INITIAL_RESOLUTION, INITIAL_RESOLUTION,
  167.     no_margins,
  168.     dci_black_and_white,
  169.     dev_init_misc, { 0 },    /* not open yet */
  170.     8,        /* BitsPerPixel */
  171.     5000,        /* UpdateInterval */
  172.     "\0",        /* GSVIEW */
  173.     0        /* is not DLL device */
  174. };
  175.  
  176. /* Compress a gx_color_value into an 8-bit PM color value, */
  177. /* using only the high order 5 bits. */
  178. #define pm_color_value(z)\
  179.   ((((z) >> (gx_color_value_bits - 5)) << 3) +\
  180.    ((z) >> (gx_color_value_bits - 3)))
  181.  
  182. /* prototypes for internal procedures */
  183. private void pm_makepalette(gx_device_pm *);
  184. private void pm_update(gx_device_pm *);
  185. private uint pm_set_bits_per_pixel(gx_device_pm *, int);
  186. private uint pm_palette_size(gx_device_pm *);
  187. private int pm_alloc_bitmap(gx_device_pm *, gx_device *);
  188. private int pm_run_gspmdrv(gx_device_pm *);
  189. private void pm_write_bmp(gx_device_pm *);
  190.  
  191. /* Open the pm driver */
  192. int
  193. pm_open(gx_device *dev)
  194. {
  195.     int ccode;
  196.     CHAR id[128];
  197.     CHAR name[128];
  198.     PTIB pptib;
  199.     PPIB pppib;
  200.  
  201.     if (!pmdev->dll && (_osmode == DOS_MODE)) {
  202.         fprintf(stderr,"os2pm driver can't be used under DOS\n");
  203.         return gs_error_limitcheck;
  204.     }
  205.  
  206.     if (DosGetInfoBlocks(&pptib, &pppib)) {
  207.         fprintf(stderr,"\npm_open: Couldn't get pid\n");
  208.         return gs_error_limitcheck;
  209.     }
  210.     pmdev->gspid = pppib->pib_ulppid; /* use parent (CMD.EXE) pid */
  211.     sprintf(id, ID_NAME, pmdev->gspid, (ULONG)dev);
  212.  
  213.     /* Allocate, but don't commit, enough memory for the largest */
  214.     /* possible bitmap (13Mbytes = A3 x 150dpi x 24bits) */
  215.     /* Shared memory is common to all processes so we don't want to allocate too much */
  216.     sprintf(name, SHARED_NAME, *pmdev->GSVIEW ? pmdev->GSVIEW : id);
  217.     if (DosAllocSharedMem((PPVOID)&pmdev->bitmap, name,
  218.         13*1024*1024, PAG_READ | PAG_WRITE)) {
  219.         fprintf(stderr,"pm_open: failed allocating shared BMP memory %s\n", name);
  220.         return gs_error_limitcheck;
  221.     }
  222.     
  223.     /* commit one page so there is enough storage for a */
  224.     /* bitmap header and palette */
  225.     if (DosSetMem(pmdev->bitmap, MIN_COMMIT, PAG_COMMIT | PAG_DEFAULT)) {
  226.         DosFreeMem(pmdev->bitmap);
  227.         fprintf(stderr,"pm_open: failed committing shared BMP memory\n");
  228.         return gs_error_limitcheck;
  229.     }
  230.     pmdev->committed = MIN_COMMIT;
  231.  
  232.     if (pmdev->dll) {
  233.         /* Create mutex - used for preventing another thread from accessing */
  234.         /* bitmap while we are changing the bitmap size. Initially unowned. */
  235.         sprintf(name, MUTEX_NAME, id);
  236.         if (DosCreateMutexSem(name, &(pmdev->bmp_mutex), 0, FALSE)) {
  237.         DosFreeMem(pmdev->bitmap);
  238.         DosCloseEventSem(pmdev->sync_event);
  239.         DosCloseQueue(pmdev->drv_queue);
  240.         fprintf(stderr,"pm_open: failed to create mutex semaphore %s\n", name);
  241.         return gs_error_limitcheck;
  242.         }
  243.     }
  244.     else {
  245.       if (*pmdev->GSVIEW) {
  246.         APIRET rc;
  247.         /* GSview has already created the necessary objects */
  248.         /* so we use Open instead of Create */
  249.         rc = 0;
  250.         if (!rc) {
  251.             sprintf(name, NEXT_NAME, pmdev->GSVIEW);
  252.         rc = DosOpenEventSem(name, &pmdev->next_event);
  253.         }
  254.         if (!rc) {
  255.         sprintf(name, MUTEX_NAME, pmdev->GSVIEW);
  256.         rc = DosOpenMutexSem(name, &pmdev->bmp_mutex);
  257.         }
  258.         if (!rc) {
  259.         PID owner_pid;
  260.         sprintf(name, QUEUE_NAME, pmdev->GSVIEW);
  261.         rc = DosOpenQueue(&owner_pid, &pmdev->drv_queue, name);
  262.         }
  263.         if (rc) {
  264.         DosFreeMem(pmdev->bitmap);
  265.         DosCloseEventSem(pmdev->next_event);
  266.         fprintf(stderr, "pm_open: failed to open %s, rc = %u\n", name, rc);
  267.         return gs_error_limitcheck;
  268.         }
  269.       }
  270.       else {    /* not GSVIEW */
  271.         /* Create update event semaphore */
  272.         sprintf(name, SYNC_NAME, id);
  273.         if (DosCreateEventSem(name, &(pmdev->sync_event), 0, FALSE)) {
  274.         DosFreeMem(pmdev->bitmap);
  275.         fprintf(stderr,"pm_open: failed to create event semaphore %s\n", name);
  276.         return gs_error_limitcheck;
  277.         }
  278.         /* Create mutex - used for preventing gspmdrv from accessing */
  279.         /* bitmap while we are changing the bitmap size. Initially unowned. */
  280.         sprintf(name, MUTEX_NAME, id);
  281.         if (DosCreateMutexSem(name, &(pmdev->bmp_mutex), 0, FALSE)) {
  282.         DosFreeMem(pmdev->bitmap);
  283.         DosCloseEventSem(pmdev->sync_event);
  284.         DosCloseQueue(pmdev->drv_queue);
  285.         fprintf(stderr,"pm_open: failed to create mutex semaphore %s\n", name);
  286.         return gs_error_limitcheck;
  287.         }
  288.       }
  289.     }
  290.  
  291.     if ( (pm_set_bits_per_pixel(pmdev, pmdev->BitsPerPixel) < 0) ||
  292.          (gdev_mem_device_for_bits(dev->color_info.depth) == 0) )
  293.     {
  294.         if (!pmdev->dll) {
  295.         if (*pmdev->GSVIEW) {
  296.             DosCloseQueue(pmdev->drv_queue);
  297.             DosCloseEventSem(pmdev->next_event);
  298.         }
  299.         else
  300.             DosCloseEventSem(pmdev->sync_event);
  301.         }
  302.         DosCloseMutexSem(pmdev->bmp_mutex);
  303.         DosFreeMem(pmdev->bitmap);
  304.         return gs_error_limitcheck;
  305.     }
  306.  
  307.     /* initialise bitmap header */
  308.     pmdev->bmi = (PBITMAPINFO2)pmdev->bitmap;
  309.     pmdev->bmi->cbFix = 40;        /* OS/2 2.0 and Windows 3.0 compatible */
  310.     pmdev->bmi->cx = dev->width;
  311.     pmdev->bmi->cy = dev->height;
  312.     pmdev->bmi->cPlanes = 1;
  313.     pmdev->bmi->cBitCount = dev->color_info.depth;
  314.     pmdev->bmi->ulCompression = BCA_UNCOMP;
  315.     pmdev->bmi->cbImage = 0;
  316.     pmdev->bmi->cxResolution = (ULONG)(dev->x_pixels_per_inch / 25.4 * 1000);
  317.     pmdev->bmi->cyResolution = (ULONG)(dev->y_pixels_per_inch / 25.4 * 1000);
  318.     if (pmdev->BitsPerPixel <= 8) {
  319.         pmdev->bmi->cclrUsed = 1<<(pmdev->BitsPerPixel);
  320.         pmdev->bmi->cclrImportant = pmdev->nColors;
  321.     }
  322.     else {
  323.         pmdev->bmi->cclrUsed = 0;
  324.         pmdev->bmi->cclrImportant = 0;
  325.     }
  326.  
  327.     pm_makepalette(pmdev);
  328.  
  329.     /* commit pages */
  330.     ccode = pm_alloc_bitmap((gx_device_pm *)dev, dev);
  331.     if (ccode < 0) {
  332.         if (!pmdev->dll) {
  333.         if (*pmdev->GSVIEW) {
  334.             DosCloseQueue(pmdev->drv_queue);
  335.             DosCloseEventSem(pmdev->next_event);
  336.         }
  337.         else
  338.             DosCloseEventSem(pmdev->sync_event);
  339.         }
  340.         DosCloseMutexSem(pmdev->bmp_mutex);
  341.         DosFreeMem(pmdev->bitmap);
  342.         return ccode;
  343.     }
  344.  
  345.     if (*pmdev->GSVIEW)
  346.         return 0;    /* GSview will handle displaying */
  347.  
  348. #ifdef __DLL__
  349.     if (pmdev->dll) {
  350.         /* notify caller about new device */
  351.         (*pgsdll_callback)(GSDLL_DEVICE, (unsigned char *)pmdev, 1);
  352.         return 0;    /* caller will handle displaying */
  353.     }
  354. #endif
  355.  
  356.     ccode = pm_run_gspmdrv(pmdev);
  357.     if (ccode < 0) {
  358.         DosFreeMem(pmdev->bitmap);
  359.         DosCloseEventSem(pmdev->sync_event);
  360.         DosCloseMutexSem(pmdev->bmp_mutex);
  361.     }
  362.         
  363.     return ccode;
  364. }
  365.  
  366. /* Get the initial matrix.  BMPs, unlike most displays, */
  367. /* put (0,0) in the lower left corner. */
  368. private void
  369. pm_get_initial_matrix(gx_device *dev, gs_matrix *pmat)
  370. {    pmat->xx = dev->x_pixels_per_inch / 72.0;
  371.     pmat->xy = 0;
  372.     pmat->yx = 0;
  373.     pmat->yy = dev->y_pixels_per_inch / 72.0;
  374.     pmat->tx = 0;
  375.     pmat->ty = 0;
  376.     if (*pmdev->GSVIEW)
  377.        pm_update((gx_device_pm *)dev);  /* let GSVIEW know we are drawing */
  378. }
  379.  
  380. /* Make the output appear on the screen. */
  381. int
  382. pm_sync_output(gx_device *dev)
  383. {
  384. #ifdef __DLL__
  385.     if (pmdev->dll) {
  386.     (*pgsdll_callback)(GSDLL_SYNC, (unsigned char *)dev, 0);
  387.     return 0;
  388.     }
  389. #endif
  390.  
  391.     /* tell gspmdrv or GSview process to update display */
  392.     if (*pmdev->GSVIEW) {
  393.     APIRET rc;
  394.     rc = DosWriteQueue(pmdev->drv_queue, GS_SYNC, 0, NULL, 0);
  395.     if (rc)
  396.         fprintf(stderr,"pm_sync_output: DosWriteQueue error %d\n",rc);
  397.     }
  398.     else {
  399.         if (pmdev->updating)
  400.             DosStopTimer(pmdev->update_timer);
  401.     DosPostEventSem(pmdev->sync_event);
  402.     }
  403.     pmdev->updating = FALSE;
  404.     return(0);
  405. }
  406.  
  407. /* Make the output appear on the screen  */
  408. /* and bring image window to foreground. */
  409. int
  410. pm_output_page(gx_device *dev, int copies, int flush)
  411. {
  412. int code;
  413. APIRET rc;
  414. #ifdef DEBUG
  415.     pm_write_bmp(pmdev);
  416. #endif
  417. #ifdef __DLL__
  418.         if (pmdev->dll) {
  419.         (*pgsdll_callback)(GSDLL_PAGE, (unsigned char *)dev, 0);
  420.         return 0;
  421.         }
  422. #endif
  423.  
  424.     if (*pmdev->GSVIEW) {
  425.         if (copies == -2) {
  426.                 rc = DosWriteQueue(pmdev->drv_queue, GS_END, 0, NULL, 0);
  427.                 if (rc)
  428.             fprintf(stderr,"pm_output_page: DosWriteQueue error %d\n",rc);
  429.         }
  430.         else if (copies == -1) {
  431.                 rc = DosWriteQueue(pmdev->drv_queue, GS_BEGIN, 0, NULL, 0);
  432.                 if (rc)
  433.             fprintf(stderr,"pm_output_page: DosWriteQueue error %d\n",rc);
  434.         }
  435.         else {
  436.             ULONG count;
  437.             pmdev->updating = FALSE;
  438.             /* signal GSview that another page is ready */
  439.                 rc = DosWriteQueue(pmdev->drv_queue, GS_PAGE, 0, NULL, 0);
  440.                 if (rc)
  441.             fprintf(stderr,"pm_output_page: DosWriteQueue error %d\n",rc);
  442.             /* wait for GSview to signal we can move on to next page */
  443.             DosWaitEventSem(pmdev->next_event, SEM_INDEFINITE_WAIT);
  444.             DosResetEventSem(pmdev->next_event, &count);
  445.         }
  446.         code = 0;
  447.     }
  448.     else {
  449.         code = pm_sync_output(dev);
  450.         rc = DosSelectSession(pmdev->session_id);
  451.         if (rc) {
  452.           DosSleep(2000);    /* give gspmdrv.exe a chance to run */
  453.           rc = DosSelectSession(pmdev->session_id);
  454.           if (rc == ERROR_SMG_NO_TARGET_WINDOW) {
  455.               DosSleep(5000);    /* give gspmdrv.exe a chance to run */
  456.               rc = DosSelectSession(pmdev->session_id);  /* try yet again */
  457.           }
  458.           if ( (rc == ERROR_SMG_SESSION_NOT_FOUND) ||
  459.          (rc == ERROR_SMG_INVALID_SESSION_ID) ){
  460.         /* someone has killed the session */
  461.         REQUESTDATA Request;
  462.         ULONG DataLength;
  463.         PVOID DataAddress;
  464.         PULONG QueueEntry;
  465.         BYTE ElemPriority;
  466.         /* Close gspmdrv driver */
  467.         DosStopSession(STOP_SESSION_SPECIFIED, pmdev->session_id);
  468.         Request.pid = pmdev->gspid;
  469.         Request.ulData = 0;
  470.         /* wait for termination queue, queue is then closed by session manager */
  471.         DosReadQueue(pmdev->term_queue, &Request, &DataLength, 
  472.             &DataAddress, 0, DCWW_WAIT, &ElemPriority, (HEV)NULL);
  473.         DosCloseQueue(pmdev->term_queue);
  474.         pmdev->term_queue = (HQUEUE)0;
  475.         /* restart it */
  476.         pm_run_gspmdrv(pmdev);
  477.         DosSleep(2000);    /* give gspmdrv.exe a chance to run */
  478.         rc = DosSelectSession(pmdev->session_id);
  479.           }
  480.           if (rc == ERROR_SMG_SESSION_NOT_FOREGRND)
  481.         DosBeep(400,50);
  482.           else if (rc)
  483.         fprintf(stderr,"pm_output_page: Select Session error code %u\n", rc);
  484.         }
  485.     }
  486.     return code;
  487. }
  488.  
  489. /* Close the pm driver */
  490. int
  491. pm_close(gx_device *dev)
  492. {
  493. APIRET rc;
  494. #ifdef __DLL__
  495.     if (pmdev->dll) {
  496.         (*pgsdll_callback)(GSDLL_DEVICE, (unsigned char *)dev, 0);
  497.     }
  498.     else
  499. #endif
  500.     {
  501.     if (*pmdev->GSVIEW) {
  502.         if (gs_exit_status) {
  503.         ULONG count;
  504.         /* pause so error messages can be read */
  505.         DosResetEventSem(pmdev->next_event, &count);
  506.         DosWriteQueue(pmdev->drv_queue, GS_ERROR, 0, NULL, 0);
  507.         DosWaitEventSem(pmdev->next_event, SEM_INDEFINITE_WAIT);
  508.         DosResetEventSem(pmdev->next_event, &count);
  509.         }
  510.             rc = DosWriteQueue(pmdev->drv_queue, GS_CLOSE, 0, NULL, 0);
  511.             if (rc)
  512.         fprintf(stderr,"pm_close: DosWriteQueue error %d\n",rc);
  513.     }
  514.     else {
  515.         REQUESTDATA Request;
  516.         ULONG DataLength;
  517.         PVOID DataAddress;
  518.         PULONG QueueEntry;
  519.         BYTE ElemPriority;
  520.         /* Close gspmdrv driver */
  521.         DosStopSession(STOP_SESSION_SPECIFIED, pmdev->session_id);
  522.         Request.pid = pmdev->gspid;
  523.         Request.ulData = 0;
  524.         /* wait for termination queue, queue is then closed by session manager */
  525.         DosReadQueue(pmdev->term_queue, &Request, &DataLength, 
  526.         &DataAddress, 0, DCWW_WAIT, &ElemPriority, (HEV)NULL);
  527.     }
  528.     }
  529.     /* release memory */
  530.     DosFreeMem(pmdev->bitmap);
  531.     pmdev->bitmap = (unsigned char *)NULL;
  532.     pmdev->committed = 0;
  533.  
  534.     if (!pmdev->dll) {
  535.     /* close objects */
  536.     if (*pmdev->GSVIEW) {
  537.         DosCloseQueue(pmdev->drv_queue);
  538.         DosCloseEventSem(pmdev->next_event);
  539.     }
  540.     else {
  541.         DosCloseEventSem(pmdev->sync_event);
  542.         /* stop update timer */
  543.         if (pmdev->updating)
  544.         DosStopTimer(pmdev->update_timer);
  545.         pmdev->updating = FALSE;
  546.     }
  547.     }
  548.  
  549.     DosCloseMutexSem(pmdev->bmp_mutex);
  550.     return(0);
  551. }
  552.  
  553.  
  554. /* Map a r-g-b color to the colors available under PM */
  555. gx_color_index
  556. pm_map_rgb_color(gx_device *dev, gx_color_value r, gx_color_value g,
  557.   gx_color_value b)
  558. {
  559.     if (!dev->is_open)
  560.         return 0;
  561.     switch(pmdev->BitsPerPixel) {
  562.       case 24:
  563.         return    ((b >> (gx_color_value_bits - 8)) << 16) +
  564.             ((g >> (gx_color_value_bits - 8)) << 8) +
  565.             ((r >> (gx_color_value_bits - 8)));
  566.       case 16:
  567.       case 15:
  568.         return    (pm_color_value(b) << 16) +
  569.             (pm_color_value(g) << 8) +
  570.             (pm_color_value(r));
  571.       case 8: {
  572.         int i;
  573.         RGB2 *prgb;
  574.         byte cr, cg, cb;
  575.  
  576.         /* map colors to 0->255 in 32 steps */
  577.         cr = pm_color_value(r);
  578.         cg = pm_color_value(g);
  579.         cb = pm_color_value(b);
  580.  
  581.         prgb = (RGB2 *)((PBYTE)pmdev->bmi + pmdev->bmi->cbFix);
  582.         /* search in palette */
  583.         for ( i = 0; i < pmdev->nColors; i++, prgb++ )
  584.         {    if ( !((cr ^ prgb->bRed) & 0xf8) &&
  585.                  !((cg ^ prgb->bGreen) & 0xf8) &&
  586.                  !((cb ^ prgb->bBlue) & 0xf8)
  587.                )
  588.                 return((gx_color_index)i);    /* found it */
  589.         }
  590.  
  591.         /* next try adding it to palette */
  592.         if (i < 230) { /* allow 26 for PM and other apps */
  593.             prgb->bRed = cr;
  594.             prgb->bGreen = cg;
  595.             prgb->bBlue = cb;
  596.             prgb->fcOptions = 0;
  597.             pmdev->nColors = i+1;
  598.             pmdev->bmi->cclrImportant = pmdev->nColors;
  599.                 if (*pmdev->GSVIEW) {
  600.                 APIRET rc;
  601.                 rc = DosWriteQueue(pmdev->drv_queue, GS_PALCHANGE, 0, NULL, 0);
  602.                 if (rc)
  603.                     fprintf(stderr,"pm_sync_output: DosWriteQueue error %d\n",rc);
  604.                 }
  605.             return((gx_color_index)i);    /* return new palette index */
  606.         }
  607.  
  608.         return(gx_no_color_index);  /* not found - dither instead */
  609.         }
  610.       case 4:
  611.         if ((r == g) && (g == b) && (r >= gx_max_color_value / 3 * 2 - 1)
  612.            && (r < gx_max_color_value / 4 * 3))
  613.             return ((gx_color_index)8);    /* light gray */
  614.         return pc_4bit_map_rgb_color(dev, r, g, b);
  615.     }
  616.     return (gx_default_map_rgb_color(dev,r,g,b));
  617. }
  618.  
  619. /* Map a color code to r-g-b. */
  620. int
  621. pm_map_color_rgb(gx_device *dev, gx_color_index color,
  622.   gx_color_value prgb[3])
  623. {
  624. gx_color_value one;
  625.     if (!dev->is_open)
  626.         return 0;
  627.     switch(pmdev->BitsPerPixel) {
  628.       case 24:
  629.       case 16:
  630.       case 15:
  631.         /* untested */
  632.         one = (gx_color_value) (gx_max_color_value / 255);
  633.         prgb[0] = ((color)     & 255) * one;
  634.         prgb[1] = ((color>>8)  & 255) * one;
  635.         prgb[2] = ((color>>16) & 255) * one;
  636.         break;
  637.       case 8:
  638.         one = (gx_color_value) (gx_max_color_value / 255);
  639.         prgb[0] = pmdev->bmi->argbColor[(int)color].bRed * one;
  640.         prgb[1] = pmdev->bmi->argbColor[(int)color].bGreen * one;
  641.         prgb[2] = pmdev->bmi->argbColor[(int)color].bBlue * one;
  642.         break;
  643.       case 4:
  644.         if (color == 8)    /* VGA light gray */
  645.             prgb[0] = prgb[1] = prgb[2] = (gx_max_color_value / 4 * 3);
  646.         else
  647.             pc_4bit_map_color_rgb(dev, color, prgb);
  648.         break;
  649.       default:
  650.         prgb[0] = prgb[1] = prgb[2] = 
  651.             (int)color ? gx_max_color_value : 0;
  652.     }
  653.     return 0;
  654. }
  655.  
  656. #define pmmdev ((gx_device *)&pmdev->mdev)
  657. #define pmmproc(proc) (*dev_proc(&pmdev->mdev, proc))
  658.  
  659. /* Fill a rectangle. */
  660. private int
  661. pm_fill_rectangle(gx_device *dev, int x, int y, int w, int h,
  662.   gx_color_index color)
  663. {
  664.     pmmproc(fill_rectangle)(pmmdev, x, y, w, h, color);
  665.     pm_update((gx_device_pm *)dev);
  666.     return 0;
  667. }
  668.  
  669. /* Copy a monochrome bitmap.  The colors are given explicitly. */
  670. /* Color = gx_no_color_index means transparent (no effect on the image). */
  671. private int
  672. pm_copy_mono(gx_device *dev,
  673.   const byte *base, int sourcex, int raster, gx_bitmap_id id,
  674.   int x, int y, int w, int h,
  675.   gx_color_index zero, gx_color_index one)
  676. {
  677.     pmmproc(copy_mono)(pmmdev, base, sourcex, raster, id,
  678.                      x, y, w, h, zero, one);
  679.     pm_update((gx_device_pm *)dev);
  680.     return 0;
  681. }
  682.  
  683. /* Copy a color pixel map.  This is just like a bitmap, except that */
  684. /* each pixel takes 8 or 4 bits instead of 1 when device driver has color. */
  685. private int
  686. pm_copy_color(gx_device *dev,
  687.   const byte *base, int sourcex, int raster, gx_bitmap_id id,
  688.   int x, int y, int w, int h)
  689. {
  690.     pmmproc(copy_color)(pmmdev, base, sourcex, raster, id,
  691.                       x, y, w, h);
  692.     pm_update((gx_device_pm *)dev);
  693.     return 0;
  694. }
  695.  
  696. /* Get PM parameters */
  697. int
  698. pm_get_params(gx_device *dev, gs_param_list *plist)
  699. {    int code = gx_default_get_params(dev, plist);
  700.     gs_param_string gvs;
  701.     gvs.data = pmdev->GSVIEW, gvs.size = strlen(gvs.data),
  702.       gvs.persistent = false;
  703.     code < 0 ||
  704.     (code = param_write_int(plist, "BitsPerPixel", &pmdev->BitsPerPixel)) < 0 ||
  705.     (code = param_write_int(plist, "UpdateInterval", &pmdev->UpdateInterval)) < 0 ||
  706.     (code = param_write_string(plist, "GSVIEW", &gvs)) < 0;
  707.     return code;
  708. }
  709.  
  710. /* Set PM parameters -- size and resolution. */
  711. /* We implement this ourselves so that we can do it without */
  712. /* closing and opening the device. */
  713. /* Also set BitsPerPixel and GSVIEW if device not open */
  714. int
  715. pm_put_params(gx_device *dev, gs_param_list *plist)
  716. {    gx_device_pm temp_dev;
  717.     int ecode, rcode = 0, code;
  718.     int v;
  719.     gs_param_string s;
  720.     temp_dev = *pmdev;
  721.     ecode = gx_default_put_params_only((gx_device *)&temp_dev, plist);
  722.  
  723.     /* Handle extra parameters */
  724.  
  725.     switch ( code = param_read_string(plist, "GSVIEW", &s) )
  726.     {
  727.     default:
  728.         ecode = code;
  729.     case 1:
  730.         break;
  731.     case 0:
  732.         if ( dev->is_open )
  733.             ecode = gs_error_rangecheck;
  734.         else if ( s.size >= pm_gsview_sizeof )
  735.             ecode = gs_error_limitcheck;
  736.         else
  737.           {    memcpy(temp_dev.GSVIEW, s.data, s.size);
  738.             temp_dev.GSVIEW[s.size] = 0;
  739.             break;
  740.           }
  741.         param_signal_error(plist, "GSVIEW", ecode);
  742.     }
  743.  
  744.     switch ( code = param_read_int(plist, "UpdateInterval", &v) )
  745.     {
  746.     default:
  747.         ecode = code;
  748.     case 1:
  749.         break;
  750.     case 0:
  751.         if ( v < 0 )
  752.             param_signal_error(plist, "UpdateInterval",
  753.                        ecode = gs_error_rangecheck);
  754.         else
  755.             temp_dev.UpdateInterval = v;
  756.     }
  757.  
  758.     switch ( code = param_read_int(plist, "BitsPerPixel", &v) )
  759.     {
  760.     default:
  761.         ecode = code;
  762.     case 1:
  763.         break;
  764.     case 0:
  765.         if ( dev->is_open )
  766.             ecode = gs_error_rangecheck;
  767.         else if ((v!=1) && (v!=4) && (v!=8) && (v!=15) && (v!=16) && (v!=24))
  768.             ecode = gs_error_rangecheck;
  769.         else
  770.           {    code = pm_set_bits_per_pixel(&temp_dev, v);
  771.             if ( code < 0 )
  772.                 ecode = code;
  773.             else
  774.               {    rcode = 1;
  775.                 break;
  776.               }
  777.           }
  778.         param_signal_error(plist, "BitsPerPixel", ecode);
  779.     }
  780.  
  781.     if ( ecode < 0 )
  782.       return ecode;
  783.  
  784.     /* obtain mutex - to prevent gspmdrv from using bitmap */
  785.     /* while we change its size */
  786.     DosRequestMutexSem(pmdev->bmp_mutex, 20000);
  787.     if ( dev->is_open && rcode )
  788.     {    int ccode;
  789.         ccode = pm_alloc_bitmap(pmdev, (gx_device *)&temp_dev);
  790.         if ( ccode < 0 )
  791.         {    pm_alloc_bitmap(pmdev, dev);
  792.             DosReleaseMutexSem(pmdev->bmp_mutex);
  793.             return ccode;
  794.         }
  795.     }
  796.     dev->x_pixels_per_inch = temp_dev.x_pixels_per_inch;
  797.     dev->y_pixels_per_inch = temp_dev.y_pixels_per_inch;
  798.     dev->width = temp_dev.width;
  799.     dev->height = temp_dev.height;
  800.     dev->color_info = temp_dev.color_info;
  801.     pmdev->BitsPerPixel = temp_dev.BitsPerPixel;
  802.     pmdev->UpdateInterval = temp_dev.UpdateInterval;
  803.     strcpy(pmdev->GSVIEW, temp_dev.GSVIEW);
  804.  
  805.     if ( dev->is_open && rcode ) {
  806.         /* need to update bitmap info header also */
  807.         pmdev->bmi->cx = dev->width;
  808.         pmdev->bmi->cy = dev->height;
  809.         /* update bit count and palette */
  810.         pmdev->bmi->cBitCount = dev->color_info.depth;
  811.         pmdev->bmi->cclrUsed = 1<<(pmdev->BitsPerPixel);
  812.         pmdev->bmi->cclrImportant = pmdev->nColors;
  813.         pm_makepalette(pmdev);
  814.         /* erase bitmap - before window gets redrawn */
  815.         (*dev_proc(dev, fill_rectangle))(dev,
  816.         0, 0, dev->width, dev->height,
  817.         pm_map_rgb_color(dev, gx_max_color_value, 
  818.             gx_max_color_value, gx_max_color_value));
  819.         /* cause scroll bars to be redrawn */
  820.         /* need to signal gspmdrv that bitmap size has changed */
  821.         /* or perhaps gspmdrv can check if the bitmap size has */
  822.         /* before each use */
  823.  
  824. #ifdef __DLL__
  825.         if (pmdev->dll)
  826.             (*pgsdll_callback)(GSDLL_SIZE, (unsigned char *)dev,
  827.             (dev->width & 0xffff) + ( (dev->height & 0xffff)<<16) );
  828. #endif
  829.     }
  830.  
  831.     /* release bmp mutex */
  832.     DosReleaseMutexSem(pmdev->bmp_mutex);
  833.     return 0;
  834. }
  835.  
  836. #ifdef __DLL__
  837. /* ------ DLL routines ------ */
  838. /* store at pbitmap the address of the bitmap */
  839. /* device is a pointer to Ghostscript device from GSDLL_DEVICE message */
  840. unsigned long 
  841. gsdll_get_bitmap(unsigned char *device, unsigned char **pbitmap)
  842. {
  843. gx_device *dev = (gx_device *)device;
  844.     *pbitmap =  (unsigned char *)(pmdev->bmi );
  845.     return 0;
  846. }
  847.  
  848. /* Lock the bitmap (so it's size cannot be changed) if flag = TRUE */
  849. /* or unlock the bitmap if flag = FALSE */
  850. /* device is a pointer to Ghostscript device from GSDLL_DEVICE message */
  851. unsigned long 
  852. gsdll_lock_bitmap(unsigned char *device, int flag)
  853. {
  854. gx_device *dev = (gx_device *)device;
  855. APIRET rc;
  856.     if (flag)
  857.     rc = DosRequestMutexSem(pmdev->bmp_mutex, 60000);
  858.     else
  859.     rc = DosReleaseMutexSem(pmdev->bmp_mutex);
  860.     return rc;
  861. }
  862.  
  863. #endif /* __DLL__ */
  864.  
  865. /* ------ Internal routines ------ */
  866.  
  867. #undef pmdev
  868.  
  869.  
  870. /* start gspmdrv.exe */
  871. private int
  872. pm_run_gspmdrv(gx_device_pm *pmdev)
  873. {
  874.     int ccode;
  875.     PCHAR pdrvname = "gspmdrv.exe";
  876.     CHAR error_message[256];
  877.     CHAR term_queue_name[128];
  878.     CHAR id[128];
  879.     CHAR arg[1024];
  880.     STARTDATA sdata;
  881.     APIRET rc;
  882.     PTIB pptib;
  883.     PPIB pppib;
  884.     CHAR progname[256];
  885.     PCHAR tail;
  886.  
  887.     sprintf(id, ID_NAME, pmdev->gspid, (ULONG)pmdev);
  888.  
  889.     /* Create termination queue - used to find out when gspmdrv terminates */
  890.     sprintf(term_queue_name, "\\QUEUES\\TERMQ_%s", id);
  891.     if (DosCreateQueue(&(pmdev->term_queue), QUE_FIFO, term_queue_name)) {
  892.         fprintf(stderr,"pm_run_gspmdrv: failed to create termination queue\n");
  893.         return gs_error_limitcheck;
  894.     }
  895.  
  896.     /* get full path to gsos2.exe and hence path to gspmdrv.exe */
  897.     if ( (rc = DosGetInfoBlocks(&pptib, &pppib)) != 0 ) {
  898.         fprintf(stderr,"pm_run_gspmdrv: Couldn't get module handle, rc = %d\n", rc);
  899.         return gs_error_limitcheck;
  900.     }
  901.     if ( (rc = DosQueryModuleName(pppib->pib_hmte, 256, progname)) != 0 ) {
  902.         fprintf(stderr,"pm_run_gspmdrv: Couldn't get module name, rc = %d\n", rc);
  903.         return gs_error_limitcheck;
  904.     }
  905.     if ((tail = strrchr(progname,'\\')) != (PCHAR)NULL)
  906.     {
  907.         tail++;
  908.         *tail = '\0';
  909.     }
  910.     else 
  911.         tail = progname;
  912.     strcat(progname, pdrvname);
  913.  
  914.     /* Open the PM driver session gspmdrv.exe */
  915.     /* arguments are: */
  916.     /*  (1) -d (display) option */
  917.     /*  (2) id string */
  918.     sprintf(arg, "-d %s", id);
  919.  
  920.     /* because gspmdrv.exe is a different EXE type to gs.exe, 
  921.      * we must use start session not DosExecPgm() */
  922.     sdata.Length = sizeof(sdata);
  923.     sdata.Related = SSF_RELATED_CHILD;    /* to be a child  */
  924.     sdata.FgBg = SSF_FGBG_BACK;        /* start in background */
  925.     sdata.TraceOpt = 0;
  926.     sdata.PgmTitle = "Ghostscript PM driver session";
  927.     sdata.PgmName = progname;
  928.     sdata.PgmInputs = arg;
  929.     sdata.TermQ = term_queue_name;
  930.     sdata.Environment = pppib->pib_pchenv;    /* use Parent's environment */
  931.     sdata.InheritOpt = 0;            /* Can't inherit from parent because different sesison type */
  932.     sdata.SessionType = SSF_TYPE_DEFAULT;    /* default is PM */
  933.     sdata.IconFile = NULL;
  934.     sdata.PgmHandle = 0;
  935.     sdata.PgmControl = 0;
  936.     sdata.InitXPos = 0;
  937.     sdata.InitYPos = 0;
  938.     sdata.InitXSize = 0;
  939.     sdata.InitYSize = 0;
  940.     sdata.ObjectBuffer = error_message;
  941.     sdata.ObjectBuffLen = sizeof(error_message);
  942.  
  943.     rc = DosStartSession(&sdata, &pmdev->session_id, &pmdev->process_id);
  944.     if (rc == ERROR_FILE_NOT_FOUND) {
  945.         sdata.PgmName = pdrvname;
  946.         rc = DosStartSession(&sdata, &pmdev->session_id, &pmdev->process_id);
  947.     }
  948.     if (rc) {
  949.         fprintf(stderr,"pm_run_gspmdrv: failed to run %s, rc = %d\n", sdata.PgmName, rc);
  950.         fprintf(stderr,"pm_run_gspmdrv: error_message: %s\n", error_message);
  951.         return gs_error_limitcheck;
  952.     }
  953.  
  954.     return 0;
  955.  
  956. }
  957.  
  958. /* Allocate the backing bitmap. */
  959. private int
  960. pm_alloc_bitmap(gx_device_pm *pmdev, gx_device *param_dev)
  961. {
  962.     gx_device_memory mdev;
  963.     byte *base;
  964.     ulong data_size;
  965.     uint ptr_size;
  966.     uint pal_size;
  967.     uint raster;
  968.     ULONG rc;
  969.     ULONG needed;
  970.  
  971.     /* Finish initializing the bitmap. */
  972.  
  973.     gs_make_mem_device(&mdev, gdev_mem_device_for_bits(pmdev->color_info.depth), 0, 0);
  974.     mdev.width = param_dev->width;
  975.     mdev.height = param_dev->height;
  976.     mdev.target = (gx_device *)pmdev;
  977.     /* BMP files need width rounded up so that a scan line is */
  978.     /* a multiple of 4 bytes. */
  979.     /* This is currently done by gdev_mem_raster(). */
  980.     /* It may be better to do it here explicitly in case */
  981.     /* gdev_mem_raster changes. */
  982.     raster = gdev_mem_raster(&mdev);
  983.     data_size = (ulong)raster * mdev.height;
  984.     ptr_size = sizeof(byte **) * mdev.height;
  985.     pal_size = pm_palette_size(pmdev);
  986.     needed = pmdev->bmi->cbFix + pal_size + data_size + ptr_size;
  987.     /* round up to page boundary */
  988.     needed = (needed + MIN_COMMIT - 1) & (~(MIN_COMMIT-1));
  989.     if (needed > pmdev->committed) {
  990.         /* commit more memory */
  991.         if (rc = DosSetMem(pmdev->bitmap + pmdev->committed, 
  992.             needed - pmdev->committed, 
  993.             PAG_COMMIT | PAG_DEFAULT)) {
  994.             fprintf(stderr,"No memory in pm_alloc_bitmap, rc = %d\n",rc);
  995.             return gs_error_limitcheck;
  996.         }
  997.         pmdev->committed = needed;
  998.     }
  999.     /* Shared memory can't be decommitted */
  1000.  
  1001.     /* Nothing can go wrong now.... */
  1002.     base = pmdev->bitmap + pmdev->bmi->cbFix + pm_palette_size(pmdev);
  1003.     pmdev->mdev = mdev;
  1004.     pmdev->mdev.base = (byte *)base;
  1005.     pmmproc(open_device)((gx_device *)&pmdev->mdev);
  1006.     pmdev->bmi->cbImage = data_size;
  1007.     return 0;
  1008. }
  1009.  
  1010. private void
  1011. pm_makepalette(gx_device_pm *pmdev)
  1012. {
  1013.     int i, val;
  1014.     RGB2 *argb = (RGB2 *)( (PBYTE)pmdev->bmi + pmdev->bmi->cbFix );
  1015.     if (pmdev->BitsPerPixel > 8)
  1016.         return;    /* these don't use a palette */
  1017.  
  1018.     for (i=0; i<pmdev->nColors; i++) {
  1019.       switch (pmdev->nColors) {
  1020.         case 64:
  1021.           /* colors are rrggbb */
  1022.           argb[i].bRed   = ((i & 0x30)>>4)*85;
  1023.           argb[i].bGreen = ((i & 0xC)>>2)*85;
  1024.           argb[i].bBlue  = (i & 3)*85;
  1025.           argb[i].fcOptions = 0;
  1026.           /* zero unused entries */
  1027.           argb[i+64].bRed = argb[i+64].bGreen = argb[i+64].bBlue  = 0;
  1028.           argb[i+64].fcOptions = 0;
  1029.           argb[i+128].bRed = argb[i+128].bGreen = argb[i+128].bBlue  = 0;
  1030.           argb[i+128].fcOptions = 0;
  1031.           argb[i+192].bRed = argb[i+192].bGreen = argb[i+192].bBlue  = 0;
  1032.           argb[i+192].fcOptions = 0;
  1033.           break;
  1034.         case 16:
  1035.           /* colors are irgb */
  1036.           val = (i & 8 ? 255 : 128);
  1037.           argb[i].bRed   = i & 4 ? val : 0;
  1038.           argb[i].bGreen = i & 2 ? val : 0;
  1039.           argb[i].bBlue  = i & 1 ? val : 0;
  1040.           if (i == 8) {    /* light gray */
  1041.               argb[i].bRed   = 
  1042.               argb[i].bGreen = 
  1043.               argb[i].bBlue  = 192;
  1044.               argb[i].fcOptions = 0;
  1045.           }
  1046.           break;
  1047.         case 2:
  1048.           argb[i].bRed =
  1049.             argb[i].bGreen =
  1050.             argb[i].bBlue = (i ? 255 : 0);
  1051.           argb[i].fcOptions = 0;
  1052.           break;
  1053.       }
  1054.     }
  1055. }
  1056.  
  1057.  
  1058. /* Cause display to be updated periodically */
  1059. private void 
  1060. pm_update(gx_device_pm *pmdev)
  1061. {
  1062.     if (pmdev->updating)
  1063.         return;
  1064.     if (!pmdev->UpdateInterval)
  1065.         return;
  1066.     if (*pmdev->GSVIEW) {
  1067.         APIRET rc;
  1068.         rc = DosWriteQueue(pmdev->drv_queue, GS_UPDATING, 0, NULL, 0);
  1069.             if (rc)
  1070.         fprintf(stderr,"pm_update: DosWriteQueue error %d\n",rc);
  1071.     }
  1072.     else {
  1073.         DosStartTimer(pmdev->UpdateInterval, (HSEM)pmdev->sync_event, 
  1074.         &pmdev->update_timer);
  1075.     }
  1076.     pmdev->updating = TRUE;
  1077. }
  1078.  
  1079. private uint
  1080. pm_set_bits_per_pixel(gx_device_pm *pmdev, int bpp)
  1081. {
  1082. static const gx_device_color_info pm_24bit_color = dci_color(24,255,255);
  1083. static const gx_device_color_info pm_16bit_color = dci_color(24,31,31);
  1084. static const gx_device_color_info pm_8bit_color = dci_color(8,31,4);
  1085. static const gx_device_color_info pm_4bit_color = dci_pc_4bit;
  1086. static const gx_device_color_info pm_2color = dci_black_and_white;
  1087.     switch (bpp) {
  1088.         case 24:
  1089.         pmdev->color_info = pm_24bit_color;
  1090.         pmdev->nColors = (1<<24);
  1091.         break;
  1092.         case 16:
  1093.         case 15:
  1094.         /* bitmap is stored as 24 bits/pixel */
  1095.         pmdev->color_info = pm_16bit_color;
  1096.         pmdev->nColors = 32768;
  1097.         break;
  1098.         case 8:
  1099.         /* use 64 static colors and 166 dynamic colors from 8 planes */
  1100.         pmdev->color_info = pm_8bit_color;
  1101.         pmdev->nColors = 64;
  1102.         break;
  1103.         case 4:
  1104.         pmdev->color_info = pm_4bit_color;
  1105.         pmdev->nColors = 16;
  1106.         break;
  1107.         case 1:
  1108.         pmdev->color_info = pm_2color;
  1109.         pmdev->nColors = 2;
  1110.         break;
  1111.         default:
  1112.         return (gs_error_rangecheck);
  1113.     }
  1114.     pmdev->BitsPerPixel = bpp;
  1115.     return 0;
  1116. }
  1117.  
  1118. /* return length of BMP palette in bytes */
  1119. private uint
  1120. pm_palette_size(gx_device_pm *pmdev)
  1121. {
  1122.     switch(pmdev->color_info.depth) {
  1123.         case 24:
  1124.         return 0;
  1125.         case 8:
  1126.         return 256*sizeof(RGB2);
  1127.         case 4:
  1128.         return 16*sizeof(RGB2);
  1129.     }
  1130.     /* must be two color */
  1131.     return 2*sizeof(RGB2);
  1132. }
  1133.  
  1134. /* This is used for testing */
  1135. /* Write out a BMP file to "out.bmp" */
  1136. private void 
  1137. pm_write_bmp(gx_device_pm *pmdev)
  1138. {
  1139.     BITMAPFILEHEADER2 bmfh;
  1140.     uint bmfh_length = sizeof(BITMAPFILEHEADER2) - sizeof(BITMAPINFOHEADER2);
  1141.     uint length;    /* bitmap length */
  1142.     ULONG fh;    /* file handle */
  1143.     ULONG action;
  1144.     ULONG count;
  1145.     bmfh.usType = 0x4d42;    /* "BM" */
  1146.     length = pmdev->bmi->cbFix + pm_palette_size(pmdev)
  1147.        + ( (gdev_mem_raster(&pmdev->mdev) * pmdev->mdev.height) );
  1148.     bmfh.cbSize = bmfh_length + length;
  1149.     bmfh.xHotspot = bmfh.yHotspot = 0;
  1150.     bmfh.offBits = bmfh_length + pmdev->bmi->cbFix + pm_palette_size(pmdev);
  1151.     if (DosOpen("out.bmp",    /* filename */
  1152.         &fh,        /* pointer to handle */
  1153.         &action,    /* pointer to result */
  1154.         0,        /* initial length */
  1155.         FILE_NORMAL,    /* normal file */
  1156.         OPEN_ACTION_CREATE_IF_NEW | OPEN_ACTION_REPLACE_IF_EXISTS,
  1157.         OPEN_ACCESS_WRITEONLY | OPEN_SHARE_DENYREADWRITE,
  1158.         0)) {
  1159.         fprintf(stderr,"error opening out.bmp\n");
  1160.         return;
  1161.     }
  1162.     if (DosWrite(fh, (PBYTE)&bmfh, bmfh_length, &count))
  1163.         fprintf(stderr,"error writing header for out.bmp\n");
  1164.     if (DosWrite(fh, pmdev->bitmap, length, &count))
  1165.         fprintf(stderr,"error writing out.bmp\n");
  1166.     if (DosClose(fh))
  1167.         fprintf(stderr,"error closing out.bmp\n");
  1168. }
  1169.