-
Notifications
You must be signed in to change notification settings - Fork 3
/
Xqoi.c
218 lines (185 loc) · 5.6 KB
/
Xqoi.c
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
212
213
214
215
216
217
218
// Xqoi.c - QOI XnView plugin
//
// Copyright (C) 2021 Piotr Fusik
//
// MIT License:
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
#ifdef WIN32
#define _WIN32_WINNT 0x0500
#include <windows.h>
#define API __stdcall
#define DLL_EXPORT __declspec(dllexport)
#else
#include <stdint.h>
#define BOOL int32_t
#define DWORD uint32_t
#define INT int32_t
#define API
#define DLL_EXPORT
#define TRUE 1
#define FALSE 0
#endif
#include <stddef.h>
#include <stdlib.h>
#include "QOI-stdio.h"
#define GFP_RGB 0
#define GFP_BGR 1
#define GFP_READ 0x0001
#define GFP_WRITE 0x0002
typedef struct {
unsigned char red[256];
unsigned char green[256];
unsigned char blue[256];
} GFP_COLORMAP;
static size_t strlcpy(char *dst, const char *src, size_t size)
{
int i;
for (i = 0; i < size - 1 && src[i] != '\0'; i++)
dst[i] = src[i];
dst[i] = '\0';
while (src[i] != '\0')
i++;
return i;
}
DLL_EXPORT BOOL API gfpGetPluginInfo(
DWORD version, char *label, INT label_max_size,
char *extension, INT extension_max_size, INT *support)
{
if (version != 0x0002)
return FALSE;
strlcpy(label, "Quite OK Image", label_max_size);
strlcpy(extension, "qoi", extension_max_size);
*support = GFP_READ | GFP_WRITE;
return TRUE;
}
DLL_EXPORT void * API gfpLoadPictureInit(const char *filename)
{
return QOIDecoder_LoadFile(filename);
}
DLL_EXPORT BOOL API gfpLoadPictureGetInfo(
void *ptr, INT *pictype, INT *width, INT *height,
INT *dpi, INT *bits_per_pixel, INT *bytes_per_line,
BOOL *has_colormap, char *label, INT label_max_size)
{
const QOIDecoder *qoi = (const QOIDecoder *) ptr;
*pictype = GFP_RGB;
*width = QOIDecoder_GetWidth(qoi);
*height = QOIDecoder_GetHeight(qoi);
*dpi = 96;
int bytes_per_pixel = QOIDecoder_HasAlpha(qoi) ? 4 : 3;
*bits_per_pixel = bytes_per_pixel << 3;
*bytes_per_line = *width * bytes_per_pixel;
*has_colormap = FALSE;
strlcpy(label, "Quite OK Image", label_max_size);
return TRUE;
}
DLL_EXPORT BOOL API gfpLoadPictureGetLine(void *ptr, INT line, unsigned char *buffer)
{
const QOIDecoder *qoi = (const QOIDecoder *) ptr;
int width = QOIDecoder_GetWidth(qoi);
const int *pixels = QOIDecoder_GetPixels(qoi) + line * width;
int bytes_per_pixel = QOIDecoder_HasAlpha(qoi) ? 4 : 3;
for (int x = 0; x < width; x++) {
int rgb = pixels[x];
buffer[x * bytes_per_pixel] = (unsigned char) (rgb >> 16);
buffer[x * bytes_per_pixel + 1] = (unsigned char) (rgb >> 8);
buffer[x * bytes_per_pixel + 2] = (unsigned char) rgb;
if (bytes_per_pixel == 4)
buffer[x * bytes_per_pixel + 3] = (unsigned char) (rgb >> 24);
}
return TRUE;
}
DLL_EXPORT BOOL API gfpLoadPictureGetColormap(void *ptr, GFP_COLORMAP *cmap)
{
return FALSE;
}
DLL_EXPORT void API gfpLoadPictureExit(void *ptr)
{
QOIDecoder *qoi = (QOIDecoder *) ptr;
QOIDecoder_Delete(qoi);
}
DLL_EXPORT BOOL API gfpSavePictureIsSupported(INT width, INT height, INT bits_per_pixel, BOOL has_colormap)
{
return (bits_per_pixel == 24 || bits_per_pixel == 32)
&& !has_colormap
&& QOIEncoder_CanEncode(width, height, bits_per_pixel == 32);
}
typedef struct {
int width;
int height;
bool alpha;
FILE *f;
int pixels[];
} QOIWriter;
DLL_EXPORT void * API gfpSavePictureInit(
const char *filename, INT width, INT height, INT bits_per_pixel,
INT dpi, INT *picture_type, char *label, INT label_max_size)
{
FILE *f = fopen(filename, "wb");
if (f == NULL)
return NULL;
QOIWriter *w = (QOIWriter *) malloc(sizeof(QOIWriter) + width * height * sizeof(int));
if (w == NULL) {
fclose(f);
return NULL;
}
w->width = width;
w->height = height;
w->alpha = bits_per_pixel == 32;
w->f = f;
*picture_type = GFP_RGB;
strlcpy(label, "Quite OK Image", label_max_size);
return w;
}
DLL_EXPORT BOOL API gfpSavePicturePutLine(void *ptr, INT line, const unsigned char *buffer)
{
QOIWriter *w = (QOIWriter *) ptr;
int width = w->width;
bool alpha = w->alpha;
int bytes_per_pixel = alpha ? 4 : 3;
int *pixels = w->pixels + line * width;
for (int x = 0; x < width; x++) {
pixels[x] = buffer[x * bytes_per_pixel] << 16
| buffer[x * bytes_per_pixel + 1] << 8
| buffer[x * bytes_per_pixel + 2]
| (alpha ? buffer[x * bytes_per_pixel + 3] : 0xff) << 24;
}
return TRUE;
}
DLL_EXPORT BOOL API gfpSavePicturePutColormap(void *ptr, const GFP_COLORMAP *cmap)
{
return FALSE;
}
DLL_EXPORT void API gfpSavePictureExit(void *ptr)
{
QOIWriter *w = (QOIWriter *) ptr;
QOIEncoder *qoi = QOIEncoder_New();
if (QOIEncoder_Encode(qoi, w->width, w->height, w->pixels, w->alpha, false))
QOIEncoder_SaveStdio(qoi, w->f);
QOIEncoder_Delete(qoi);
fclose(w->f);
free(w);
}
#ifdef WIN32
BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
{
return TRUE;
}
#endif