forked from evgeny-nadymov/telegram-wp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTelegramClient.WebP.cpp
More file actions
executable file
·196 lines (180 loc) · 6.44 KB
/
Copy pathTelegramClient.WebP.cpp
File metadata and controls
executable file
·196 lines (180 loc) · 6.44 KB
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
//
// This is the source code of Telegram for Windows Phone v. 3.x.x.
// It is licensed under GNU GPL v. 2 or later.
// You should have received a copy of the license in this archive (see LICENSE).
//
// Copyright Evgeny Nadymov, 2013-present.
//
#include "pch.h"
#include "TelegramClient.WebP.h"
#include <windows.h>
#include <malloc.h>
using namespace TelegramClient_WebP::LibWebP;
using namespace Platform;
int(*WebPGetDecoderVersion) ();
int (*WebPGetInfo) (void* data, unsigned int data_size, int* width, int* height);
void* (*WebPDecodeRGBInto) (void* data, unsigned int data_size, void* output_buffer, int output_buffer_size, int output_stride);
void* (*WebPDecodeBGRInto) (void* data, unsigned int data_size, void* output_buffer, int output_buffer_size, int output_stride);
void* (*WebPDecodeBGRAInto) (void* data, unsigned int data_size, void* output_buffer, int output_buffer_size, int output_stride);
void* (*WebPDecodeRgbAInto) (void* data, unsigned int data_size, void* output_buffer, int output_buffer_size, int output_stride);
void* (*WebPDecodeRGBAInto) (void* data, unsigned int data_size, void* output_buffer, int output_buffer_size, int output_stride);
bool loaded = false;
WebPDecoder::WebPDecoder()
{
if (!loaded)
{
#if _M_ARM
auto lib = LoadPackagedLibrary(L"ARM\\libwebp", 0);
#else
auto lib = LoadPackagedLibrary(L"x86\\libwebp", 0);
#endif
if (!lib)
throw ref new Platform::FailureException();
WebPGetDecoderVersion = (int(*)()) GetProcAddress(lib, "WebPGetDecoderVersion");
WebPGetInfo =
(int(*)(
void* data,
unsigned int data_size,
int* width,
int* height))
GetProcAddress(lib, "WebPGetInfo");
WebPDecodeRGBInto =
(void*(*)(
void* data,
unsigned int data_size,
void* output_buffer,
int output_buffer_size,
int output_stride))
GetProcAddress(lib, "WebPDecodeRGBInto");
WebPDecodeBGRInto =
(void*(*) (
void* data,
unsigned int data_size,
void* output_buffer,
int output_buffer_size,
int output_stride))
GetProcAddress(lib, "WebPDecodeBGRInto");
WebPDecodeBGRAInto =
(void* (*) (
void* data,
unsigned int data_size,
void* output_buffer,
int output_buffer_size,
int output_stride))
GetProcAddress(lib, "WebPDecodeBGRAInto");
WebPDecodeRgbAInto =
(void* (*) (
void* data,
unsigned int data_size,
void* output_buffer,
int output_buffer_size,
int output_stride))
GetProcAddress(lib, "WebPDecodeRgbAInto");
WebPDecodeRGBAInto =
(void* (*) (
void* data,
unsigned int data_size,
void* output_buffer,
int output_buffer_size,
int output_stride))
GetProcAddress(lib, "WebPDecodeRGBAInto");
if (WebPGetDecoderVersion&&
WebPGetInfo&&
WebPDecodeRGBInto&&
WebPDecodeBGRInto&&
WebPDecodeBGRAInto&&
WebPDecodeRgbAInto&&
WebPDecodeRGBAInto)
loaded = true;
else
throw ref new Platform::FailureException();
}
}
Platform::String^ WebPDecoder::GetDecoderVersion()
{
return GetDecoderVersion()->ToString();
}
bool WebPDecoder::GetInfo(const Platform::Array<unsigned char>^ Data, int* Width, int* Height)
{
auto nData = (unsigned char*) malloc(Data->Length*sizeof(unsigned char));
memcpy(nData, Data->Data, Data->Length);
auto result = WebPGetInfo(nData, Data->Length, Width, Height);
free(nData);
return result;
}
Platform::Array<unsigned char>^ WebPDecoder::Decode(DecodeType type, const Platform::Array<unsigned char>^ Data, int* Width, int* Height)
{
auto nData = (unsigned char*) malloc(Data->Length*sizeof(unsigned char));
memcpy(nData, Data->Data, Data->Length);
int retDataLength = 0;
unsigned char* retData = NULL;
if (!GetInfo(Data, Width, Height))
throw ref new Platform::FailureException();
switch (type)
{
case DecodeType::rgbA:
retDataLength = (*Width) * (*Height) * 4 * sizeof(unsigned char);
retData = (unsigned char*)malloc(retDataLength);
retData = (unsigned char*)WebPDecodeRgbAInto(nData, Data->Length, retData, (*Width) * (*Height) * 4, (*Width) * 4);
free(nData);
break;
case DecodeType::BGR:
retDataLength = (*Width) * (*Height) * 3 * sizeof(unsigned char);
retData = (unsigned char*) malloc(retDataLength);
retData = (unsigned char*) WebPDecodeBGRInto(nData, Data->Length, retData, (*Width) * (*Height) * 3, (*Width) * 3);
free(nData);
break;
case DecodeType::RGB:
retDataLength = (*Width) * (*Height) * 3 * sizeof(unsigned char);
retData = (unsigned char*) malloc(retDataLength);
retData = (unsigned char*) WebPDecodeRGBInto(nData, Data->Length, retData, (*Width) * (*Height) * 3, (*Width) * 3);
free(nData);
break;
case DecodeType::RGBA:
retDataLength = (*Width) * (*Height) * 4 * sizeof(unsigned char);
retData = (unsigned char*) malloc(retDataLength);
retData = (unsigned char*) WebPDecodeRGBAInto(nData, Data->Length, retData, (*Width) * (*Height) * 4, (*Width) * 4);
free(nData);
break;
case DecodeType::BGRA:
retDataLength = (*Width) * (*Height) * 4 * sizeof(unsigned char);
retData = (unsigned char*) malloc(retDataLength);
retData = (unsigned char*) WebPDecodeBGRAInto(nData, Data->Length, retData, (*Width) * (*Height) * 4, (*Width) * 4);
free(nData);
break;
default:
free(nData);
throw ref new Platform::NotImplementedException();
}
if (retData == NULL)
throw ref new Platform::FailureException();
auto retArr = ref new Platform::Array<unsigned char>(retData, retDataLength);
//for (auto i = 0; i < retDataLength; i++)
//{
// retArr[i] = retData[i];
//}
free(retData);
return retArr;
}
Platform::Array<unsigned char>^ WebPDecoder::DecodeRgbA(const Platform::Array<unsigned char>^ Data, int* Width, int* Height)
{
auto nData = (unsigned char*)malloc(Data->Length*sizeof(unsigned char));
memcpy(nData, Data->Data, Data->Length);
int retDataLength = 0;
unsigned char* retData = NULL;
if (!GetInfo(Data, Width, Height))
throw ref new Platform::FailureException();
retDataLength = (*Width)* (*Height)* 4 * sizeof(unsigned char);
retData = (unsigned char*)malloc(retDataLength);
retData = (unsigned char*)WebPDecodeRgbAInto(nData, Data->Length, retData, (*Width)* (*Height)* 4, (*Width)* 4);
free(nData);
if (retData == NULL)
throw ref new Platform::FailureException();
auto retArr = ref new Platform::Array<unsigned char>(retData, retDataLength);
//for (auto i = 0; i < retDataLength; i++)
//{
// retArr[i] = retData[i];
//}
free(retData);
return retArr;
}