Added some more tests.
[wine] / dlls / twain / ds_image.c
1 /*
2  * Copyright 2000 Corel Corporation
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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
17  */
18
19 #include "config.h"
20
21 #include "winbase.h"
22 #include "wingdi.h"
23 #include "winuser.h"
24 #include "twain.h"
25 #include "twain_i.h"
26 #include "wine/debug.h"
27
28 WINE_DEFAULT_DEBUG_CHANNEL(twain);
29
30 /* DG_IMAGE/DAT_CIECOLOR/MSG_GET */
31 TW_UINT16 TWAIN_CIEColorGet (pTW_IDENTITY pOrigin, pTW_IDENTITY pDest,
32                              TW_MEMREF pData)
33 {
34     FIXME ("stub!\n");
35
36     return TWRC_FAILURE;
37 }
38
39 /* DG_IMAGE/DAT_EXTIMAGEINFO/MSG_GET */
40 TW_UINT16 TWAIN_ExtImageInfoGet (pTW_IDENTITY pOrigin, pTW_IDENTITY pDest,
41                                  TW_MEMREF pData)
42 {
43     FIXME ("stub!\n");
44
45     return TWRC_FAILURE;
46 }
47
48 /* DG_IMAGE/DAT_GRAYRESPONSE/MSG_RESET */
49 TW_UINT16 TWAIN_GrayResponseReset (pTW_IDENTITY pOrigin, pTW_IDENTITY pDest,
50                                    TW_MEMREF pData)
51 {
52     FIXME ("stub!\n");
53
54     return TWRC_FAILURE;
55 }
56
57 /* DG_IMAGE/DAT_GRAYRESPONSE/MSG_SET */
58 TW_UINT16 TWAIN_GrayResponseSet (pTW_IDENTITY pOrigin, pTW_IDENTITY pDest,
59                                  TW_MEMREF pData)
60 {
61     FIXME ("stub!\n");
62
63     return TWRC_FAILURE;
64 }
65
66 /* DG_IMAGE/DAT_IMAGEFILEXFER/MSG_GET */
67 TW_UINT16 TWAIN_ImageFileXferGet (pTW_IDENTITY pOrigin, pTW_IDENTITY pDest,
68                                   TW_MEMREF pData)
69 {
70     FIXME ("stub!\n");
71
72     return TWRC_FAILURE;
73 }
74
75 /* DG_IMAGE/DAT_IMAGEINFO/MSG_GET */
76 TW_UINT16 TWAIN_ImageInfoGet (pTW_IDENTITY pOrigin, pTW_IDENTITY pDest,
77                               TW_MEMREF pData)
78 {
79 #ifndef HAVE_SANE
80     return TWRC_FAILURE;
81 #else
82     TW_UINT16 twRC = TWRC_SUCCESS;
83     pTW_IMAGEINFO pImageInfo = (pTW_IMAGEINFO) pData;
84     activeDS *pSource = TWAIN_LookupSource (pDest);
85     SANE_Status status;
86
87     TRACE("DG_IMAGE/DAT_IMAGEINFO/MSG_GET\n");
88
89     if (!pSource)
90     {
91         twRC = TWRC_FAILURE;
92         DSM_twCC = TWCC_BADDEST;
93     }
94     else if (pSource->currentState != 6 && pSource->currentState != 7)
95     {
96         twRC = TWRC_FAILURE;
97         pSource->twCC = TWCC_SEQERROR;
98     }
99     else
100     {
101         if (pSource->currentState == 6)
102         {
103             /* return general image description information about the image about to be transfer */
104             status = sane_get_parameters (pSource->deviceHandle, &pSource->sane_param);
105         }
106
107         pImageInfo->XResolution.Whole = -1;
108         pImageInfo->XResolution.Frac = 0;
109         pImageInfo->YResolution.Whole = -1;
110         pImageInfo->YResolution.Frac = 0;
111         pImageInfo->ImageWidth = pSource->sane_param.pixels_per_line;
112         pImageInfo->ImageLength = pSource->sane_param.lines;
113         if (pSource->sane_param.depth == 24)
114         {
115             pImageInfo->SamplesPerPixel = 3;
116             pImageInfo->BitsPerSample[0] = 8;
117             pImageInfo->BitsPerSample[1] = 8;
118             pImageInfo->BitsPerSample[2] = 8;
119             pImageInfo->BitsPerPixel = 24;
120             pImageInfo->Planar = TRUE;
121             pImageInfo->PixelType = TWPT_RGB;
122             pImageInfo->Compression = TWCP_NONE;
123         } 
124         else if (pSource->sane_param.depth == 8)
125         {
126             /* FIXME: fill the image info structure for 8-bit image */
127         }
128     }
129
130     return twRC;
131 #endif
132 }
133
134 /* DG_IMAGE/DAT_IMAGELAYOUT/MSG_GET */
135 TW_UINT16 TWAIN_ImageLayoutGet (pTW_IDENTITY pOrigin, pTW_IDENTITY pDest,
136                                 TW_MEMREF pData)
137 {
138     FIXME ("stub!\n");
139
140     return TWRC_FAILURE;
141 }
142
143 /* DG_IMAGE/DAT_IMAGELAYOUT/MSG_GETDEFAULT */
144 TW_UINT16 TWAIN_ImageLayoutGetDefault (pTW_IDENTITY pOrigin, pTW_IDENTITY pDest,
145                                        TW_MEMREF pData)
146 {
147     FIXME ("stub!\n");
148
149     return TWRC_FAILURE;
150 }
151
152 /* DG_IMAGE/DAT_IMAGELAYOUT/MSG_RESET */
153 TW_UINT16 TWAIN_ImageLayoutReset (pTW_IDENTITY pOrigin, pTW_IDENTITY pDest,
154                                   TW_MEMREF pData)
155 {
156     FIXME ("stub!\n");
157
158     return TWRC_FAILURE;
159 }
160
161 /* DG_IMAGE/DAT_IMAGELAYOUT/MSG_SET */
162 TW_UINT16 TWAIN_ImageLayoutSet (pTW_IDENTITY pOrigin, pTW_IDENTITY pDest,
163                                 TW_MEMREF pData)
164 {
165     FIXME ("stub!\n");
166
167     return TWRC_FAILURE;
168 }
169
170 /* DG_IMAGE/DAT_IMAGEMEMXFER/MSG_GET */
171 TW_UINT16 TWAIN_ImageMemXferGet (pTW_IDENTITY pOrigin, pTW_IDENTITY pDest,
172                                  TW_MEMREF pData)
173 {
174     FIXME ("stub!\n");
175
176     return TWRC_FAILURE;
177 }
178
179 /* DG_IMAGE/DAT_IMAGENATIVEXFER/MSG_GET */
180 TW_UINT16 TWAIN_ImageNativeXferGet (pTW_IDENTITY pOrigin, pTW_IDENTITY pDest,
181                                     TW_MEMREF pData)
182 {
183 #ifndef HAVE_SANE
184     return TWRC_FAILURE;
185 #else
186     TW_UINT16 twRC = TWRC_SUCCESS;
187     pTW_UINT32 pHandle = (pTW_UINT32) pData;
188     activeDS *pSource = TWAIN_LookupSource (pDest);
189     SANE_Status status;
190     SANE_Byte buffer[32*1024];
191     int buff_len;
192     HBITMAP hDIB;
193     BITMAPINFO bmpInfo;
194     VOID *pBits;
195     HDC dc;
196  
197     TRACE("DG_IMAGE/DAT_IMAGENATIVEXFER/MSG_GET\n");
198
199     if (!pSource)
200     {
201         twRC = TWRC_FAILURE;
202         DSM_twCC = TWCC_NODS;
203     }
204     else if (pSource->currentState != 6)
205     {
206         twRC = TWRC_FAILURE;
207         pSource->twCC = TWCC_SEQERROR;
208     }
209     else
210     {
211         /* Transfer an image from the source to the application */
212         status = sane_start (pSource->deviceHandle);
213         if (status != SANE_STATUS_GOOD)
214         {
215             WARN("sane_start: %s\n", sane_strstatus (status));
216             sane_cancel (pSource->deviceHandle);
217             pSource->twCC = TWCC_OPERATIONERROR;
218             return TWRC_FAILURE;
219         }
220         
221         status = sane_get_parameters (pSource->deviceHandle, &pSource->sane_param);
222         if (status != SANE_STATUS_GOOD)
223         {
224             WARN("sane_get_parameters: %s\n", sane_strstatus (status));
225             sane_cancel (pSource->deviceHandle);
226             pSource->twCC = TWCC_OPERATIONERROR;
227             return TWRC_FAILURE;
228         }
229
230         TRACE("Acquiring image %dx%dx%d bits (format=%d last=%d) from sane...\n"
231               , pSource->sane_param.pixels_per_line, pSource->sane_param.lines,
232               pSource->sane_param.depth, pSource->sane_param.format,
233               pSource->sane_param.last_frame);
234
235         ZeroMemory (&bmpInfo, sizeof (BITMAPINFO));
236         bmpInfo.bmiHeader.biSize = sizeof (BITMAPINFOHEADER);
237         bmpInfo.bmiHeader.biWidth = pSource->sane_param.pixels_per_line;
238         bmpInfo.bmiHeader.biHeight = pSource->sane_param.lines;
239         bmpInfo.bmiHeader.biPlanes = 1;
240         bmpInfo.bmiHeader.biBitCount = pSource->sane_param.depth;
241         bmpInfo.bmiHeader.biCompression = BI_RGB;
242         bmpInfo.bmiHeader.biSizeImage = 0;
243         bmpInfo.bmiHeader.biXPelsPerMeter = 0;
244         bmpInfo.bmiHeader.biYPelsPerMeter = 0;
245         bmpInfo.bmiHeader.biClrUsed = 1;
246         bmpInfo.bmiHeader.biClrImportant = 0;
247         bmpInfo.bmiColors[0].rgbBlue = 128;
248         bmpInfo.bmiColors[0].rgbGreen = 128;
249         bmpInfo.bmiColors[0].rgbRed = 128;
250         hDIB = CreateDIBSection ((dc = GetDC(pSource->hwndOwner)), &bmpInfo, 
251                                  DIB_RGB_COLORS, &pBits, 0, 0);
252         if (!hDIB)
253         {
254             sane_cancel (pSource->deviceHandle);
255             pSource->twCC = TWCC_LOWMEMORY;
256             return TWRC_FAILURE;
257         }
258             
259         do
260         {
261             status = sane_read (pSource->deviceHandle, buffer, 
262                                 sizeof (buffer),  &buff_len);
263             if (status == SANE_STATUS_GOOD)
264             {
265                 /* FIXME: put code for coverting the image data into DIB here */
266
267             }
268             else if (status != SANE_STATUS_EOF)
269             {
270                 WARN("sane_read: %s\n", sane_strstatus (status));
271                 sane_cancel (pSource->deviceHandle);
272                 pSource->twCC = TWCC_OPERATIONERROR;
273                 return TWRC_FAILURE;
274             }
275         } while (status == SANE_STATUS_GOOD);
276         
277         sane_cancel (pSource->deviceHandle); 
278         ReleaseDC (pSource->hwndOwner, dc);
279         *pHandle = hDIB;
280         twRC = TWRC_XFERDONE; 
281         pSource->twCC = TWCC_SUCCESS;
282         pSource->currentState = 7;
283     }
284     return twRC;
285 #endif
286 }
287
288 /* DG_IMAGE/DAT_JPEGCOMPRESSION/MSG_GET */
289 TW_UINT16 TWAIN_JPEGCompressionGet (pTW_IDENTITY pOrigin, pTW_IDENTITY pDest,
290                                     TW_MEMREF pData)
291 {
292     FIXME ("stub!\n");
293
294     return TWRC_FAILURE;
295 }
296
297 /* DG_IMAGE/DAT_JPEGCOMPRESSION/MSG_GETDEFAULT */
298 TW_UINT16 TWAIN_JPEGCompressionGetDefault (pTW_IDENTITY pOrigin,
299                                            pTW_IDENTITY pDest,
300                                            TW_MEMREF pData)
301 {
302     FIXME ("stub!\n");
303
304     return TWRC_FAILURE;
305 }
306
307 /* DG_IMAGE/DAT_JPEGCOMPRESSION/MSG_RESET */
308 TW_UINT16 TWAIN_JPEGCompressionReset (pTW_IDENTITY pOrigin, pTW_IDENTITY pDest,
309                                       TW_MEMREF pData)
310 {
311     FIXME ("stub!\n");
312
313     return TWRC_FAILURE;
314 }
315
316 /* DG_IMAGE/DAT_JPEGCOMPRESSION/MSG_SET */
317 TW_UINT16 TWAIN_JPEGCompressionSet (pTW_IDENTITY pOrigin, pTW_IDENTITY pDest,
318                                     TW_MEMREF pData)
319 {
320     FIXME ("stub!\n");
321
322     return TWRC_FAILURE;
323 }
324
325 /* DG_IMAGE/DAT_PALETTE8/MSG_GET */
326 TW_UINT16 TWAIN_Palette8Get (pTW_IDENTITY pOrigin, pTW_IDENTITY pDest,
327                              TW_MEMREF pData)
328 {
329     FIXME ("stub!\n");
330
331     return TWRC_FAILURE;
332 }
333
334 /* DG_IMAGE/DAT_PALETTE8/MSG_GETDEFAULT */
335 TW_UINT16 TWAIN_Palette8GetDefault (pTW_IDENTITY pOrigin, pTW_IDENTITY pDest,
336                                     TW_MEMREF pData)
337 {
338     FIXME ("stub!\n");
339
340     return TWRC_FAILURE;
341 }
342
343 /* DG_IMAGE/DAT_PALETTE8/MSG_RESET */
344 TW_UINT16 TWAIN_Palette8Reset (pTW_IDENTITY pOrigin, pTW_IDENTITY pDest,
345                                TW_MEMREF pData)
346 {
347     FIXME ("stub!\n");
348
349     return TWRC_FAILURE;
350 }
351
352 /* DG_IMAGE/DAT_PALETTE8/MSG_SET */
353 TW_UINT16 TWAIN_Palette8Set (pTW_IDENTITY pOrigin, pTW_IDENTITY pDest,
354                              TW_MEMREF pData)
355 {
356     FIXME ("stub!\n");
357
358     return TWRC_FAILURE;
359 }
360
361 /* DG_IMAGE/DAT_RGBRESPONSE/MSG_RESET */
362 TW_UINT16 TWAIN_RGBResponseReset (pTW_IDENTITY pOrigin, pTW_IDENTITY pDest,
363                                   TW_MEMREF pData)
364 {
365     FIXME ("stub!\n");
366
367     return TWRC_FAILURE;
368 }
369
370 /* DG_IMAGE/DAT_RGBRESPONSE/MSG_SET */
371 TW_UINT16 TWAIN_RGBResponseSet (pTW_IDENTITY pOrigin, pTW_IDENTITY pDest,
372                                 TW_MEMREF pData)
373 {
374     FIXME ("stub!\n");
375
376     return TWRC_FAILURE;
377 }