Coverage Report

Created: 2026-12-01 06:53

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libvips/libvips/foreign/rawload.c
Line
Count
Source
1
/* load raw data from a file
2
 *
3
 * 14/12/11
4
 * 5/8/19
5
 *  - add @format and @interpretation
6
 */
7
8
/*
9
10
  This file is part of VIPS.
11
12
  VIPS is free software; you can redistribute it and/or modify
13
  it under the terms of the GNU Lesser General Public License as published by
14
  the Free Software Foundation; either version 2 of the License, or
15
  (at your option) any later version.
16
17
  This program is distributed in the hope that it will be useful,
18
  but WITHOUT ANY WARRANTY; without even the implied warranty of
19
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20
  GNU Lesser General Public License for more details.
21
22
  You should have received a copy of the GNU Lesser General Public License
23
  along with this program; if not, write to the Free Software
24
  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
25
  02110-1301  USA
26
27
 */
28
29
/*
30
31
  These files are distributed with VIPS - http://www.vips.ecs.soton.ac.uk
32
33
 */
34
35
/*
36
#define DEBUG
37
 */
38
39
#ifdef HAVE_CONFIG_H
40
#include <config.h>
41
#endif /*HAVE_CONFIG_H*/
42
#include <glib/gi18n-lib.h>
43
44
#include <stdio.h>
45
#include <stdlib.h>
46
47
#include <vips/vips.h>
48
#include <vips/internal.h>
49
50
typedef struct _VipsForeignLoadRaw {
51
  VipsForeignLoad parent_object;
52
53
  char *filename;
54
  int width;
55
  int height;
56
  int bands;
57
  guint64 offset;
58
  VipsBandFormat format;
59
  VipsInterpretation interpretation;
60
} VipsForeignLoadRaw;
61
62
typedef VipsForeignLoadClass VipsForeignLoadRawClass;
63
64
2
G_DEFINE_TYPE(VipsForeignLoadRaw, vips_foreign_load_raw,
65
2
  VIPS_TYPE_FOREIGN_LOAD);
