1 /*
2  * This library is free software; you can redistribute it and/or
3  * modify it under the terms of the GNU Lesser General Public
4  * License as published by the Free Software Foundation; either
5  * version 2.1 of the License, or (at your option) any later version.
6  *
7  * This library is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
10  * Lesser General Public License for more details.
11  *
12  * You should have received a copy of the GNU Lesser General Public
13  * License along with this library; if not, write to the Free Software
14  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
15  */
16 
17 #define COBJMACROS
18 
19 #include "objbase.h"
20 #include "wincodec.h"
21 #include "wine/test.h"
22 
23 static const char jpeg_adobe_cmyk_1x5[] =
24     "\xff\xd8\xff\xe0\x00\x10\x4a\x46\x49\x46\x00\x01\x01\x01\x01\x2c"
25     "\x01\x2c\x00\x00\xff\xee\x00\x0e\x41\x64\x6f\x62\x65\x00\x64\x00"
26     "\x00\x00\x00\x02\xff\xfe\x00\x13\x43\x72\x65\x61\x74\x65\x64\x20"
27     "\x77\x69\x74\x68\x20\x47\x49\x4d\x50\xff\xdb\x00\x43\x00\x01\x01"
28     "\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01"
29     "\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01"
30     "\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01"
31     "\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\xff\xdb"
32     "\x00\x43\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01"
33     "\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01"
34     "\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01"
35     "\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01"
36     "\x01\x01\x01\xff\xc0\x00\x14\x08\x00\x05\x00\x01\x04\x01\x11\x00"
37     "\x02\x11\x01\x03\x11\x01\x04\x11\x00\xff\xc4\x00\x15\x00\x01\x01"
38     "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x06\x08"
39     "\xff\xc4\x00\x14\x10\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
40     "\x00\x00\x00\x00\x00\x00\xff\xc4\x00\x14\x01\x01\x00\x00\x00\x00"
41     "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x0a\xff\xc4\x00\x14"
42     "\x11\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
43     "\x00\x00\xff\xda\x00\x0e\x04\x01\x00\x02\x11\x03\x11\x04\x00\x00"
44     "\x3f\x00\x40\x44\x02\x1e\xa4\x1f\xff\xd9";
45 
46 static void test_decode_adobe_cmyk(void)
47 {
48     IWICBitmapDecoder *decoder;
49     IWICBitmapFrameDecode *framedecode;
50     HRESULT hr;
51     HGLOBAL hjpegdata;
52     char *jpegdata;
53     IStream *jpegstream;
54     GUID guidresult;
55     UINT count=0, width=0, height=0;
56     BYTE imagedata[5 * 4] = {1};
57     UINT i;
58 
59     const BYTE expected_imagedata[5 * 4] = {
60         0x00, 0xb0, 0xfc, 0x6d,
61         0x00, 0xb0, 0xfc, 0x6d,
62         0x00, 0xb0, 0xfc, 0x6d,
63         0x00, 0xb0, 0xfc, 0x6d,
64         0x00, 0xb0, 0xfc, 0x6d,
65     };
66 
67     const BYTE expected_imagedata_24bpp[5 * 4] = {
68         0x0d, 0x4b, 0x94, 0x00,
69         0x0d, 0x4b, 0x94, 0x00,
70         0x0d, 0x4b, 0x94, 0x00,
71         0x0d, 0x4b, 0x94, 0x00,
72         0x0d, 0x4b, 0x94, 0x00,
73     };
74 
75     hr = CoCreateInstance(&CLSID_WICJpegDecoder, NULL, CLSCTX_INPROC_SERVER,
76         &IID_IWICBitmapDecoder, (void**)&decoder);
77     ok(SUCCEEDED(hr), "CoCreateInstance failed, hr=%x\n", hr);
78     if (FAILED(hr)) return;
79 
80     hjpegdata = GlobalAlloc(GMEM_MOVEABLE, sizeof(jpeg_adobe_cmyk_1x5));
81     ok(hjpegdata != 0, "GlobalAlloc failed\n");
82     if (hjpegdata)
83     {
84         jpegdata = GlobalLock(hjpegdata);
85         memcpy(jpegdata, jpeg_adobe_cmyk_1x5, sizeof(jpeg_adobe_cmyk_1x5));
86         GlobalUnlock(hjpegdata);
87 
88         hr = CreateStreamOnHGlobal(hjpegdata, FALSE, &jpegstream);
89         ok(SUCCEEDED(hr), "CreateStreamOnHGlobal failed, hr=%x\n", hr);
90         if (SUCCEEDED(hr))
91         {
92             hr = IWICBitmapDecoder_Initialize(decoder, jpegstream, WICDecodeMetadataCacheOnLoad);
93             ok(hr == S_OK, "Initialize failed, hr=%x\n", hr);
94 
95             hr = IWICBitmapDecoder_GetContainerFormat(decoder, &guidresult);
96             ok(SUCCEEDED(hr), "GetContainerFormat failed, hr=%x\n", hr);
97             ok(IsEqualGUID(&guidresult, &GUID_ContainerFormatJpeg), "unexpected container format\n");
98 
99             hr = IWICBitmapDecoder_GetFrameCount(decoder, &count);
100             ok(SUCCEEDED(hr), "GetFrameCount failed, hr=%x\n", hr);
101             ok(count == 1, "unexpected count %u\n", count);
102 
103             hr = IWICBitmapDecoder_GetFrame(decoder, 0, &framedecode);
104             ok(SUCCEEDED(hr), "GetFrame failed, hr=%x\n", hr);
105             if (SUCCEEDED(hr))
106             {
107                 hr = IWICBitmapFrameDecode_GetSize(framedecode, &width, &height);
108                 ok(SUCCEEDED(hr), "GetSize failed, hr=%x\n", hr);
109                 ok(width == 1, "expected width=1, got %u\n", width);
110                 ok(height == 5, "expected height=5, got %u\n", height);
111 
112                 hr = IWICBitmapFrameDecode_GetPixelFormat(framedecode, &guidresult);
113                 ok(SUCCEEDED(hr), "GetPixelFormat failed, hr=%x\n", hr);
114                 ok(IsEqualGUID(&guidresult, &GUID_WICPixelFormat32bppCMYK) ||
115                     broken(IsEqualGUID(&guidresult, &GUID_WICPixelFormat24bppBGR)), /* xp/2003 */
116                     "unexpected pixel format: %s\n", wine_dbgstr_guid(&guidresult));
117 
118                 /* We want to be sure our state tracking will not impact output
119                  * data on subsequent calls */
120                 for(i=2; i>0; --i)
121                 {
122                     hr = IWICBitmapFrameDecode_CopyPixels(framedecode, NULL, 4, sizeof(imagedata), imagedata);
123                     ok(SUCCEEDED(hr), "CopyPixels failed, hr=%x\n", hr);
124                     ok(!memcmp(imagedata, expected_imagedata, sizeof(imagedata)) ||
125                             broken(!memcmp(imagedata, expected_imagedata_24bpp, sizeof(expected_imagedata))), /* xp/2003 */
126                             "unexpected image data\n");
127                 }
128                 IWICBitmapFrameDecode_Release(framedecode);
129             }
130             IStream_Release(jpegstream);
131         }
132         GlobalFree(hjpegdata);
133     }
134     IWICBitmapDecoder_Release(decoder);
135 }
136 
137 
138 START_TEST(jpegformat)
139 {
140     CoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
141 
142     test_decode_adobe_cmyk();
143 
144     CoUninitialize();
145 }
146