-
Notifications
You must be signed in to change notification settings - Fork 83
/
Copy pathimage-load-fits.cc
211 lines (173 loc) · 5.48 KB
/
image-load-fits.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
/*
* Copyright (C) 2024 - The Geeqie Team
*
* Author: Colin Clark
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#include "image-load-fits.h"
#include <cmath>
#include <fitsio.h>
#include <limits>
#include "image-load.h"
namespace
{
struct ImageLoaderFITS : public ImageLoaderBackend
{
public:
~ImageLoaderFITS() override;
void init(AreaUpdatedCb area_updated_cb, SizePreparedCb size_prepared_cb, AreaPreparedCb area_prepared_cb, gpointer data) override;
gboolean write(const guchar *buf, gsize &chunk_size, gsize count, GError **error) override;
GdkPixbuf *get_pixbuf() override;
gchar *get_format_name() override;
gchar **get_format_mime_types() override;
void set_page_num(gint page_num) override;
gint get_page_total() override;
private:
AreaUpdatedCb area_updated_cb;
gpointer data;
GdkPixbuf *pixbuf;
gint page_num;
gint page_total;
};
gboolean ImageLoaderFITS::write(const guchar *buf, gsize &chunk_size, gsize count, GError **)
{
fitsfile *fptr; // FITS file pointer
gint anynul;
gint bitpix;
gint naxis;
gint status = 0; // cfitsio status value must be initialized to zero
glong fpixel = 1;
glong naxes[2] = {1, 1}; // Image width and height
/* Open FITS file from memory buffer */
if (fits_open_memfile(&fptr, "mem://", READONLY, (void **)&buf, &count, 0, nullptr, &status))
{
fits_report_error(stderr, status);
return FALSE;
}
/* Check that the file is an image and get its dimensions */
if (fits_get_img_param(fptr, 2, &bitpix, &naxis, naxes, &status))
{
fits_report_error(stderr, status);
fits_close_file(fptr, &status);
return FALSE;
}
if (naxis != 2) // Ensure it's a 2D image
{
log_printf("Error: FITS image is not 2D");
fits_close_file(fptr, &status);
return FALSE;
}
glong width = naxes[0];
glong height = naxes[1];
/* Allocate memory for the image data */
auto *image_data = (gfloat *)malloc(width * height * sizeof(gfloat));
if (!image_data)
{
log_printf("Memory allocation error when processing .fits file");
fits_close_file(fptr, &status);
return FALSE;
}
/* Read the image data */
if (fits_read_img(fptr, TFLOAT, fpixel, width * height, nullptr, image_data, &anynul, &status))
{
fits_report_error(stderr, status);
free(image_data);
fits_close_file(fptr, &status);
return FALSE;
}
/* Close the FITS file */
fits_close_file(fptr, &status);
/* Create a GdkPixbuf in RGB format (24-bit depth, 8 bits per channel) */
GdkPixbuf *pixbuf_tmp = gdk_pixbuf_new(GDK_COLORSPACE_RGB, FALSE, 8, width, height);
if (!pixbuf_tmp)
{
log_printf("Failed to create GdkPixbuf for .fits file");
free(image_data);
return FALSE;
}
guchar *pixels = gdk_pixbuf_get_pixels(pixbuf_tmp);
gint rowstride = gdk_pixbuf_get_rowstride(pixbuf_tmp);
gfloat max_value = 0;
gfloat min_value = std::numeric_limits<float>::max();
/* Get the max and min intensity values in the image */
for (glong y = 0; y < height; y++)
{
for (glong x = 0; x < width; x++)
{
gfloat value = (image_data[(y * width) + x]);
max_value = std::max(max_value, value);
min_value = std::min(min_value, value);
}
}
/* Map the float data to RGB and load it into the GdkPixbuf */
for (glong y = 0; y < height; y++)
{
for (glong x = 0; x < width; x++)
{
gint pixel_index = (y * rowstride) + (x * 3);
gfloat value = (image_data[(y * width) + x]);
/* fits images seem to have a large intensity range, but the useful data is mostly at the lower end.
* Using linear scaling results in a black image. */
auto intensity = (guchar)(255.0 * (log(value - min_value) / log((max_value - min_value))));
/* Set the RGB channels to the intensity value for grayscale */
pixels[pixel_index] = intensity; // Red
pixels[pixel_index + 1] = intensity; // Green
pixels[pixel_index + 2] = intensity; // Blue
}
}
pixbuf = gdk_pixbuf_copy(pixbuf_tmp);
g_object_unref(pixbuf_tmp);
area_updated_cb(nullptr, 0, 0, width, height, data);
chunk_size = count;
return TRUE;
}
void ImageLoaderFITS::init(AreaUpdatedCb area_updated_cb, SizePreparedCb, AreaPreparedCb, gpointer data)
{
this->area_updated_cb = area_updated_cb;
this->data = data;
page_num = 0;
}
GdkPixbuf *ImageLoaderFITS::get_pixbuf()
{
return pixbuf;
}
gchar *ImageLoaderFITS::get_format_name()
{
return g_strdup("fits");
}
gchar **ImageLoaderFITS::get_format_mime_types()
{
static const gchar *mime[] = {"image/fits", nullptr};
return g_strdupv(const_cast<gchar **>(mime));
}
void ImageLoaderFITS::set_page_num(gint page_num)
{
this->page_num = page_num;
}
gint ImageLoaderFITS::get_page_total()
{
return page_total;
}
ImageLoaderFITS::~ImageLoaderFITS()
{
if (pixbuf) g_object_unref(pixbuf);
}
} // namespace
std::unique_ptr<ImageLoaderBackend> get_image_loader_backend_fits()
{
return std::make_unique<ImageLoaderFITS>();
}
/* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */