Coverage Report

Created: 2025-01-28 06:33

/src/libvips/libvips/colour/float2rad.c
Line
Count
Source (jump to first uncovered line)
1
/* Convert float to Radiance 32bit packed format
2
 *
3
 * 23/3/09
4
 *  - from im_rad2float and Radiance sources
5
 * 2/11/09
6
 *  - gtkdoc
7
 * 20/9/12
8
 *  - redo as a class
9
 * 13/12/12
10
 *  - tag as scRGB rather than XYZ
11
 */
12
13
/*
14
15
  This file is part of VIPS.
16
17
  VIPS is free software; you can redistribute it and/or modify
18
  it under the terms of the GNU Lesser General Public License as published by
19
  the Free Software Foundation; either version 2 of the License, or
20
  (at your option) any later version.
21
22
  This program is distributed in the hope that it will be useful,
23
  but WITHOUT ANY WARRANTY; without even the implied warranty of
24
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
25
  GNU Lesser General Public License for more details.
26
27
  You should have received a copy of the GNU Lesser General Public License
28
  along with this program; if not, write to the Free Software
29
  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
30
  02110-1301  USA
31
32
 */
33
34
/*
35
36
  These files are distributed with VIPS - http://www.vips.ecs.soton.ac.uk
37
38
 */
39
40
/*
41
42
  Sections of this file from Greg Ward and Radiance with kind
43
  permission. The Radience copyright notice appears below.
44
45
 */
46
47
/* ====================================================================
48
 * The Radiance Software License, Version 1.0
49
 *
50
 * Copyright (c) 1990 - 2009 The Regents of the University of California,
51
 * through Lawrence Berkeley National Laboratory.   All rights reserved.
52
 *
53
 * Redistribution and use in source and binary forms, with or without
54
 * modification, are permitted provided that the following conditions
55
 * are met:
56
 *
57
 * 1. Redistributions of source code must retain the above copyright
58
 *         notice, this list of conditions and the following disclaimer.
59
 *
60
 * 2. Redistributions in binary form must reproduce the above copyright
61
 *       notice, this list of conditions and the following disclaimer in
62
 *       the documentation and/or other materials provided with the
63
 *       distribution.
64
 *
65
 * 3. The end-user documentation included with the redistribution,
66
 *           if any, must include the following acknowledgment:
67
 *             "This product includes Radiance software
68
 *                 (http://radsite.lbl.gov/)
69
 *                 developed by the Lawrence Berkeley National Laboratory
70
 *               (http://www.lbl.gov/)."
71
 *       Alternately, this acknowledgment may appear in the software itself,
72
 *       if and wherever such third-party acknowledgments normally appear.
73
 *
74
 * 4. The names "Radiance," "Lawrence Berkeley National Laboratory"
75
 *       and "The Regents of the University of California" must
76
 *       not be used to endorse or promote products derived from this
77
 *       software without prior written permission. For written
78
 *       permission, please contact radiance@radsite.lbl.gov.
79
 *
80
 * 5. Products derived from this software may not be called "Radiance",
81
 *       nor may "Radiance" appear in their name, without prior written
82
 *       permission of Lawrence Berkeley National Laboratory.
83
 *
84
 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
85
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
86
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
87
 * DISCLAIMED.   IN NO EVENT SHALL Lawrence Berkeley National Laboratory OR
88
 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
89
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
90
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
91
 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
92
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
93
 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
94
 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
95
 * SUCH DAMAGE.
96
 * ====================================================================
97
 *
98
 * This software consists of voluntary contributions made by many
99
 * individuals on behalf of Lawrence Berkeley National Laboratory.   For more
100
 * information on Lawrence Berkeley National Laboratory, please see
101
 * <http://www.lbl.gov/>.
102
 */
103
104
#ifdef HAVE_CONFIG_H
105
#include <config.h>
106
#endif /*HAVE_CONFIG_H*/
107
#include <glib/gi18n-lib.h>
108
109
#include <stdio.h>
110
#include <math.h>
111
112
#include <vips/vips.h>
113
114
#include "pcolour.h"
115
116
/* Begin copy-paste from Radiance sources.
117
 */
