| 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 | #pragma once |
| 29 | |
| 30 | #include <glib.h> |
| 31 | #include <gio/gio.h> |
| 32 | #include <glib-object.h> |
| 33 | |
| 34 | /** |
| 35 | * GNOME_QR_BYTES_PER_FORMAT: |
| 36 | * @format: a #GnomeQrPixelFormat |
| 37 | * |
| 38 | * Calculates the number of bytes per pixel for a given pixel format. |
| 39 | * |
| 40 | * Returns: the number of bytes |
| 41 | */ |
| 42 | #define GNOME_QR_BYTES_PER_FORMAT(format) \ |
| 43 | ((format) == GNOME_QR_PIXEL_FORMAT_A_8 ? 1 : \ |
| 44 | ((format) == GNOME_QR_PIXEL_FORMAT_G_8 ? 1 : \ |
| 45 | (format) == GNOME_QR_PIXEL_FORMAT_RGB_888 ? 3 : 4)) |
| 46 | |
| 47 | /** |
| 48 | * GNOME_QR_COLOR_TRANSPARENT: |
| 49 | * |
| 50 | * Predefined transparent color (0, 0, 0, 0). |
| 51 | */ |
| 52 | #define GNOME_QR_COLOR_TRANSPARENT ((GnomeQrColor) { 0, 0, 0, 0 }) |
| 53 | |
| 54 | /** |
| 55 | * GNOME_QR_COLOR_WHITE: |
| 56 | * |
| 57 | * Predefined white color (255, 255, 255, 255). |
| 58 | */ |
| 59 | #define GNOME_QR_COLOR_WHITE ((GnomeQrColor) { 255, 255, 255, 255 }) |
| 60 | |
| 61 | /** |
| 62 | * GNOME_QR_COLOR_BLACK: |
| 63 | * |
| 64 | * Predefined black color (0, 0, 0, 255). |
| 65 | */ |
| 66 | #define GNOME_QR_COLOR_BLACK ((GnomeQrColor) { 0, 0, 0, 255 }) |
| 67 | |
| 68 | /** |
| 69 | * GnomeQrEccLevel: |
| 70 | * @GNOME_QR_ECC_LEVEL_LOW: Low error correction (~7% recovery capability) |
| 71 | * @GNOME_QR_ECC_LEVEL_MEDIUM: Medium error correction (~15% recovery capability) |
| 72 | * @GNOME_QR_ECC_LEVEL_QUARTILE: Quartile error correction (~25% recovery capability) |
| 73 | * @GNOME_QR_ECC_LEVEL_HIGH: High error correction (~30% recovery capability) |
| 74 | * |
| 75 | * Error correction levels for QR codes. Higher levels provide better error |
| 76 | * recovery at the cost of requiring bigger dimensions. |
| 77 | */ |
| 78 | typedef enum { |
| 79 | GNOME_QR_ECC_LEVEL_LOW, |
| 80 | GNOME_QR_ECC_LEVEL_MEDIUM, |
| 81 | GNOME_QR_ECC_LEVEL_QUARTILE, |
| 82 | GNOME_QR_ECC_LEVEL_HIGH, |
| 83 | } GnomeQrEccLevel; |
| 84 | |
| 85 | /** |
| 86 | * GnomeQrPixelFormat: |
| 87 | * @GNOME_QR_PIXEL_FORMAT_RGB_888: 24-bit RGB format (3 bytes per pixel) |
| 88 | * @GNOME_QR_PIXEL_FORMAT_RGBA_8888: 32-bit RGBA format (4 bytes per pixel) |
| 89 | * |
| 90 | * Pixel formats for the generated QR code image data. |
| 91 | */ |
| 92 | typedef enum { |
| 93 | GNOME_QR_PIXEL_FORMAT_A_8, |
| 94 | GNOME_QR_PIXEL_FORMAT_G_8, |
| 95 | GNOME_QR_PIXEL_FORMAT_RGB_888, |
| 96 | GNOME_QR_PIXEL_FORMAT_RGBA_8888, |
| 97 | } GnomeQrPixelFormat; |
| 98 | |
| 99 | /** |
| 100 | * GnomeQrColor: |
| 101 | * @red: Red component (0-255) |
| 102 | * @green: Green component (0-255) |
| 103 | * @blue: Blue component (0-255) |
| 104 | * @alpha: Alpha component (0-255, where 255 is fully opaque). |
| 105 | * Must be 255 when using %GNOME_QR_PIXEL_FORMAT_RGB_888. |
| 106 | * |
| 107 | * Represents an RGBA color for QR code generation. |
| 108 | */ |
| 109 | typedef struct { |
| 110 | guint8 red; |
| 111 | guint8 green; |
| 112 | guint8 blue; |
| 113 | guint8 alpha; |
| 114 | } GnomeQrColor; |
| 115 | |
| 116 | GType gnome_qr_ecc_level_get_type (void); |
| 117 | |
| 118 | GType gnome_qr_pixel_format_get_type (void); |
| 119 | |
| 120 | GBytes * gnome_qr_generate_qr_code_sync (const char *text, |
| 121 | size_t requested_size, |
| 122 | const GnomeQrColor *bg_color, |
| 123 | const GnomeQrColor *fg_color, |
| 124 | GnomeQrPixelFormat format, |
| 125 | GnomeQrEccLevel ecc, |
| 126 | size_t *pixel_size_out, |
| 127 | GCancellable *cancellable, |
| 128 | GError **error); |
| 129 | |
| 130 | void gnome_qr_generate_qr_code_async (const char *text, |
| 131 | size_t requested_size, |
| 132 | const GnomeQrColor *bg_color, |
| 133 | const GnomeQrColor *fg_color, |
| 134 | GnomeQrPixelFormat format, |
| 135 | GnomeQrEccLevel ecc, |
| 136 | GCancellable *cancellable, |
| 137 | GAsyncReadyCallback callback, |
| 138 | gpointer user_data); |
| 139 | |
| 140 | GBytes * gnome_qr_generate_qr_code_finish (GAsyncResult *result, |
| 141 | size_t *pixel_size_out, |
| 142 | GError **error); |