home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 2 / AACD 2.iso / AACD / Magazine / UsingPDF / GhostScript / source / gs5.10 / gsfunc0.c < prev    next >
Encoding:
C/C++ Source or Header  |  1997-10-30  |  7.0 KB  |  247 lines

  1. /* Copyright (C) 1997 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. /* gsfunc0.c */
  20. /* Implementation of FunctionType 0 (Sampled) Functions */
  21. #include "math_.h"
  22. #include "gx.h"
  23. #include "gserrors.h"
  24. #include "gsfunc0.h"
  25. #include "gxfunc.h"
  26.  
  27. typedef struct gs_function_Sd_s {
  28.   gs_function_head_t head;
  29.   gs_function_Sd_params_t params;
  30. } gs_function_Sd_t;
  31. private_st_function_Sd();
  32.  
  33. /* Define the maximum plausible number of inputs and outputs */
  34. /* for a Sampled function. */
  35. #define max_Sd_m 16
  36. #define max_Sd_n 16
  37.  
  38. /* Get one set of sample values. */
  39. private int
  40. fn_get_samples(const gs_function_Sd_t *pfn, ulong offset, uint *samples)
  41. {    int bps = pfn->params.BitsPerSample;
  42.     int n = pfn->params.n;
  43.     byte buf[max_Sd_n * 4];
  44.     const byte *p;
  45.     int i;
  46.  
  47.     data_source_access(&pfn->params.DataSource, offset >> 3,
  48.                (bps * n + 7) >> 3, buf, &p);
  49.     switch ( bps ) {
  50.       case 1:
  51.         for ( i = 0; i < n; ++i ) {
  52.           samples[i] = (*p >> (~offset & 7)) & 1;
  53.           if ( !(++offset & 7) )
  54.         p++;
  55.         }
  56.         break;
  57.       case 2:
  58.         for ( i = 0; i < n; ++i ) {
  59.           samples[i] = (*p >> (6 - (offset & 7))) & 3;
  60.           if ( !((offset += 2) & 7) )
  61.         p++;
  62.         }
  63.         break;
  64.       case 4:
  65.         for ( i = 0; i < n; offset ^= 4, ++i )
  66.           samples[i] = (offset & 4 ? *p++ & 0xf : *p >> 4);
  67.         break;
  68.       case 8:
  69.         for ( i = 0; i < n; ++i )
  70.           samples[i] = *p++;
  71.         break;
  72.       case 12:
  73.         for ( i = 0; i < n; offset ^= 4, ++i )
  74.           if ( offset & 4 )
  75.         samples[i] = ((*p & 0xf) << 8) + p[1], p += 2;
  76.           else
  77.         samples[i] = (*p << 4) + (p[1] >> 4), p++;
  78.         break;
  79.       case 16:
  80.         for ( i = 0; i < n; p += 2, ++i )
  81.           samples[i] = (*p << 8) + p[1];
  82.         break;
  83.       case 24:
  84.         for ( i = 0; i < n; p += 3, ++i )
  85.           samples[i] = (*p << 16) + (p[1] << 8) + p[2];
  86.         break;
  87.       case 32:
  88.         for ( i = 0; i < n; p += 4, ++i )
  89.           samples[i] = (*p << 24) + (p[1] << 16) + (p[2] << 8) + p[3];
  90.         break;
  91.     }
  92.     return 0;
  93. }
  94.  
  95. /* Calculate a result by multilinear interpolation. */
  96. private void
  97. fn_interpolate_linear(const gs_function_Sd_t *pfn, const float *values,
  98.   const ulong *factors, uint *samples, ulong offset, int i)
  99. {    if ( i == pfn->params.m )
  100.       fn_get_samples(pfn, offset, samples);
  101.     else {
  102.       float fpart = values[i] - floor(values[i]);
  103.  
  104.       fn_interpolate_linear(pfn, values, factors, samples, offset, i + 1);
  105.       if ( fpart != 0 ) {
  106.         int j;
  107.         uint samples1[max_Sd_n];
  108.  
  109.         fn_interpolate_linear(pfn, values, factors, samples1,
  110.                    offset + factors[i], i + 1);
  111.         for ( j = 0; j < pfn->params.n; ++j )
  112.           samples[j] += (samples1[j] - samples[j]) * fpart;
  113.       }
  114.     }
  115. }
  116.  
  117. /* Evaluate a Sampled function. */
  118. private int
  119. fn_Sd_evaluate(const gs_function_t *pfn_common, const float *in, float *out)
  120. {    const gs_function_Sd_t *pfn = (const gs_function_Sd_t *)pfn_common;
  121.     int bps = pfn->params.BitsPerSample;
  122.     ulong offset = 0;
  123.     int i;
  124.     float encoded[max_Sd_m];
  125.     ulong factors[max_Sd_m];
  126.     uint samples[max_Sd_n];
  127.  
  128.     /* Encode the input values. */
  129.  
  130.     for ( i = 0; i < pfn->params.m; ++i ) {
  131.       float d0 = pfn->params.Domain[2 * i],
  132.         d1 = pfn->params.Domain[2 * i + 1];
  133.       float arg = in[i], enc;
  134.  
  135.       if ( arg < d0 )
  136.         arg = d0;
  137.       else if ( arg > d1 )
  138.         arg = d1;
  139.       if ( pfn->params.Encode ) {
  140.         float e0 = pfn->params.Encode[2 * i];
  141.         float e1 = pfn->params.Encode[2 * i + 1];
  142.  
  143.         enc = (arg - d0) * (e1 - e0) / (d1 - d0) + e0;
  144.         if ( enc < 0 )
  145.           encoded[i] = 0;
  146.         else if ( enc >= pfn->params.Size[i] - 1 )
  147.           encoded[i] = pfn->params.Size[i] - 1;
  148.         else
  149.           encoded[i] = enc;
  150.       } else {
  151.         /* arg is guaranteed to be in bounds, ergo so is enc */
  152.         encoded[i] = (arg - d0) * (pfn->params.Size[i] - 1) / (d1 - d0);
  153.       }
  154.     }
  155.  
  156.     /* Look up and interpolate the output values. */
  157.  
  158.     { ulong factor = bps * pfn->params.n;
  159.  
  160.       for ( i = 0; i < pfn->params.m; factor *= pfn->params.Size[i++] )
  161.         offset += (factors[i] = factor) * (int)encoded[i];
  162.     }
  163.     /****** LINEAR INTERPOLATION ONLY ******/
  164.     fn_interpolate_linear(pfn, encoded, factors, samples, offset, 0);
  165.  
  166.     /* Encode the output values. */
  167.  
  168.     for ( i = 0; i < pfn->params.n; offset += bps, ++i ) {
  169.       float d0, d1, r0, r1, value;
  170.  
  171.       if ( pfn->params.Range )
  172.         r0 = pfn->params.Range[2 * i], r1 = pfn->params.Range[2 * i + 1];
  173.       else
  174.         r0 = 0, r1 = (1 << bps) - 1;
  175.       if ( pfn->params.Decode )
  176.         d0 = pfn->params.Decode[2 * i], d1 = pfn->params.Decode[2 * i + 1];
  177.       else
  178.         d0 = r0, d1 = r1;
  179.  
  180.       value = samples[i] * (d1 - d0) / ((1 << bps) - 1) + d0;
  181.       if ( value < r0 )
  182.         out[i] = r0;
  183.       else if ( value > r1 )
  184.         out[i] = r1;
  185.       else
  186.         out[i] = value;
  187.     }
  188.  
  189.     return 0;
  190. }
  191.  
  192. /* Free the parameters of a Sampled function. */
  193. void
  194. gs_function_Sd_free_params(gs_function_Sd_params_t *params, gs_memory_t *mem)
  195. {    gs_free_object(mem, (void *)params->Size, "Size"); /* break const */
  196.     gs_free_object(mem, (void *)params->Decode, "Decode"); /* break const */
  197.     gs_free_object(mem, (void *)params->Encode, "Encode"); /* break const */
  198.     fn_common_free_params((gs_function_params_t *)params, mem);
  199. }
  200.  
  201. /* Allocate and initialize a Sampled function. */
  202. int
  203. gs_function_Sd_init(gs_function_t **ppfn,
  204.    const gs_function_Sd_params_t *params, gs_memory_t *mem)
  205. {    static const gs_function_head_t function_Sd_head = {
  206.       function_type_Sampled,
  207.       (fn_evaluate_proc_t)fn_Sd_evaluate,
  208.       (fn_free_params_proc_t)gs_function_Sd_free_params,
  209.       fn_common_free
  210.     };
  211.     int i;
  212.  
  213.     *ppfn = 0;        /* in case of error */
  214.     fn_check_mnDR(params, params->m, params->n);
  215.     if ( params->m > max_Sd_m )
  216.       return_error(gs_error_limitcheck);
  217.     switch ( params->Order ) {
  218.       case 0:        /* use default */
  219.       case 1: case 3:
  220.         break;
  221.       default:
  222.         return_error(gs_error_rangecheck);
  223.     }
  224.     switch ( params->BitsPerSample ) {
  225.       case 1: case 2: case 4: case 8: case 12: case 16: case 24: case 32:
  226.         break;
  227.       default:
  228.         return_error(gs_error_rangecheck);
  229.     }
  230.     for ( i = 0; i < params->m; ++i )
  231.       if ( params->Size[i] <= 0 )
  232.         return_error(gs_error_rangecheck);
  233.     { gs_function_Sd_t *pfn =
  234.         gs_alloc_struct(mem, gs_function_Sd_t, &st_function_Sd,
  235.                 "gs_function_Sd_init");
  236.  
  237.       if ( pfn == 0 )
  238.         return_error(gs_error_VMerror);
  239.       pfn->params = *params;
  240.       if ( params->Order == 0 )
  241.         pfn->params.Order = 1; /* default */
  242.       pfn->head = function_Sd_head;
  243.       *ppfn = (gs_function_t *)pfn;
  244.     }
  245.     return 0;
  246. }
  247.