66
2
67
2
static VipsForeignFlags
68
2
vips_foreign_load_raw_get_flags(VipsForeignLoad *load)
69
2
{
70
0
  return VIPS_FOREIGN_PARTIAL;
71
0
}
72
73
static VipsForeignFlags
74
vips_foreign_load_raw_get_flags_filename(const char *filename)
75
0
{
76
0
  return VIPS_FOREIGN_PARTIAL;
77
0
}
78
79
static int
80
vips_foreign_load_raw_header(VipsForeignLoad *load)
81
0
{
82
0
  VipsForeignLoadRaw *raw = (VipsForeignLoadRaw *) load;
83
84
0
  VipsImage *out;
85
0
  VipsImage *x;
86
87
0
  if (!(out = vips_image_new_from_file_raw(raw->filename,
88
0
        raw->width, raw->height,
89
0
        vips_format_sizeof_unsafe(raw->format) * raw->bands,
90
0
        raw->offset)))
91
0
    return -1;
92
93
0
  if (vips_copy(out, &x,
94
0
      "interpretation", raw->interpretation,
95
0
      "format", raw->format,
96
0
      "bands", raw->bands,
97
0
      NULL)) {
98
0
    g_object_unref(out);
99
0
    return -1;
100
0
  }
101
0
  g_object_unref(out);
102
0
  out = x;
103
104
  /* Remove the @out that's there now.
105
   */
106
0
  g_object_get(load, "out", &x, NULL);
107
0
  g_object_unref(x);
108
0
  g_object_unref(x);
109
110
0
  g_object_set(load, "out", out, NULL);
111
112
0
  return 0;
113
0
}
114
115
static void
116
vips_foreign_load_raw_class_init(VipsForeignLoadRawClass *class)
117
1
{
118
1
  GObjectClass *gobject_class = G_OBJECT_CLASS(class);
119
1
  VipsObjectClass *object_class = (VipsObjectClass *) class;
120
1
  VipsOperationClass *operation_class = VIPS_OPERATION_CLASS(class);
121
1
  VipsForeignLoadClass *load_class = (VipsForeignLoadClass *) class;
122
123
1
  gobject_class->set_property = vips_object_set_property;
124
1
  gobject_class->get_property = vips_object_get_property;
125
126
  /* You're unlikely to want to use this on untrusted files.
127
   */
128
1
  operation_class->flags |= VIPS_OPERATION_UNTRUSTED;
129
130
1
  object_class->nickname = "rawload";
131
1
  object_class->description = _("load raw data from a file");
132
133
1
  load_class->get_flags = vips_foreign_load_raw_get_flags;
134
1
  load_class->get_flags_filename =
135
1
    vips_foreign_load_raw_get_flags_filename;
136
1
  load_class->header = vips_foreign_load_raw_header;
137
138
1
  VIPS_ARG_STRING(class, "filename", 1,
139
1
    _("Filename"),
140
1
    _("Filename to load from"),
141
1
    VIPS_ARGUMENT_REQUIRED_INPUT,
142
1
    G_STRUCT_OFFSET(VipsForeignLoadRaw, filename),
143
1
    NULL);
144
145
1
  VIPS_ARG_INT(class, "width", 20,
146
1
    _("Width"),
147
1
    _("Image width in pixels"),
148
1
    VIPS_ARGUMENT_REQUIRED_INPUT,
149
1
    G_STRUCT_OFFSET(VipsForeignLoadRaw, width),
150
1
    0, VIPS_MAX_COORD, 0);
151
152
1
  VIPS_ARG_INT(class, "height", 21,
153
1
    _("Height"),
154
1
    _("Image height in pixels"),
155
1
    VIPS_ARGUMENT_REQUIRED_INPUT,
156
1
    G_STRUCT_OFFSET(VipsForeignLoadRaw, height),
157
1
    0, VIPS_MAX_COORD, 0);
158
159
1
  VIPS_ARG_INT(class, "bands", 22,
160
1
    _("Bands"),
161
1
    _("Number of bands in image"),
162
1
    VIPS_ARGUMENT_REQUIRED_INPUT,
163
1
    G_STRUCT_OFFSET(VipsForeignLoadRaw, bands),
164
1
    0, VIPS_MAX_COORD, 0);
165
166
1
  VIPS_ARG_UINT64(class, "offset", 23,
167
1
    _("Size of header"),
168
1
    _("Offset in bytes from start of file"),
169
1
    VIPS_ARGUMENT_OPTIONAL_INPUT,
170
1
    G_STRUCT_OFFSET(VipsForeignLoadRaw, offset),
171
1
    0, 100000000000, 0);
172
173
1
  VIPS_ARG_ENUM(class, "format", 24,
174
1
    _("Format"),
175
1
    _("Pixel format in image"),
176
1
    VIPS_ARGUMENT_OPTIONAL_INPUT,
177
1
    G_STRUCT_OFFSET(VipsForeignLoadRaw, format),
178
1
    VIPS_TYPE_BAND_FORMAT, VIPS_FORMAT_UCHAR);
179
180
1
  VIPS_ARG_ENUM(class, "interpretation", 25,
181
1
    _("Interpretation"),
182
1
    _("Pixel interpretation"),
183
1
    VIPS_ARGUMENT_OPTIONAL_INPUT,
184
1
    G_STRUCT_OFFSET(VipsForeignLoadRaw, interpretation),
185
1
    VIPS_TYPE_INTERPRETATION, VIPS_INTERPRETATION_MULTIBAND);
186
1
}
187
188
static void
189
vips_foreign_load_raw_init(VipsForeignLoadRaw *raw)
190
0
{
191
0
  raw->format = VIPS_FORMAT_UCHAR;
192
0
  raw->interpretation = VIPS_INTERPRETATION_MULTIBAND;
193
0
}
194
195
/**
196
 * vips_rawload:
197
 * @filename: file to load
198
 * @out: (out): output image
199
 * @width: width of image in pixels
200
 * @height: height of image in pixels
201
 * @bands: number of image bands
202
 * @...: `NULL`-terminated list of optional named arguments
203
 *
204
 * This operation mmaps the file, setting up @out so that access to that
205
 * image will read from the file.
206
 *
207
 * By default, it assumes uchar pixels. Use @format to select something else.
208
 *
209
 * The image will be tagged as [enum@Vips.Interpretation.MULTIBAND]. Use
210
 * @interpretation to select something else.
211
 *
212
 * Use [method@Image.byteswap] to reverse the byte ordering if necessary.
213
 *
214
 * ::: tip "Optional arguments"
215
 *     * @offset: `guint64`, offset in bytes from start of file
216
 *     * @format: [enum@BandFormat], set image format
217
 *     * @interpretation: [enum@Interpretation], set image interpretation
218
 *
219
 * ::: seealso
220
 *     [ctor@Image.new_from_file], [method@Image.copy], [method@Image.byteswap].
221
 *
222
 * Returns: 0 on success, -1 on error.
223
 */
224
int
225
vips_rawload(const char *filename, VipsImage **out,
226
  int width, int height, int bands, ...)
227
0
{
228
0
  va_list ap;
229
0
  int result;
230
231
0
  va_start(ap, bands);
232
0
  result = vips_call_split("rawload", ap,
233
0
    filename, out, width, height, bands);
234
0
  va_end(ap);
235
236
0
  return result;
237
0
}