-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
extractor.c
197 lines (175 loc) · 4.82 KB
/
extractor.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
/////////////////////////////////////////////
// //
// Copyright (C) 2019-2019 Julian Uy //
// https://sites.google.com/site/awertyb //
// //
// See details of license at "LICENSE" //
// //
/////////////////////////////////////////////
#include "extractor.h"
#include <avif/avif.h>
#include <stdint.h>
#include <stdio.h>
#include <string.h>
const char *plugin_info[4] = {
"00IN",
"AVIF Plugin for Susie Image Viewer",
"*.avif",
"AVIF file (*.avif)",
};
const int header_size = 64;
int getBMPFromAVIF(const uint8_t *input_data, size_t file_size,
HANDLE* h_bitmap_info,
HANDLE* h_bitmap_data) {
BITMAPINFOHEADER* bitmap_info_header = NULL;
uint8_t *bitmap_data = NULL;
int ret_result = -1;
int width = 0;
int height = 0;
int bit_width, bit_length;
avifRGBImage rgb;
memset(&rgb, 0, sizeof(rgb));
SYSTEM_INFO info;
avifDecoder *decoder = avifDecoderCreate();
GetSystemInfo(&info);
decoder->maxThreads = info.dwNumberOfProcessors;
avifResult result = avifDecoderSetIOMemory(decoder, input_data, file_size);
if (result != AVIF_RESULT_OK)
{
goto cleanup;
}
result = avifDecoderParse(decoder);
if (result != AVIF_RESULT_OK)
{
goto cleanup;
}
result = avifDecoderNextImage(decoder);
if (result == AVIF_RESULT_OK) {
width = decoder->image->width;
height = decoder->image->height;
bit_width = width * 4;
bit_length = bit_width;
avifRGBImageSetDefaults(&rgb, decoder->image);
rgb.depth = 8;
rgb.format = AVIF_RGB_FORMAT_BGRA;
rgb.maxThreads = info.dwNumberOfProcessors;
*h_bitmap_data = LocalAlloc(LMEM_MOVEABLE, sizeof(uint8_t) * bit_length * height);
if (!*h_bitmap_data)
{
goto cleanup;
}
bitmap_data = (uint8_t*)LocalLock(*h_bitmap_data);
if (!bitmap_data)
{
LocalFree(*h_bitmap_data);
*h_bitmap_data = NULL;
goto cleanup;
}
rgb.pixels = bitmap_data;
rgb.rowBytes = sizeof(uint8_t) * bit_length;
if (avifImageYUVToRGB(decoder->image, &rgb) != AVIF_RESULT_OK)
{
goto cleanup;
}
// Flip along the horizontal axis
for (int j = 0; j < height / 2; j++)
{
DWORD *curbit_1 = (DWORD*)(bitmap_data + j * bit_length);
DWORD *curbit_2 = (DWORD*)(bitmap_data + (height - (1 + j)) * bit_length);
for (int i = 0; i < width; i++)
{
DWORD tmp = curbit_1[i];
curbit_1[i] = curbit_2[i];
curbit_2[i] = tmp;
}
}
} else {
goto cleanup;
}
*h_bitmap_info = LocalAlloc(LMEM_MOVEABLE | LMEM_ZEROINIT, sizeof(BITMAPINFO));
if (NULL == *h_bitmap_info)
{
goto cleanup;
}
bitmap_info_header = (BITMAPINFOHEADER*)LocalLock(*h_bitmap_info);
if (NULL == bitmap_info_header)
{
LocalFree(*h_bitmap_info);
*h_bitmap_info = NULL;
goto cleanup;
}
bitmap_info_header->biSize = sizeof(BITMAPINFOHEADER);
bitmap_info_header->biWidth = width;
bitmap_info_header->biHeight = height;
bitmap_info_header->biPlanes = 1;
bitmap_info_header->biBitCount = 32;
bitmap_info_header->biCompression = BI_RGB;
bitmap_info_header->biSizeImage = sizeof(uint8_t) * bit_length * height;
LocalUnlock(*h_bitmap_data);
LocalUnlock(*h_bitmap_info);
ret_result = 0;
cleanup:
if (ret_result && bitmap_data)
{
LocalUnlock(*h_bitmap_data);
LocalFree(*h_bitmap_data);
*h_bitmap_data = NULL;
}
if (ret_result && bitmap_info_header)
{
LocalUnlock(*h_bitmap_info);
LocalFree(*h_bitmap_info);
*h_bitmap_info = NULL;
}
avifDecoderDestroy(decoder);
return ret_result;
}
BOOL IsSupportedEx(const char *data) {
if(strncmp(data + 4, "ftypavif", 8) == 0)
{
return TRUE;
}
return FALSE;
}
int GetPictureInfoEx(size_t data_size, const char *data, SusiePictureInfo *picture_info)
{
int width = 0;
int height = 0;
int ret_result = SPI_MEMORY_ERROR;
avifDecoder *decoder = avifDecoderCreate();
avifResult result = avifDecoderSetIOMemory(decoder, (uint8_t *)data, data_size);
if (result != AVIF_RESULT_OK)
{
goto cleanup;
}
result = avifDecoderParse(decoder);
if (result != AVIF_RESULT_OK)
{
goto cleanup;
}
width = decoder->image->width;
height = decoder->image->height;
picture_info->left = 0;
picture_info->top = 0;
picture_info->width = width;
picture_info->height = height;
picture_info->x_density = 0;
picture_info->y_density = 0;
picture_info->colorDepth = 32;
picture_info->hInfo = NULL;
ret_result = SPI_ALL_RIGHT;
cleanup:
avifDecoderDestroy(decoder);
return ret_result;
}
int GetPictureEx(size_t data_size, HANDLE *bitmap_info, HANDLE *bitmap_data, SPI_PROGRESS progress_callback, intptr_t user_data, const char *data) {
if (progress_callback != NULL)
if (progress_callback(1, 1, user_data))
return SPI_ABORT;
if (getBMPFromAVIF((const uint8_t*)data, data_size, bitmap_info, bitmap_data))
return SPI_MEMORY_ERROR;
if (progress_callback != NULL)
if (progress_callback(1, 1, user_data))
return SPI_ABORT;
return SPI_ALL_RIGHT;
}