1 /*
2  * Copyright 2009 Vincent Povirk for CodeWeavers
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
17  */
18 
19 #ifndef WINCODECS_PRIVATE_H
20 #define WINCODECS_PRIVATE_H
21 
22 #include "wincodec.h"
23 #include "wincodecsdk.h"
24 
25 #include "wine/debug.h"
26 
27 #ifdef __REACTOS__
28 #include <wine/library.h>
29 #endif
30 
31 DEFINE_GUID(CLSID_WineTgaDecoder, 0xb11fc79a,0x67cc,0x43e6,0xa9,0xce,0xe3,0xd5,0x49,0x45,0xd3,0x04);
32 
33 DEFINE_GUID(GUID_WineContainerFormatTga, 0x0c44fda1,0xa5c5,0x4298,0x96,0x85,0x47,0x3f,0xc1,0x7c,0xd3,0x22);
34 
35 DEFINE_GUID(GUID_VendorWine, 0xddf46da1,0x7dc1,0x404e,0x98,0xf2,0xef,0xa4,0x8d,0xfc,0x95,0x0a);
36 
37 #ifdef __REACTOS__
38 extern const GUID IID_IMILBitmap;
39 extern const GUID IID_IMILBitmapSource;
40 extern const GUID IID_IMILBitmapLock;
41 extern const GUID IID_IMILBitmapScaler;
42 extern const GUID IID_IMILFormatConverter;
43 extern const GUID IID_IMILPalette;
44 #else
45 extern IID IID_IMILBitmap;
46 extern IID IID_IMILBitmapSource;
47 extern IID IID_IMILBitmapLock;
48 extern IID IID_IMILBitmapScaler;
49 extern IID IID_IMILFormatConverter;
50 extern IID IID_IMILPalette;
51 #endif
52 
53 #define INTERFACE IMILBitmapSource
DECLARE_INTERFACE_(IMILBitmapSource,IUnknown)54 DECLARE_INTERFACE_(IMILBitmapSource,IUnknown)
55 {
56     /*** IUnknown methods ***/
57     STDMETHOD_(HRESULT,QueryInterface)(THIS_ REFIID,void **) PURE;
58     STDMETHOD_(ULONG,AddRef)(THIS) PURE;
59     STDMETHOD_(ULONG,Release)(THIS) PURE;
60     /*** IWICBitmapSource methods ***/
61     STDMETHOD_(HRESULT,GetSize)(THIS_ UINT *,UINT *) PURE;
62     STDMETHOD_(HRESULT,GetPixelFormat)(THIS_ int *) PURE;
63     STDMETHOD_(HRESULT,GetResolution)(THIS_ double *,double *) PURE;
64     STDMETHOD_(HRESULT,CopyPalette)(THIS_ IWICPalette *) PURE;
65     STDMETHOD_(HRESULT,CopyPixels)(THIS_ const WICRect *,UINT,UINT,BYTE *) PURE;
66 };
67 #undef INTERFACE
68 
69 #define INTERFACE IMILBitmap
DECLARE_INTERFACE_(IMILBitmap,IMILBitmapSource)70 DECLARE_INTERFACE_(IMILBitmap,IMILBitmapSource)
71 {
72     /*** IUnknown methods ***/
73     STDMETHOD_(HRESULT,QueryInterface)(THIS_ REFIID,void **) PURE;
74     STDMETHOD_(ULONG,AddRef)(THIS) PURE;
75     STDMETHOD_(ULONG,Release)(THIS) PURE;
76     /*** IWICBitmapSource methods ***/
77     STDMETHOD_(HRESULT,GetSize)(THIS_ UINT *,UINT *) PURE;
78     STDMETHOD_(HRESULT,GetPixelFormat)(THIS_ int *) PURE;
79     STDMETHOD_(HRESULT,GetResolution)(THIS_ double *,double *) PURE;
80     STDMETHOD_(HRESULT,CopyPalette)(THIS_ IWICPalette *) PURE;
81     STDMETHOD_(HRESULT,CopyPixels)(THIS_ const WICRect *,UINT,UINT,BYTE *) PURE;
82     /*** IMILBitmap methods ***/
83     STDMETHOD_(HRESULT,unknown1)(THIS_ void **) PURE;
84     STDMETHOD_(HRESULT,Lock)(THIS_ const WICRect *,DWORD,IWICBitmapLock **) PURE;
85     STDMETHOD_(HRESULT,Unlock)(THIS_ IWICBitmapLock *) PURE;
86     STDMETHOD_(HRESULT,SetPalette)(THIS_ IWICPalette *) PURE;
87     STDMETHOD_(HRESULT,SetResolution)(THIS_ double,double) PURE;
88     STDMETHOD_(HRESULT,AddDirtyRect)(THIS_ const WICRect *) PURE;
89 };
90 #undef INTERFACE
91 
92 #define INTERFACE IMILBitmapScaler
DECLARE_INTERFACE_(IMILBitmapScaler,IMILBitmapSource)93 DECLARE_INTERFACE_(IMILBitmapScaler,IMILBitmapSource)
94 {
95     /*** IUnknown methods ***/
96     STDMETHOD_(HRESULT,QueryInterface)(THIS_ REFIID,void **) PURE;
97     STDMETHOD_(ULONG,AddRef)(THIS) PURE;
98     STDMETHOD_(ULONG,Release)(THIS) PURE;
99     /*** IWICBitmapSource methods ***/
100     STDMETHOD_(HRESULT,GetSize)(THIS_ UINT *,UINT *) PURE;
101     STDMETHOD_(HRESULT,GetPixelFormat)(THIS_ int *) PURE;
102     STDMETHOD_(HRESULT,GetResolution)(THIS_ double *,double *) PURE;
103     STDMETHOD_(HRESULT,CopyPalette)(THIS_ IWICPalette *) PURE;
104     STDMETHOD_(HRESULT,CopyPixels)(THIS_ const WICRect *,UINT,UINT,BYTE *) PURE;
105     /*** IMILBitmapScaler methods ***/
106     STDMETHOD_(HRESULT,unknown1)(THIS_ void **) PURE;
107     STDMETHOD_(HRESULT,Initialize)(THIS_ IMILBitmapSource *,UINT,UINT,WICBitmapInterpolationMode) PURE;
108 };
109 #undef INTERFACE
110 
111 #define THISCALLMETHOD_(type,method)  type (__thiscall *method)
112 
113 #define INTERFACE IMILUnknown1
DECLARE_INTERFACE_(IMILUnknown1,IUnknown)114 DECLARE_INTERFACE_(IMILUnknown1,IUnknown)
115 {
116     /*** IUnknown methods ***/
117     STDMETHOD_(HRESULT,QueryInterface)(THIS_ REFIID,void **) PURE;
118     STDMETHOD_(ULONG,AddRef)(THIS) PURE;
119     STDMETHOD_(ULONG,Release)(THIS) PURE;
120     /*** thiscall method ***/
121     #ifdef __REACTOS__
122     STDMETHOD_(void,unknown1)(THIS_ void*) PURE;
123     #else
124     THISCALLMETHOD_(void,unknown1)(THIS_ void*) PURE;
125     #endif
126     STDMETHOD_(HRESULT,unknown2)(THIS_ void*, void*) PURE;
127     #ifdef __REACTOS__
128     STDMETHOD_(HRESULT,unknown3)(THIS_ void*) PURE;
129     #else
130     THISCALLMETHOD_(HRESULT,unknown3)(THIS_ void*) PURE;
131     #endif
132     STDMETHOD_(HRESULT,unknown4)(THIS_ void*) PURE;
133     STDMETHOD_(HRESULT,unknown5)(THIS_ void*) PURE;
134     STDMETHOD_(HRESULT,unknown6)(THIS_ DWORD64) PURE;
135     STDMETHOD_(HRESULT,unknown7)(THIS_ void*) PURE;
136     #ifdef __REACTOS__
137     /*** thiscall method ***/
138     STDMETHOD_(HRESULT,unknown8)(THIS) PURE;
139     #else
140     THISCALLMETHOD_(HRESULT,unknown8)(THIS) PURE;
141     #endif
142 };
143 #undef INTERFACE
144 
145 #define INTERFACE IMILUnknown2
DECLARE_INTERFACE_(IMILUnknown2,IUnknown)146 DECLARE_INTERFACE_(IMILUnknown2,IUnknown)
147 {
148     /*** IUnknown methods ***/
149     STDMETHOD_(HRESULT,QueryInterface)(THIS_ REFIID,void **) PURE;
150     STDMETHOD_(ULONG,AddRef)(THIS) PURE;
151     STDMETHOD_(ULONG,Release)(THIS) PURE;
152     /*** unknown methods ***/
153     STDMETHOD_(HRESULT,unknown1)(THIS_ void *,void **) PURE;
154     STDMETHOD_(HRESULT,unknown2)(THIS_ void *,void *) PURE;
155     STDMETHOD_(HRESULT,unknown3)(THIS_ void *) PURE;
156 };
157 #undef INTERFACE
158 
159 HRESULT create_instance(const CLSID *clsid, const IID *iid, void **ppv);
160 
161 typedef HRESULT(*class_constructor)(REFIID,void**);
162 extern HRESULT FormatConverter_CreateInstance(REFIID riid, void** ppv);
163 extern HRESULT ImagingFactory_CreateInstance(REFIID riid, void** ppv);
164 extern HRESULT BmpDecoder_CreateInstance(REFIID riid, void** ppv);
165 extern HRESULT PngDecoder_CreateInstance(REFIID iid, void** ppv);
166 extern HRESULT PngEncoder_CreateInstance(REFIID iid, void** ppv);
167 extern HRESULT BmpEncoder_CreateInstance(REFIID iid, void** ppv);
168 extern HRESULT DibDecoder_CreateInstance(REFIID iid, void** ppv);
169 extern HRESULT GifDecoder_CreateInstance(REFIID riid, void** ppv);
170 extern HRESULT GifEncoder_CreateInstance(REFIID iid, void** ppv);
171 extern HRESULT IcoDecoder_CreateInstance(REFIID iid, void** ppv);
172 extern HRESULT JpegDecoder_CreateInstance(REFIID iid, void** ppv);
173 extern HRESULT JpegEncoder_CreateInstance(REFIID iid, void** ppv);
174 extern HRESULT TiffDecoder_CreateInstance(REFIID iid, void** ppv);
175 extern HRESULT TiffEncoder_CreateInstance(REFIID iid, void** ppv);
176 extern HRESULT TgaDecoder_CreateInstance(REFIID iid, void** ppv);
177 extern HRESULT DdsDecoder_CreateInstance(REFIID iid, void** ppv);
178 extern HRESULT DdsEncoder_CreateInstance(REFIID iid, void** ppv);
179 
180 extern HRESULT BitmapImpl_Create(UINT uiWidth, UINT uiHeight,
181     UINT stride, UINT datasize, void *view, UINT offset,
182     REFWICPixelFormatGUID pixelFormat, WICBitmapCreateCacheOption option,
183     IWICBitmap **ppIBitmap);
184 extern HRESULT BitmapScaler_Create(IWICBitmapScaler **scaler);
185 extern HRESULT FlipRotator_Create(IWICBitmapFlipRotator **fliprotator);
186 extern HRESULT PaletteImpl_Create(IWICPalette **palette);
187 extern HRESULT StreamImpl_Create(IWICStream **stream);
188 extern HRESULT ColorContext_Create(IWICColorContext **context);
189 extern HRESULT ColorTransform_Create(IWICColorTransform **transform);
190 extern HRESULT BitmapClipper_Create(IWICBitmapClipper **clipper);
191 
192 extern HRESULT copy_pixels(UINT bpp, const BYTE *srcbuffer,
193     UINT srcwidth, UINT srcheight, INT srcstride,
194     const WICRect *rc, UINT dststride, UINT dstbuffersize, BYTE *dstbuffer);
195 
196 extern HRESULT configure_write_source(IWICBitmapFrameEncode *iface,
197     IWICBitmapSource *source, const WICRect *prc,
198     const WICPixelFormatGUID *format,
199     INT width, INT height, double xres, double yres);
200 
201 extern HRESULT write_source(IWICBitmapFrameEncode *iface,
202     IWICBitmapSource *source, const WICRect *prc,
203     const WICPixelFormatGUID *format, UINT bpp, BOOL need_palette,
204     INT width, INT height);
205 
206 extern void reverse_bgr8(UINT bytesperpixel, LPBYTE bits, UINT width, UINT height, INT stride);
207 
208 extern HRESULT get_pixelformat_bpp(const GUID *pixelformat, UINT *bpp);
209 
210 extern HRESULT CreatePropertyBag2(const PROPBAG2 *options, UINT count,
211                                   IPropertyBag2 **property);
212 
213 extern HRESULT CreateComponentInfo(REFCLSID clsid, IWICComponentInfo **ppIInfo);
214 extern void ReleaseComponentInfos(void);
215 extern HRESULT CreateComponentEnumerator(DWORD componentTypes, DWORD options, IEnumUnknown **ppIEnumUnknown);
216 extern HRESULT get_decoder_info(REFCLSID clsid, IWICBitmapDecoderInfo **info);
217 
218 typedef struct BmpDecoder BmpDecoder;
219 
220 extern HRESULT IcoDibDecoder_CreateInstance(BmpDecoder **ppDecoder);
221 extern void BmpDecoder_GetWICDecoder(BmpDecoder *This, IWICBitmapDecoder **ppDecoder);
222 extern void BmpDecoder_FindIconMask(BmpDecoder *This, ULONG *mask_offset, int *topdown);
223 
224 typedef struct _MetadataItem
225 {
226     PROPVARIANT schema;
227     PROPVARIANT id;
228     PROPVARIANT value;
229 } MetadataItem;
230 
231 typedef struct _MetadataHandlerVtbl
232 {
233     int is_writer;
234     const CLSID *clsid;
235     HRESULT (*fnLoad)(IStream *stream, const GUID *preferred_vendor,
236         DWORD persist_options, MetadataItem **items, DWORD *item_count);
237     HRESULT (*fnSave)(IStream *stream, DWORD persist_options,
238         const MetadataItem *items, DWORD item_count);
239     HRESULT (*fnGetSizeMax)(const MetadataItem *items, DWORD item_count,
240         ULARGE_INTEGER *size);
241 } MetadataHandlerVtbl;
242 
243 extern HRESULT MetadataReader_Create(const MetadataHandlerVtbl *vtable, REFIID iid, void** ppv);
244 
245 extern HRESULT UnknownMetadataReader_CreateInstance(REFIID iid, void** ppv);
246 extern HRESULT IfdMetadataReader_CreateInstance(REFIID iid, void **ppv);
247 extern HRESULT PngChrmReader_CreateInstance(REFIID iid, void** ppv);
248 extern HRESULT PngGamaReader_CreateInstance(REFIID iid, void** ppv);
249 extern HRESULT PngHistReader_CreateInstance(REFIID iid, void** ppv);
250 extern HRESULT PngTextReader_CreateInstance(REFIID iid, void** ppv);
251 extern HRESULT PngTimeReader_CreateInstance(REFIID iid, void** ppv);
252 extern HRESULT LSDReader_CreateInstance(REFIID iid, void **ppv);
253 extern HRESULT IMDReader_CreateInstance(REFIID iid, void **ppv);
254 extern HRESULT GCEReader_CreateInstance(REFIID iid, void **ppv);
255 extern HRESULT APEReader_CreateInstance(REFIID iid, void **ppv);
256 extern HRESULT GifCommentReader_CreateInstance(REFIID iid, void **ppv);
257 extern HRESULT MetadataQueryReader_CreateInstance(IWICMetadataBlockReader *, const WCHAR *, IWICMetadataQueryReader **);
258 extern HRESULT MetadataQueryWriter_CreateInstance(IWICMetadataBlockWriter *, const WCHAR *, IWICMetadataQueryWriter **);
259 extern HRESULT stream_initialize_from_filehandle(IWICStream *iface, HANDLE hfile);
260 
debug_wic_rect(const WICRect * rect)261 static inline const char *debug_wic_rect(const WICRect *rect)
262 {
263     if (!rect) return "(null)";
264     return wine_dbg_sprintf("(%u,%u)-(%u,%u)", rect->X, rect->Y, rect->Width, rect->Height);
265 }
266 
267 extern HMODULE windowscodecs_module;
268 
269 HRESULT read_png_chunk(IStream *stream, BYTE *type, BYTE **data, ULONG *data_size);
270 
271 struct decoder_funcs;
272 
273 struct decoder_info
274 {
275     GUID container_format;
276     GUID block_format;
277     CLSID clsid;
278 };
279 
280 #define DECODER_FLAGS_CAPABILITY_MASK 0x1f
281 #define DECODER_FLAGS_UNSUPPORTED_COLOR_CONTEXT 0x80000000
282 
283 struct decoder_stat
284 {
285     DWORD flags;
286     UINT frame_count;
287 };
288 
289 struct decoder_frame
290 {
291     CLSID pixel_format;
292     UINT width, height;
293     UINT bpp;
294     DOUBLE dpix, dpiy;
295     DWORD num_color_contexts;
296     DWORD num_colors;
297     WICColor palette[256];
298 };
299 
300 #define DECODER_BLOCK_OPTION_MASK 0x0001000F
301 #define DECODER_BLOCK_FULL_STREAM 0x80000000
302 #define DECODER_BLOCK_READER_CLSID 0x40000000
303 struct decoder_block
304 {
305     ULONGLONG offset;
306     ULONGLONG length;
307     DWORD options;
308     GUID reader_clsid;
309 };
310 
311 struct decoder
312 {
313     const struct decoder_funcs *vtable;
314 };
315 
316 struct decoder_funcs
317 {
318     HRESULT (CDECL *initialize)(struct decoder* This, IStream *stream, struct decoder_stat *st);
319     HRESULT (CDECL *get_frame_info)(struct decoder* This, UINT frame, struct decoder_frame *info);
320     HRESULT (CDECL *copy_pixels)(struct decoder* This, UINT frame, const WICRect *prc,
321         UINT stride, UINT buffersize, BYTE *buffer);
322     HRESULT (CDECL *get_metadata_blocks)(struct decoder* This, UINT frame, UINT *count,
323         struct decoder_block **blocks);
324     HRESULT (CDECL *get_color_context)(struct decoder* This, UINT frame, UINT num,
325         BYTE **data, DWORD *datasize);
326     void (CDECL *destroy)(struct decoder* This);
327 };
328 
329 HRESULT CDECL stream_getsize(IStream *stream, ULONGLONG *size);
330 HRESULT CDECL stream_read(IStream *stream, void *buffer, ULONG read, ULONG *bytes_read);
331 HRESULT CDECL stream_seek(IStream *stream, LONGLONG ofs, DWORD origin, ULONGLONG *new_position);
332 HRESULT CDECL stream_write(IStream *stream, const void *buffer, ULONG write, ULONG *bytes_written);
333 
334 HRESULT CDECL decoder_create(const CLSID *decoder_clsid, struct decoder_info *info, struct decoder **result);
335 HRESULT CDECL decoder_initialize(struct decoder *This, IStream *stream, struct decoder_stat *st);
336 HRESULT CDECL decoder_get_frame_info(struct decoder* This, UINT frame, struct decoder_frame *info);
337 HRESULT CDECL decoder_copy_pixels(struct decoder* This, UINT frame, const WICRect *prc,
338     UINT stride, UINT buffersize, BYTE *buffer);
339 HRESULT CDECL decoder_get_metadata_blocks(struct decoder* This, UINT frame, UINT *count,
340     struct decoder_block **blocks);
341 HRESULT CDECL decoder_get_color_context(struct decoder* This, UINT frame, UINT num,
342     BYTE **data, DWORD *datasize);
343 void CDECL decoder_destroy(struct decoder *This);
344 
345 struct encoder_funcs;
346 
347 /* sync with encoder_option_properties */
348 enum encoder_option
349 {
350     ENCODER_OPTION_INTERLACE,
351     ENCODER_OPTION_FILTER,
352     ENCODER_OPTION_COMPRESSION_METHOD,
353     ENCODER_OPTION_COMPRESSION_QUALITY,
354     ENCODER_OPTION_IMAGE_QUALITY,
355     ENCODER_OPTION_BITMAP_TRANSFORM,
356     ENCODER_OPTION_LUMINANCE,
357     ENCODER_OPTION_CHROMINANCE,
358     ENCODER_OPTION_YCRCB_SUBSAMPLING,
359     ENCODER_OPTION_SUPPRESS_APP0,
360     ENCODER_OPTION_END
361 };
362 
363 #define ENCODER_FLAGS_MULTI_FRAME 0x1
364 #define ENCODER_FLAGS_ICNS_SIZE 0x2
365 #define ENCODER_FLAGS_SUPPORTS_METADATA 0x4
366 
367 struct encoder_info
368 {
369     DWORD flags;
370     GUID container_format;
371     CLSID clsid;
372     DWORD encoder_options[7];
373 };
374 
375 struct encoder_frame
376 {
377     GUID pixel_format;
378     UINT width, height;
379     UINT bpp;
380     BOOL indexed;
381     DOUBLE dpix, dpiy;
382     UINT num_colors;
383     WICColor palette[256];
384     /* encoder options */
385     BOOL interlace;
386     DWORD filter;
387 };
388 
389 struct encoder
390 {
391     const struct encoder_funcs *vtable;
392 };
393 
394 struct encoder_funcs
395 {
396     HRESULT (CDECL *initialize)(struct encoder* This, IStream *stream);
397     HRESULT (CDECL *get_supported_format)(struct encoder* This, GUID *pixel_format, DWORD *bpp, BOOL *indexed);
398     HRESULT (CDECL *create_frame)(struct encoder* This, const struct encoder_frame *frame);
399     HRESULT (CDECL *write_lines)(struct encoder* This, BYTE *data, DWORD line_count, DWORD stride);
400     HRESULT (CDECL *commit_frame)(struct encoder* This);
401     HRESULT (CDECL *commit_file)(struct encoder* This);
402     void (CDECL *destroy)(struct encoder* This);
403 };
404 
405 HRESULT CDECL encoder_initialize(struct encoder* This, IStream *stream);
406 HRESULT CDECL encoder_get_supported_format(struct encoder* This, GUID *pixel_format, DWORD *bpp, BOOL *indexed);
407 HRESULT CDECL encoder_create_frame(struct encoder* This, const struct encoder_frame *frame);
408 HRESULT CDECL encoder_write_lines(struct encoder* This, BYTE *data, DWORD line_count, DWORD stride);
409 HRESULT CDECL encoder_commit_frame(struct encoder* This);
410 HRESULT CDECL encoder_commit_file(struct encoder* This);
411 void CDECL encoder_destroy(struct encoder* This);
412 
413 HRESULT CDECL png_decoder_create(struct decoder_info *info, struct decoder **result);
414 HRESULT CDECL tiff_decoder_create(struct decoder_info *info, struct decoder **result);
415 HRESULT CDECL jpeg_decoder_create(struct decoder_info *info, struct decoder **result);
416 
417 HRESULT CDECL png_encoder_create(struct encoder_info *info, struct encoder **result);
418 HRESULT CDECL tiff_encoder_create(struct encoder_info *info, struct encoder **result);
419 HRESULT CDECL jpeg_encoder_create(struct encoder_info *info, struct encoder **result);
420 HRESULT CDECL icns_encoder_create(struct encoder_info *info, struct encoder **result);
421 
422 extern HRESULT CommonDecoder_CreateInstance(struct decoder *decoder,
423     const struct decoder_info *decoder_info, REFIID iid, void** ppv);
424 
425 extern HRESULT CommonEncoder_CreateInstance(struct encoder *encoder,
426     const struct encoder_info *encoder_info, REFIID iid, void** ppv);
427 
428 #endif /* WINCODECS_PRIVATE_H */
429