Debian Codegraph @

libgnome-desktop/gnome-qr/gnome-qr.c

raw symbols SCIP
1 /* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
2 /*
3 * Copyright 2019 Purism SPC
4 * Copyright 2024-2025 Red Hat, Inc
5 * Copyright 2024-2025 Canonical Ltd
6 *
7 * SPDX-License-Identifier: GPL-3.0-or-later
8 *
9 * This program is free software: you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation, either version 3 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program. If not, see <http://www.gnu.org/licenses/>.
21 *
22 * Author: Mohammed Sadiq <[email protected]>
23 * Ray Strode <[email protected]>
24 * Marco Trevisan <[email protected]>
25 * Joan Torres Lopez <[email protected]>
26 */
27
28 #include "config.h"
29
30 #include "gnome-qr.h"
31
32 #include <math.h>
33 #include <qrcodegen.h>
34
35 G_DEFINE_ENUM_TYPE (GnomeQrEccLevel, gnome_qr_ecc_level,
36 G_DEFINE_ENUM_VALUE (GNOME_QR_ECC_LEVEL_LOW, "low"),
37 G_DEFINE_ENUM_VALUE (GNOME_QR_ECC_LEVEL_MEDIUM, "medium"),
38 G_DEFINE_ENUM_VALUE (GNOME_QR_ECC_LEVEL_QUARTILE, "quartile"),
39 G_DEFINE_ENUM_VALUE (GNOME_QR_ECC_LEVEL_HIGH, "high"))
40
41 G_DEFINE_ENUM_TYPE (GnomeQrPixelFormat, gnome_qr_pixel_format,
42 G_DEFINE_ENUM_VALUE (GNOME_QR_PIXEL_FORMAT_A_8, "a-8"),
43 G_DEFINE_ENUM_VALUE (GNOME_QR_PIXEL_FORMAT_G_8, "g-8"),
44 G_DEFINE_ENUM_VALUE (GNOME_QR_PIXEL_FORMAT_RGB_888, "rgb-888"),
45 G_DEFINE_ENUM_VALUE (GNOME_QR_PIXEL_FORMAT_RGBA_8888, "rgba-8888"))
46
47 typedef struct
48 {
49 char *text;
50 size_t requested_size;
51 size_t pixel_size;
52 GnomeQrColor *bg_color;
53 GnomeQrColor *fg_color;
54 GnomeQrPixelFormat format;
55 GnomeQrEccLevel ecc;
56 } GnomeQrCodeData;
57
58 static void
59 gnome_qr_code_data_free (GnomeQrCodeData *data)
60 {
61 g_clear_pointer (&data->text, g_free);
62 g_clear_pointer (&data->bg_color, g_free);
63 g_clear_pointer (&data->fg_color, g_free);
64 g_free (data);
65 }
66
67 static enum qrcodegen_Ecc
68 gnome_qr_ecc_level_to_qrcodegen_ecc (GnomeQrEccLevel ecc)
69 {
70 switch (ecc) {
71 case GNOME_QR_ECC_LEVEL_LOW:
72 return qrcodegen_Ecc_LOW;
73 case GNOME_QR_ECC_LEVEL_MEDIUM:
74 return qrcodegen_Ecc_MEDIUM;
75 case GNOME_QR_ECC_LEVEL_QUARTILE:
76 return qrcodegen_Ecc_QUARTILE;
77 case GNOME_QR_ECC_LEVEL_HIGH:
78 return qrcodegen_Ecc_HIGH;
79 }
80 g_assert_not_reached ();
81 }
82
83 static void
84 fill_block (GByteArray *array,
85 const GnomeQrColor *color,
86 GnomeQrPixelFormat format,
87 int block_size)
88 {
89 guint i;
90
91 for (i = 0; i < block_size; i++) {
92 switch (format) {
93 case GNOME_QR_PIXEL_FORMAT_A_8:
94 g_byte_array_append (array, &color->alpha, 1);
95 break;
96
97 case GNOME_QR_PIXEL_FORMAT_G_8:
98 /* Use the red channel, as we assert that all the
99 * values must be equal */
100 g_byte_array_append (array, &color->red, 1);
101 break;
102
103 case GNOME_QR_PIXEL_FORMAT_RGB_888:
104 case GNOME_QR_PIXEL_FORMAT_RGBA_8888:
105 g_byte_array_append (array, &color->red, 1);
106 g_byte_array_append (array, &color->green, 1);
107 g_byte_array_append (array, &color->blue, 1);
108
109 if (format == GNOME_QR_PIXEL_FORMAT_RGBA_8888)
110 g_byte_array_append (array, &color->alpha, 1);
111 break;
112 default:
113 g_assert_not_reached ();
114 }
115 }
116 }
117
118 G_ALWAYS_INLINE static inline gboolean
119 check_color_validity (const GnomeQrColor *color,
120 GnomeQrPixelFormat format)
121 {
122 if (!color)
123 return TRUE;
124
125 switch (format) {
126 case GNOME_QR_PIXEL_FORMAT_A_8:
127 case GNOME_QR_PIXEL_FORMAT_RGBA_8888:
128 break;
129
130 case GNOME_QR_PIXEL_FORMAT_G_8:
131 g_return_val_if_fail (color->red == color->green &&
132 color->red == color->blue, FALSE);
133 /* fallthrough */
134 case GNOME_QR_PIXEL_FORMAT_RGB_888:
135 g_return_val_if_fail (color->alpha == 255, FALSE);
136 break;
137
138 default:
139 g_assert_not_reached ();
140 }
141
142 return TRUE;
143 }
144
145 /**
146 * gnome_qr_generate_qr_code_sync:
147 * @text: the text of which generate the QR code
148 * @requested_size: The requested size (width and height) in pixels of the QR code.
149 * Only square QR codes are supported. If the requested size is smaller than
150 * the minimum required size for the QR code, it will be generated with 1 pixel
151 * per block.
152 * @bg_color: (nullable): The background color of the code
153 * or %NULL to use default (white)
154 * @fg_color: (nullable): The foreground color of the code
155 * or %NULL to use default (black)
156 * @format: The pixel format for the output image data
157 * @ecc: The error correction level
158 * @pixel_size_out: (out): The actual square QR code size (width and height)
159 * in pixels. Note that it may not match @requested_size.
160 * @cancellable: (nullable): A #GCancellable to cancel the operation
161 * @error: #GError for error reporting
162 *
163 * Generates the QrCode synchronously.
164 *
165 * Returns: (transfer full): The pixel data or %NULL on error
166 */
167 GBytes *
168 gnome_qr_generate_qr_code_sync (const char *text,
169 size_t requested_size,
170 const GnomeQrColor *bg_color,
171 const GnomeQrColor *fg_color,
172 GnomeQrPixelFormat format,
173 GnomeQrEccLevel ecc,
174 size_t *pixel_size_out,
175 GCancellable *cancellable,
176 GError **error)
177 {
178 g_autoptr (GByteArray) qr_matrix = NULL;
179 uint8_t qr_code[qrcodegen_BUFFER_LEN_FOR_VERSION (qrcodegen_VERSION_MAX)];
180 uint8_t temp_buf[qrcodegen_BUFFER_LEN_FOR_VERSION (qrcodegen_VERSION_MAX)];
181 static const GnomeQrColor default_bg_color = GNOME_QR_COLOR_WHITE;
182 static const GnomeQrColor default_fg_color = GNOME_QR_COLOR_BLACK;
183 static const GnomeQrColor transparent = GNOME_QR_COLOR_TRANSPARENT;
184 gint qr_size, block_size, total_size;
185 gint column, row, i;
186
187 g_return_val_if_fail (text != NULL, NULL);
188 g_return_val_if_fail (*text != '\0', NULL);
189 g_return_val_if_fail (pixel_size_out != NULL, NULL);
190 g_return_val_if_fail (check_color_validity (bg_color, format), NULL);
191 g_return_val_if_fail (check_color_validity (fg_color, format), NULL);
192
193 if (g_cancellable_set_error_if_cancelled (cancellable, error))
194 return NULL;
195
196 if (!qrcodegen_encodeText (text,
197 temp_buf,
198 qr_code,
199 gnome_qr_ecc_level_to_qrcodegen_ecc (ecc),
200 qrcodegen_VERSION_MIN,
201 qrcodegen_VERSION_MAX,
202 qrcodegen_Mask_AUTO,
203 FALSE)) {
204 g_set_error (error,
205 G_IO_ERROR,
206 G_IO_ERROR_FAILED,
207 "QRCode generation failed for content '%s'",
208 text);
209 return NULL;
210 }
211
212 if (g_cancellable_set_error_if_cancelled (cancellable, error))
213 return NULL;
214
215 if (format == GNOME_QR_PIXEL_FORMAT_A_8 && !bg_color) {
216 bg_color = &transparent;
217 }
218
219 if (!bg_color)
220 bg_color = &default_bg_color;
221
222 if (!fg_color)
223 fg_color = &default_fg_color;
224
225 qr_size = qrcodegen_getSize (qr_code);
226 g_assert (qr_size > 0);
227
228 block_size = MAX (1, ceil ((double) requested_size / qr_size));
229 total_size = qr_size * block_size;
230
231 qr_matrix = g_byte_array_sized_new (total_size * total_size *
232 GNOME_QR_BYTES_PER_FORMAT (format));
233
234 for (row = 0; row < qr_size; ++row) {
235 for (i = 0; i < block_size; ++i) {
236 for (column = 0; column < qr_size; ++column) {
237 if (qrcodegen_getModule (qr_code, column, row))
238 fill_block (qr_matrix, fg_color, format, block_size);
239 else
240 fill_block (qr_matrix, bg_color, format, block_size);
241 }
242 }
243
244 if (g_cancellable_set_error_if_cancelled (cancellable, error))
245 return NULL;
246 }
247
248 *pixel_size_out = total_size;
249
250 return g_byte_array_free_to_bytes (g_steal_pointer (&qr_matrix));
251 }
252
253
254 static void
255 generate_qr_code_in_thread (GTask *task,
256 gpointer source,
257 gpointer task_data,
258 GCancellable *cancellable)
259 {
260 GnomeQrCodeData *data = task_data;
261 g_autoptr (GError) error = NULL;
262 g_autoptr (GBytes) icon = NULL;
263
264 icon = gnome_qr_generate_qr_code_sync (data->text,
265 data->requested_size,
266 data->bg_color,
267 data->fg_color,
268 data->format,
269 data->ecc,
270 &data->pixel_size,
271 cancellable,
272 &error);
273 if (!icon) {
274 g_task_return_error (task, g_steal_pointer (&error));
275 return;
276 }
277
278 g_task_return_pointer (task, g_steal_pointer (&icon), (GDestroyNotify) g_bytes_unref);
279 }
280
281 /**
282 * gnome_qr_generate_qr_code_async:
283 * @text: The text of which generate the QR code
284 * @requested_size: The requested size (width and height) in pixels of the QR code.
285 * Only square QR codes are supported. If the requested size is smaller than
286 * the minimum required size for the QR code, it will be generated with 1 pixel
287 * per block.
288 * @bg_color: (nullable): The background color of the code
289 * or %NULL to use default (white)
290 * @fg_color: (nullable): The foreground color of the code
291 * or %NULL to use default (black)
292 * @format: The pixel format for the output image data
293 * @ecc: The error correction level
294 * @cancellable: (nullable): A #GCancellable to cancel the operation
295 * @callback: (scope async): function to call returning success or failure
296 * of the async grabbing
297 * @user_data: the data to pass to callback function
298 *
299 * Generates the QrCode asynchronously.
300 *
301 * Use gnome_qr_generate_qr_code_finish() to complete it.
302 */
303 void
304 gnome_qr_generate_qr_code_async (const char *text,
305 size_t requested_size,
306 const GnomeQrColor *bg_color,
307 const GnomeQrColor *fg_color,
308 GnomeQrPixelFormat format,
309 GnomeQrEccLevel ecc,
310 GCancellable *cancellable,
311 GAsyncReadyCallback callback,
312 gpointer user_data)
313 {
314 g_autoptr (GTask) task = NULL;
315 GnomeQrCodeData *data;
316
317 g_return_if_fail (text != NULL);
318 g_return_if_fail (*text != '\0');
319 g_return_if_fail (check_color_validity (bg_color, format));
320 g_return_if_fail (check_color_validity (fg_color, format));
321
322 data = g_new0 (GnomeQrCodeData, 1);
323 data->text = g_strdup (text);
324 data->requested_size = requested_size;
325 data->bg_color = g_memdup2 (bg_color, sizeof (GnomeQrColor));
326 data->fg_color = g_memdup2 (fg_color, sizeof (GnomeQrColor));
327 data->format = format;
328 data->ecc = ecc;
329
330 task = g_task_new (NULL, cancellable, callback, user_data);
331 g_task_set_source_tag (task, gnome_qr_generate_qr_code_async);
332 g_task_set_task_data (task, data,
333 (GDestroyNotify) gnome_qr_code_data_free);
334 g_task_set_return_on_cancel (task, TRUE);
335 g_task_run_in_thread (task, generate_qr_code_in_thread);
336 }
337
338 /**
339 * gnome_qr_generate_qr_code_finish:
340 * @result: the #GAsyncResult that was provided to the callback
341 * @pixel_size_out: (out): The actual square QR code size (width and height)
342 * in pixels. Note that it may not match the @requested_size in
343 * gnome_qr_generate_qr_code_async().
344 * @error: #GError for error reporting
345 *
346 * Finish the asynchronous operation started by
347 * gnome_qr_generate_qr_code_async() and obtain its result.
348 *
349 * Returns: (transfer full): The pixel data or %NULL on error
350 */
351 GBytes *
352 gnome_qr_generate_qr_code_finish (GAsyncResult *result,
353 size_t *pixel_size_out,
354 GError **error)
355 {
356 GnomeQrCodeData *data;
357 g_autoptr (GBytes) pixel_data = NULL;
358
359 g_return_val_if_fail (G_IS_TASK (result), FALSE);
360 g_return_val_if_fail (g_async_result_is_tagged (result,
361 gnome_qr_generate_qr_code_async),
362 NULL);
363 g_return_val_if_fail (pixel_size_out != NULL, NULL);
364
365 pixel_data = g_task_propagate_pointer (G_TASK (result), error);
366 if (!pixel_data)
367 return NULL;
368
369 data = g_task_get_task_data (G_TASK (result));
370 *pixel_size_out = data->pixel_size;
371
372 return g_steal_pointer (&pixel_data);
373 }