118
119
0
#define RED 0
120
0
#define GRN 1
121
0
#define BLU 2
122
#define CIEX 0 /* or, if input is XYZ... */
123
#define CIEY 1
124
#define CIEZ 2
125
0
#define EXP 3    /* exponent same for either format */
126
0
#define COLXS 128 /* excess used for exponent */
127
#define WHT 3   /* used for RGBPRIMS type */
128
129
#undef BYTE
130
#define BYTE unsigned char /* 8-bit unsigned integer */
131
132
typedef BYTE COLR[4]; /* red, green, blue (or X,Y,Z), exponent */
133
134
typedef float COLORV;
135
typedef COLORV COLOR[3]; /* red, green, blue (or X,Y,Z) */
136
137
#define copycolor(c1, c2) ( \
138
  (c1)[0] = (c2)[0], \
139
  (c1)[1] = (c2)[1], \
140
  (c1)[2] = (c2)[2])
141
142
/* assign a short color value */
143
static void
144
setcolr(COLR clr, double r, double g, double b)
145
0
{
146
0
  double d;
147
0
  int e;
148
149
0
  d = r > g ? r : g;
150
0
  if (b > d)
151
0
    d = b;
152
153
0
  if (d <= 1e-32) {
154
0
    clr[RED] = clr[GRN] = clr[BLU] = 0;
155
0
    clr[EXP] = 0;
156
0
    return;
157
0
  }
158
159
0
  d = frexp(d, &e) * 255.9999 / d;
160
161
0
  if (r > 0.0)
162
0
    clr[RED] = r * d;
163
0
  else
164
0
    clr[RED] = 0;
165
0
  if (g > 0.0)
166
0
    clr[GRN] = g * d;
167
0
  else
168
0
    clr[GRN] = 0;
169
0
  if (b > 0.0)
170
0
    clr[BLU] = b * d;
171
0
  else
172
0
    clr[BLU] = 0;
173
174
0
  clr[EXP] = e + COLXS;
175
0
}
176
177
/* End copy-paste from Radiance sources.
178
 */
179
180
typedef VipsColourCode VipsFloat2rad;
181
typedef VipsColourCodeClass VipsFloat2radClass;
182
183
G_DEFINE_TYPE(VipsFloat2rad, vips_float2rad, VIPS_TYPE_COLOUR_CODE);
184
185
static void
186
vips_float2rad_line(VipsColour *colour, VipsPel *out, VipsPel **in, int width)
187
0
{
188
0
  COLOR *inp = (COLOR *) in[0];
189
0
  COLR *outbuf = (COLR *) out;
190
191
0
  while (width-- > 0) {
192
0
    setcolr(outbuf[0], inp[0][RED], inp[0][GRN], inp[0][BLU]);
193
0
    inp++;
194
0
    outbuf++;
195
0
  }
196
0
}
197
198
static void
199
vips_float2rad_class_init(VipsFloat2radClass *class)
200
1
{
201
1
  VipsObjectClass *object_class = (VipsObjectClass *) class;
202
1
  VipsColourClass *colour_class = VIPS_COLOUR_CLASS(class);
203
204
1
  object_class->nickname = "float2rad";
205
1
  object_class->description =
206
1
    _("transform float RGB to Radiance coding");
207
208
1
  colour_class->process_line = vips_float2rad_line;
209
1
}
210
211
static void
212
vips_float2rad_init(VipsFloat2rad *float2rad)
213
0
{
214
0
  VipsColour *colour = VIPS_COLOUR(float2rad);
215
0
  VipsColourCode *code = VIPS_COLOUR_CODE(float2rad);
216
217
0
  colour->coding = VIPS_CODING_RAD;
218
0
  colour->interpretation = VIPS_INTERPRETATION_scRGB;
219
0
  colour->format = VIPS_FORMAT_UCHAR;
220
0
  colour->input_bands = 3;
221
0
  colour->bands = 4;
222
223
0
  code->input_coding = VIPS_CODING_NONE;
224
0
  code->input_format = VIPS_FORMAT_FLOAT;
225
0
}
226
227
/**
228
 * vips_float2rad: (method)
229
 * @in: input image
230
 * @out: (out): output image
231
 * @...: %NULL-terminated list of optional named arguments
232
 *
233
 * Convert a three-band float image to Radiance 32-bit packed format.
234
 *
235
 * See also: vips_rad2float(), #VIPS_CODING_RAD, vips_LabQ2Lab().
236
 *
237
 * Returns: 0 on success, -1 on error.
238
 */
239
int
240
vips_float2rad(VipsImage *in, VipsImage **out, ...)
241
0
{
242
0
  va_list ap;
243
0
  int result;
244
245
0
  va_start(ap, out);
246
0
  result = vips_call_split("float2rad", ap, in, out);
247
0
  va_end(ap);
248
249
0
  return result;
250
0
}