Coverage Report

Created: 2026-08-11 06:46

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libvips/libvips/colour/rad2float.c
Line
Count
Source
1
/* Convert Radiance 32bit packed format to float.
2
 *
3
 * 3/3/09
4
 *  - from LabQ2Lab and Radiance sources
5
 * 2/11/09
6
 *  - gtkdoc
7
 * 20/9/12
8
 *  - redo as a class
9
 * 13/12/12
10
 *  - tag output as scRGB, since it'll be 0-1
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
655k
#define RED 0
120
655k
#define GRN 1
121
655k
#define BLU 2
122
#define CIEX 0 /* or, if input is XYZ... */
123
#define CIEY 1
124
#define CIEZ 2
125
655k
#define EXP 3    /* exponent same for either format */
126
275k
#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
static void
143
colr_color(COLOR col, COLR clr) /* convert short to float color */
144
380k
{
145
380k
  if (clr[EXP] == 0)
146
104k
    col[RED] = col[GRN] = col[BLU] = 0.0;
147
275k
  else {
148
275k
    double f = ldexp(1.0, (int) clr[EXP] - (COLXS + 8));
149
150
275k
    col[RED] = (clr[RED] + 0.5) * f;
151
275k
    col[GRN] = (clr[GRN] + 0.5) * f;
152
275k
    col[BLU] = (clr[BLU] + 0.5) * f;
153
275k
  }
154
380k
}
155
156
/* End copy-paste from Radiance sources.
157
 */
158
159
typedef VipsColourCode VipsRad2float;
160
typedef VipsColourCodeClass VipsRad2floatClass;
161
162
39
G_DEFINE_TYPE(VipsRad2float, vips_rad2float, VIPS_TYPE_COLOUR_CODE);
163
39
164
39
static void
165
39
vips_rad2float_line(VipsColour *colour, VipsPel *out, VipsPel **in, int width)
166
12.3k
{
167
12.3k
  COLR *inp = (COLR *) in[0];
168
12.3k
  COLOR *outbuf = (COLOR *) out;
169
170
12.3k
  int i;
171
172
392k
  for (i = 0; i < width; i++)
173
380k
    colr_color(outbuf[i], inp[i]);
174
12.3k
}
175
176
static void
177
vips_rad2float_class_init(VipsRad2floatClass *class)
178
19
{
179
19
  VipsObjectClass *object_class = (VipsObjectClass *) class;
180
19
  VipsColourClass *colour_class = VIPS_COLOUR_CLASS(class);
181
182
19
  object_class->nickname = "rad2float";
183
19
  object_class->description =
184
19
    _("unpack Radiance coding to float RGB");
185
186
19
  colour_class->process_line = vips_rad2float_line;
187
19
}
188
189
static void
190
vips_rad2float_init(VipsRad2float *rad2float)
191
2.39k
{
192
2.39k
  VipsColour *colour = VIPS_COLOUR(rad2float);
193
2.39k
  VipsColourCode *code = VIPS_COLOUR_CODE(rad2float);
194
195
2.39k
  colour->coding = VIPS_CODING_NONE;
196
2.39k
  colour->interpretation = VIPS_INTERPRETATION_scRGB;
197
2.39k
  colour->format = VIPS_FORMAT_FLOAT;
198
2.39k
  colour->bands = 3;
199
2.39k
  colour->input_bands = 4;
200
201
2.39k
  code->input_coding = VIPS_CODING_RAD;
202
2.39k
}
203
204
/**
205
 * vips_rad2float: (method)
206
 * @in: input image
207
 * @out: (out): output image
208
 * @...: `NULL`-terminated list of optional named arguments
209
 *
210
 * Unpack a RAD ([enum@Vips.Coding.RAD]) image to a three-band float image.
211
 *
212
 * ::: seealso
213
 *     [method@Image.float2rad], [method@Image.LabQ2LabS].
214
 *
215
 * Returns: 0 on success, -1 on error.
216
 */
217
int
218
vips_rad2float(VipsImage *in, VipsImage **out, ...)
219
2.39k
{
220
2.39k
  va_list ap;
221
2.39k
  int result;
222
223
2.39k
  va_start(ap, out);
224
2.39k
  result = vips_call_split("rad2float", ap, in, out);
225
2.39k
  va_end(ap);
226
227
2.39k
  return result;
228
2.39k
}