1/*
2 * "$Id: curve-cache.c,v 1.6 2005/10/18 02:08:17 rlk Exp $"
3 *
4 *   Gimp-Print color management module - traditional Gimp-Print algorithm.
5 *
6 *   Copyright 1997-2000 Michael Sweet (mike@easysw.com) and
7 *	Robert Krawitz (rlk@alum.mit.edu)
8 *
9 *   This program is free software; you can redistribute it and/or modify it
10 *   under the terms of the GNU General Public License as published by the Free
11 *   Software Foundation; either version 2 of the License, or (at your option)
12 *   any later version.
13 *
14 *   This program is distributed in the hope that it will be useful, but
15 *   WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
16 *   or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
17 *   for more details.
18 *
19 *   You should have received a copy of the GNU General Public License
20 *   along with this program; if not, write to the Free Software
21 *   Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
22 */
23
24/*
25 * This file must include only standard C header files.  The core code must
26 * compile on generic platforms that don't support glib, gimp, gtk, etc.
27 */
28
29#ifdef HAVE_CONFIG_H
30#include <config.h>
31#endif
32#include <gutenprint/gutenprint.h>
33#include "gutenprint-internal.h"
34#include <gutenprint/gutenprint-intl-internal.h>
35#include <gutenprint/curve-cache.h>
36#include <math.h>
37#ifdef HAVE_LIMITS_H
38#include <limits.h>
39#endif
40#include <string.h>
41
42void
43stp_curve_free_curve_cache(stp_cached_curve_t *cache)
44{
45  if (cache->curve)
46    stp_curve_destroy(cache->curve);
47  cache->curve = NULL;
48  cache->d_cache = NULL;
49  cache->s_cache = NULL;
50  cache->count = 0;
51}
52
53void
54stp_curve_cache_curve_data(stp_cached_curve_t *cache)
55{
56  if (cache->curve && !cache->d_cache)
57    {
58      cache->s_cache = stp_curve_get_ushort_data(cache->curve, &(cache->count));
59      cache->d_cache = stp_curve_get_data(cache->curve, &(cache->count));
60    }
61}
62
63stp_curve_t *
64stp_curve_cache_get_curve(stp_cached_curve_t *cache)
65{
66  return cache->curve;
67}
68
69void
70stp_curve_cache_curve_invalidate(stp_cached_curve_t *cache)
71{
72  cache->d_cache = NULL;
73  cache->s_cache = NULL;
74  cache->count = 0;
75}
76
77void
78stp_curve_cache_set_curve(stp_cached_curve_t *cache, stp_curve_t *curve)
79{
80  stp_curve_cache_curve_invalidate(cache);
81  cache->curve = curve;
82}
83
84void
85stp_curve_cache_set_curve_copy(stp_cached_curve_t *cache, const stp_curve_t *curve)
86{
87  stp_curve_cache_curve_invalidate(cache);
88  cache->curve = stp_curve_create_copy(curve);
89}
90
91size_t
92stp_curve_cache_get_count(stp_cached_curve_t *cache)
93{
94  if (cache->curve)
95    {
96      if (!cache->d_cache)
97	cache->d_cache = stp_curve_get_data(cache->curve, &(cache->count));
98      return cache->count;
99    }
100  else
101    return 0;
102}
103
104const unsigned short *
105stp_curve_cache_get_ushort_data(stp_cached_curve_t *cache)
106{
107  if (cache->curve)
108    {
109      if (!cache->s_cache)
110	cache->s_cache =
111	  stp_curve_get_ushort_data(cache->curve, &(cache->count));
112      return cache->s_cache;
113    }
114  else
115    return NULL;
116}
117
118const double *
119stp_curve_cache_get_double_data(stp_cached_curve_t *cache)
120{
121  if (cache->curve)
122    {
123      if (!cache->d_cache)
124	cache->d_cache = stp_curve_get_data(cache->curve, &(cache->count));
125      return cache->d_cache;
126    }
127  else
128    return NULL;
129}
130
131void
132stp_curve_cache_copy(stp_cached_curve_t *dest, const stp_cached_curve_t *src)
133{
134  stp_curve_cache_curve_invalidate(dest);
135  if (dest != src)
136    {
137      if (src->curve)
138	stp_curve_cache_set_curve_copy(dest, src->curve);
139    }
140}
141
142