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

  1. /* Copyright (C) 1989, 1992, 1993 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. /* gsht.c */
  20. /* Halftone operators for Ghostscript library */
  21. #include "memory_.h"
  22. #include <stdlib.h>        /* for qsort */
  23. #include "gx.h"
  24. #include "gserrors.h"
  25. #include "gsstruct.h"
  26. #include "gzstate.h"
  27. #include "gxdevice.h"            /* for gzht.h */
  28. #include "gzht.h"
  29.  
  30. /*** Big memory machines ***/
  31. #define max_tile_bytes_LARGE 4096
  32. /*** Small memory machines ***/
  33. #define max_tile_bytes_SMALL 512
  34.  
  35. #if arch_ints_are_short
  36. #  define max_tile_cache_bytes max_tile_bytes_SMALL
  37. #else
  38. #  define max_tile_cache_bytes max_tile_bytes_LARGE
  39. #endif
  40.  
  41. /* Forward declarations */
  42. private int process_threshold(P3(gx_ht_order *, gs_state *,
  43.   gs_threshold_halftone *));
  44. private int process_screen(P3(gs_screen_enum *, gs_state *,
  45.   gs_screen_halftone *));
  46. private void gx_sort_ht_order(P2(gx_ht_bit *, uint));
  47.  
  48. /* Structure types */
  49. public_st_ht_order();
  50. private_st_ht_order_component();
  51. private_st_ht_order_comp_element();
  52. public_st_halftone();
  53. public_st_device_halftone();
  54.  
  55. /* GC procedures */
  56.  
  57. #define hptr ((gs_halftone *)vptr)
  58.  
  59. private ENUM_PTRS_BEGIN(halftone_enum_ptrs) return 0;
  60.     case 0:
  61.         switch ( hptr->type )
  62.         {
  63.         case ht_type_threshold:
  64.           *pep = (void *)hptr->params.threshold.thresholds; break; /* discard const */
  65.         case ht_type_multiple:
  66.           *pep = hptr->params.multiple.components; break;
  67.         default:
  68.           return 0;
  69.         }
  70.         break;
  71. ENUM_PTRS_END
  72.  
  73. private RELOC_PTRS_BEGIN(halftone_reloc_ptrs) {
  74.     switch ( hptr->type )
  75.       {
  76.       case ht_type_threshold:
  77.         RELOC_PTR(gs_halftone, params.threshold.thresholds);
  78.         break;
  79.       case ht_type_multiple:
  80.         RELOC_PTR(gs_halftone, params.multiple.components);
  81.         break;
  82.       case ht_type_none:
  83.       case ht_type_screen:
  84.       case ht_type_colorscreen:
  85.       case ht_type_spot:
  86.         break;
  87.       }
  88. } RELOC_PTRS_END
  89.  
  90. #undef hptr
  91.  
  92. /* setscreen */
  93. int
  94. gs_setscreen(gs_state *pgs, gs_screen_halftone *phsp)
  95. {    gs_screen_enum senum;
  96.     int code = process_screen(&senum, pgs, phsp);
  97.     if ( code < 0 )
  98.         return code;
  99.     return gs_screen_install(&senum);
  100. }
  101.  
  102. /* currentscreen */
  103. int
  104. gs_currentscreen(gs_state *pgs, gs_screen_halftone *phsp)
  105. {    switch ( pgs->halftone->type )
  106.     {
  107.     case ht_type_screen:
  108.         *phsp = pgs->halftone->params.screen;
  109.         return 0;
  110.     case ht_type_colorscreen:
  111.         *phsp = pgs->halftone->params.colorscreen.screens.colored.gray;
  112.         return 0;
  113.     default:
  114.         return_error(gs_error_undefined);
  115.     }
  116. }
  117.  
  118. /* setcolorscreen */
  119. int
  120. gs_setcolorscreen(gs_state *pgs, gs_colorscreen_halftone *pht)
  121. {    gs_halftone ht;
  122.     ht.type = ht_type_colorscreen;
  123.     ht.params.colorscreen = *pht;
  124.     return gs_sethalftone(pgs, &ht);
  125. }
  126.  
  127. /* currentcolorscreen */
  128. int
  129. gs_currentcolorscreen(gs_state *pgs, gs_colorscreen_halftone *pht)
  130. {    int code;
  131.     switch ( pgs->halftone->type )
  132.     {
  133.     case ht_type_colorscreen:
  134.         *pht = pgs->halftone->params.colorscreen;
  135.         return 0;
  136.     default:
  137.         code = gs_currentscreen(pgs, &pht->screens.colored.gray);
  138.         if ( code < 0 )
  139.             return code;
  140.         pht->screens.colored.red = pht->screens.colored.gray;
  141.         pht->screens.colored.green = pht->screens.colored.gray;
  142.         pht->screens.colored.blue = pht->screens.colored.gray;
  143.         return 0;
  144.     }
  145. }
  146.  
  147. /* Set the halftone in the graphics state. */
  148. int
  149. gs_sethalftone(gs_state *pgs, gs_halftone *pht)
  150. {    gx_device_halftone dev_ht;
  151.     int code = gs_sethalftone_prepare(pgs, pht, &dev_ht);
  152.     if ( code < 0 )
  153.         return code;
  154.     return gx_ht_install(pgs, pht, &dev_ht);
  155. }
  156. /* Prepare the halftone, but don't install it. */
  157. int
  158. gs_sethalftone_prepare(gs_state *pgs, gs_halftone *pht,
  159.   gx_device_halftone *pdht)
  160. {    gs_memory_t *mem = pgs->memory;
  161.     gx_ht_order_component *pocs = 0;
  162.     int code = 0;
  163.     /* Currently we disregard the transfer function override. */
  164.     switch ( pht->type )
  165.     {
  166.     case ht_type_colorscreen:
  167.     {    gs_screen_halftone *phc =
  168.             pht->params.colorscreen.screens.indexed;
  169.         static const gs_ht_separation_name cnames[4] =
  170.         { gs_ht_separation_Default, gs_ht_separation_Red,
  171.           gs_ht_separation_Green, gs_ht_separation_Blue
  172.         };
  173.         static const int cindex[4] = { 3, 0, 1, 2 };
  174.         int i;
  175.         pocs = gs_alloc_struct_array(mem, 4,
  176.                          gx_ht_order_component,
  177.                          &st_ht_order_component_element,
  178.                          "gs_sethalftone");
  179.         if ( pocs == 0 )
  180.             return_error(gs_error_VMerror);
  181.         for ( i = 0; i < 4; i++ )
  182.         {    gs_screen_enum senum;
  183.             int ci = cindex[i];
  184.             gx_ht_order_component *poc = &pocs[i];
  185.             code = process_screen(&senum, pgs, &phc[ci]);
  186.             if ( code < 0 )
  187.                 break;
  188. #define sorder senum.order
  189.             poc->corder = sorder;
  190.             poc->cname = cnames[i];
  191.             if ( i == 0 )        /* Gray = Default */
  192.                 pdht->order = sorder;
  193.             else
  194.             {    uint tile_bytes =
  195.                     sorder.raster * sorder.height;
  196.                 uint num_tiles =
  197.                     max_tile_cache_bytes / tile_bytes + 1;
  198.                 gx_ht_cache *pcache =
  199.                     gx_ht_alloc_cache(mem, num_tiles,
  200.                         tile_bytes * num_tiles);
  201.                 if ( pcache == 0 )
  202.                   {    code = gs_note_error(gs_error_VMerror);
  203.                     break;
  204.                   }
  205.                 poc->corder.cache = pcache;
  206.                 gx_ht_init_cache(pcache, &poc->corder);
  207.             }
  208. #undef sorder
  209.         }
  210.         if ( code < 0 )
  211.           break;
  212.         pdht->components = pocs;
  213.         pdht->num_comp = 4;
  214.     }    break;
  215.     case ht_type_spot:
  216.     {    gs_screen_enum senum;
  217.         code = process_screen(&senum, pgs, &pht->params.spot.screen);
  218.         if ( code < 0 )
  219.             return code;
  220.         pdht->order = senum.order;
  221.         pdht->components = 0;
  222.     }    break;
  223.     case ht_type_threshold:
  224.         code = process_threshold(&pdht->order, pgs,
  225.                      &pht->params.threshold);
  226.         if ( code < 0 )
  227.             return code;
  228.         pdht->components = 0;
  229.         break;
  230.     case ht_type_multiple:
  231.     {    uint count = pht->params.multiple.num_comp;
  232.         bool have_Default = false;
  233.         uint i;
  234.         gs_halftone_component *phc = pht->params.multiple.components;
  235.         gx_ht_order_component *poc_next;
  236.         pocs = gs_alloc_struct_array(mem, count,
  237.                          gx_ht_order_component,
  238.                          &st_ht_order_component_element,
  239.                          "gs_sethalftone");
  240.         if ( pocs == 0 )
  241.             return_error(gs_error_VMerror);
  242.         poc_next = pocs + 1;
  243.         for ( i = 0; i < count; i++, phc++ )
  244.         {    gx_ht_order_component *poc;
  245.             if ( phc->cname == gs_ht_separation_Default )
  246.             {    if ( have_Default )
  247.                 {    /* Duplicate Default */
  248.                     code = gs_note_error(gs_error_rangecheck);
  249.                     break;
  250.                 }
  251.                 poc = pocs;
  252.                 have_Default = true;
  253.             }
  254.             else if ( i == count - 1 && !have_Default )
  255.             {    /* No Default */
  256.                 code = gs_note_error(gs_error_rangecheck);
  257.                 break;
  258.             }
  259.             else
  260.                 poc = poc_next++;
  261.             poc->cname = phc->cname;
  262.             switch ( phc->type )
  263.             {
  264.             case ht_type_spot:
  265.             {    gs_screen_enum senum;
  266.                 code = process_screen(&senum, pgs,
  267.                         &phc->params.spot.screen);
  268.                 if ( code < 0 )
  269.                     break;
  270.                 poc->corder = senum.order;
  271.             }    break;
  272.             case ht_type_threshold:
  273.                 code = process_threshold(&poc->corder, pgs,
  274.                         &phc->params.threshold);
  275.                 if ( code < 0 )
  276.                     break;
  277.                 break;
  278.             default:
  279.                 code = gs_note_error(gs_error_rangecheck);
  280.                 break;
  281.             }
  282.             if ( code < 0 )
  283.               break;
  284.             if ( poc != pocs )
  285.             {    gx_ht_cache *pcache =
  286.                     gx_ht_alloc_cache(mem, 1,
  287.                         poc->corder.raster *
  288.                         poc->corder.height);
  289.                 if ( pcache == 0 )
  290.                   {    code = gs_note_error(gs_error_VMerror);
  291.                     break;
  292.                   }
  293.                 poc->corder.cache = pcache;
  294.                 gx_ht_init_cache(pcache, &poc->corder);
  295.             }
  296.         }
  297.         if ( code < 0 )
  298.           break;
  299.         pdht->order = pocs[0].corder;        /* Default */
  300.         if ( count == 1 )
  301.         {    /* We have only a Default; */
  302.             /* we don't need components. */
  303.             gs_free_object(mem, pocs, "gs_sethalftone");
  304.             pdht->components = 0;
  305.         }
  306.         else
  307.         {    pdht->components = pocs;
  308.             pdht->num_comp = count;
  309.         }
  310.     }    break;
  311.     default:
  312.         return_error(gs_error_rangecheck);
  313.     }
  314.     if ( code < 0 )
  315.       gs_free_object(mem, pocs, "gs_sethalftone");
  316.     return code;
  317. }
  318.  
  319. /* Return the halftone in the graphics state. */
  320. int
  321. gs_currenthalftone(gs_state *pgs, gs_halftone *pht)
  322. {    *pht = *pgs->halftone;
  323.     return 0;
  324. }
  325.  
  326. /* sethalftonephase */
  327. int
  328. gs_sethalftonephase(gs_state *pgs, int x, int y)
  329. {    pgs->ht_phase.x = x;
  330.     pgs->ht_phase.y = y;
  331.     gx_ht_set_phase(pgs);
  332.     return 0;
  333. }
  334.  
  335. /* currenthalftonephase */
  336. int
  337. gs_currenthalftonephase(gs_state *pgs, gs_int_point *pphase)
  338. {    *pphase = pgs->ht_phase;
  339.     return 0;
  340. }
  341.  
  342. /* ------ Internal routines ------ */
  343.  
  344. /* Process one screen plane. */
  345. private int
  346. process_screen(gs_screen_enum *penum, gs_state *pgs, gs_screen_halftone *phsp)
  347. {    gs_point pt;
  348.     int code = gs_screen_init(penum, pgs, phsp);
  349.     if ( code < 0 ) return code;
  350.     while ( (code = gs_screen_currentpoint(penum, &pt)) == 0 )
  351.         if ( (code = gs_screen_next(penum, (*phsp->spot_function)(pt.x, pt.y))) < 0 )
  352.             return code;
  353.     return 0;
  354. }
  355.  
  356. /* Process a threshold plane. */
  357. private int
  358. process_threshold(gx_ht_order *porder, gs_state *pgs,
  359.   gs_threshold_halftone *phtp)
  360. {    int code = gx_ht_init_order(porder, phtp->width, phtp->height,
  361.                     256, pgs->memory);
  362.     if ( code < 0 )
  363.         return code;
  364.     gx_ht_construct_threshold_order(porder, phtp->thresholds);
  365.     return 0;
  366. }
  367.  
  368. /* Compute the negated halftone phase mod the tile size. */
  369. /* This is the displacement of the tile relative to the device coordinates. */
  370. private void near
  371. order_set_phase(register gx_ht_order *porder, gs_state *pgs)
  372. {    if ( porder->width == 0 )
  373.         porder->phase.x = 0;
  374.     else
  375.     {    if ( (porder->phase.x = -pgs->ht_phase.x % porder->width) < 0 )
  376.             porder->phase.x += porder->width;
  377.     }
  378.     if ( porder->height == 0 )
  379.         porder->phase.y = 0;
  380.     else
  381.     {    if ( (porder->phase.y = -pgs->ht_phase.y % porder->height) < 0 )
  382.             porder->phase.y += porder->height;
  383.     }
  384. }
  385. void
  386. gx_ht_set_phase(gs_state *pgs)
  387. {    gx_device_halftone *pdht = pgs->dev_ht;
  388.     order_set_phase(&pdht->order, pgs);
  389.     if ( pdht->components != 0 )
  390.     {    uint i;
  391.         for ( i = 0; i < pdht->num_comp; i++ )
  392.             order_set_phase(&pdht->components[i].corder, pgs);
  393.     }
  394. }
  395.  
  396. /* Initialize a halftone order. */
  397. int
  398. gx_ht_init_order(register gx_ht_order *porder, uint width, uint height,
  399.   uint num_levels, gs_memory_t *mem)
  400. {    uint size = width * height;
  401.     uint i;
  402.     gx_ht_order order;
  403.     order = *porder;
  404.     order.width = width;
  405.     order.height = height;
  406.     order.raster = bitmap_raster(width);
  407.     order.num_levels = num_levels;
  408.     order.num_bits = size;
  409.     order.levels =
  410.       (uint *)gs_alloc_byte_array(mem, num_levels, sizeof(uint),
  411.                       "ht order(levels)");
  412.     order.bits =
  413.       (gx_ht_bit *)gs_alloc_byte_array(mem, size, sizeof(gx_ht_bit),
  414.                        "ht order(bits)");
  415.     if ( order.levels == 0 || order.bits == 0 )
  416.     {    gs_free_object(mem, order.bits, "ht order(bits)");
  417.         gs_free_object(mem, order.levels, "ht order(levels)");
  418.         return_error(gs_error_VMerror);
  419.     }
  420.     order.cache = 0;
  421.     /* Tag each sample with its index, for sorting. */
  422.     for ( i = 0; i < size; i++ )
  423.       order.bits[i].offset = i;
  424.     *porder = order;
  425.     return 0;
  426. }
  427.  
  428. /* Compare keys ("masks", actually sample values) for qsort. */
  429. private int
  430. compare_masks(const void *p1, const void *p2)
  431. {    ht_mask_t m1 = ((const gx_ht_bit *)p1)->mask;
  432.     ht_mask_t m2 = ((const gx_ht_bit *)p2)->mask;
  433.     return (m1 < m2 ? -1 : m1 > m2 ? 1 : 0);
  434. }
  435. /* Sort the haltone order by sample value. */
  436. private void
  437. gx_sort_ht_order(gx_ht_bit *recs, uint N)
  438. {    qsort((void *)recs, N, sizeof(*recs), compare_masks);
  439. #ifdef DEBUG
  440. if ( gs_debug_c('h') )
  441.     {    uint i;
  442.         dprintf("[h]Sorted samples:\n");
  443.         for ( i = 0; i < N; i++ )
  444.             dprintf3("%5u: %5u: %5u\n",
  445.                  i, recs[i].offset, recs[i].mask);
  446.     }
  447. #endif
  448. }
  449.  
  450. /* Construct the halftone order from a sampled spot function. */
  451. /* Only width x strip samples have been filled in; */
  452. /* we must replicate the resulting sorted order vertically, */
  453. /* shifting it by shift each time. */
  454. void
  455. gx_ht_construct_spot_order(gx_ht_order *porder, uint strip, uint shift)
  456. {    uint *levels = porder->levels;
  457.     uint num_levels = porder->num_levels;    /* = width x strip */
  458.     uint width = porder->width;
  459.     uint height = porder->height;
  460.     uint copies = height / strip;
  461.     gx_ht_bit *bits = porder->bits;
  462.     gx_ht_bit *bp = bits + porder->num_bits - 1;
  463.     uint i;
  464.     gx_sort_ht_order(bits, porder->num_levels);
  465.     /* Replicate the sorted order vertically. */
  466.     for ( i = num_levels; i > 0; )
  467.     {    uint offset = bits[--i].offset;
  468.         uint x = offset % height;
  469.         uint hy = offset - x;
  470.         uint k;
  471.         levels[i] = i * copies;
  472.         for ( k = 0; k < copies;
  473.               k++, bp--, hy += num_levels, x = (x + shift) % width
  474.             )
  475.             bp->offset = hy + x;
  476.     }
  477.     gx_ht_construct_bits(porder);
  478. }
  479.  
  480. /* Construct the halftone order from a threshold array. */
  481. void
  482. gx_ht_construct_threshold_order(gx_ht_order *porder, const byte *thresholds)
  483. {    uint size = porder->num_bits;
  484.     uint *levels = porder->levels;
  485.     gx_ht_bit *bits = porder->bits;
  486.     uint i, j;
  487.     for ( i = 0; i < size; i++ )
  488.         bits[i].mask = max(1, thresholds[i]);
  489.     gx_sort_ht_order(bits, size);
  490.     /* We want to set levels[j] to the lowest value of i */
  491.     /* such that bits[i].mask > j. */
  492.     for ( i = 0, j = 0; i < size; i++ )
  493.     {    if ( bits[i].mask != j )
  494.         {    if_debug3('h', "[h]levels[%u..%u] = %u\n",
  495.                   j, (uint)bits[i].mask, i);
  496.             while ( j < bits[i].mask )
  497.                 levels[j++] = i;
  498.         }
  499.     }
  500.     while ( j < 256 )
  501.         levels[j++] = size;
  502.     gx_ht_construct_bits(porder);
  503. }
  504.  
  505. /* Construct offset/masks from the whitening order. */
  506. /* porder->bits[i].offset contains the index of the bit position */
  507. /* that is i'th in the whitening order. */
  508. void
  509. gx_ht_construct_bits(gx_ht_order *porder)
  510. {    uint width = porder->width;
  511.     uint size = porder->num_bits;
  512.     gx_ht_bit *bits = porder->bits;
  513.     uint i;
  514.     gx_ht_bit *phb;
  515.     byte *pb;
  516.     uint padding = porder->raster * 8 - width;
  517.     for ( i = 0, phb = bits; i < size; i++, phb++ )
  518.     {    int pix = phb->offset;
  519.         ht_mask_t mask;
  520.         pix += pix / width * padding;
  521.         phb->offset = (pix >> 3) & -sizeof(mask);
  522.         mask = (ht_mask_t)1 << (~pix & (ht_mask_bits - 1));
  523.         /* Replicate the mask bits. */
  524.         pix = ht_mask_bits - width;
  525.         while ( (pix -= width) >= 0 )
  526.             mask |= mask >> width;
  527.         /* Store the mask, reversing bytes if necessary. */
  528.         phb->mask = 0;
  529.         for ( pb = (byte *)&phb->mask + (sizeof(mask) - 1);
  530.               mask != 0;
  531.               mask >>= 8, pb--
  532.             )
  533.             *pb = (byte)mask;
  534.     }
  535. #ifdef DEBUG
  536. if ( gs_debug_c('h') )
  537.        {    dprintf1("[h]Halftone order bits 0x%lx:\n", (ulong)bits);
  538.         for ( i = 0, phb = bits; i < size; i++, phb++ )
  539.             dprintf3("%4d: %u:0x%lx\n", i, phb->offset,
  540.                  (ulong)phb->mask);
  541.        }
  542. #endif
  543. }
  544.  
  545. /* Install a new halftone in the graphics state. */
  546. int
  547. gx_ht_install(gs_state *pgs, const gs_halftone *pht,
  548.   const gx_device_halftone *pdht)
  549. {    gx_device_halftone *pgdht = pgs->dev_ht;
  550.     if ( (ulong)pdht->order.raster * pdht->order.height >
  551.            pgs->ht_cache->bits_size
  552.        )
  553.         return_error(gs_error_limitcheck);
  554.     *pgs->halftone = *pht;
  555.     *pgdht = *pdht;
  556.     gx_ht_set_phase(pgs);
  557.     /* Clear the cache, to avoid confusion in case the address of */
  558.     /* a new order vector matches that of a (deallocated) old one. */
  559.     gx_ht_clear_cache(pgs->ht_cache);
  560.     /* Set the color_indices according to the device color_info. */
  561.     if ( pdht->components != 0 )
  562.     {    static const gs_ht_separation_name dcnames[5][4] =
  563.         {    { gs_ht_separation_Default },    /* not used */
  564.             { gs_ht_separation_Gray, gs_ht_separation_Default,
  565.               gs_ht_separation_Default, gs_ht_separation_Default
  566.             },
  567.             { gs_ht_separation_Default },    /* not used */
  568.             { gs_ht_separation_Red, gs_ht_separation_Green,
  569.               gs_ht_separation_Blue, gs_ht_separation_Default
  570.             },
  571.             { gs_ht_separation_Cyan, gs_ht_separation_Magenta,
  572.               gs_ht_separation_Yellow, gs_ht_separation_Black
  573.             }
  574.         };
  575.         const gs_ht_separation_name _ds *cnames =
  576.             dcnames[gs_currentdevice_inline(pgs)->color_info.
  577.                 num_components];
  578.         uint i;
  579.         memset(pgdht->color_indices, 0, sizeof(pdht->color_indices));
  580.         for ( i = 0; i < pdht->num_comp; i++ )
  581.         {    int j;
  582.             for ( j = 0; j < 4; j++ )
  583.             {    if ( pdht->components[i].cname == cnames[j] )
  584.                     pgdht->color_indices[j] = i;
  585.             }
  586.         }
  587.     }
  588.     gx_unset_dev_color(pgs);
  589.     return 0;
  590. }
  591.