Remove whitespace, update email addresses and URLs.
[kodak.git] / dc210photo.cpp
1 //+----------------------------------------------------------------------------
2 //
3 // File:     dc210photo.cpp
4 //
5 // Module:
6 //
7 // Synopsis:
8 //
9 // Copyright (C) 1999-2000 Scott Gasch <[email protected]>
10 //
11 // Created:  15 Jan 2000
12 //
13 //+----------------------------------------------------------------------------
14
15 #include <string.h>
16
17 #include "global.h"
18 #include "trace.h"
19 #include "debug.h"
20 #include "utils.h"
21 #include "dc210photo.h"
22
23
24 CKodakDC210Photo::CKodakDC210Photo(int iPictureNumber, CKodakDC210 *pCamera)
25 {
26         _fInitialized = false;
27         _pCamera = pCamera;
28
29         if (iPictureNumber)
30         {
31                 if (!_pCamera->SendCommand(DC210_PICTURE_INFO,
32                                                                    (((iPictureNumber - 1) & 0xFF00) >> 8),
33                                                                    (iPictureNumber - 1) & 0x00FF,
34                                                                    0,
35                                                                    0))
36                 {
37                         Trace("CKodakDC210Photo: Could not send command to camera.\n");
38                         ASSERT(FALSE);
39                         return;
40                 }
41                 
42                 if (!_pCamera->ReadPacket(_bRawPictureData, 256))
43                 {
44                         //
45                         // if this failes it means we have a bad picture -- the asked for
46                         // a number that is not in the camera right now.
47                         //
48                         iPictureNumber = 0;
49                         goto takepicture;
50                 }
51         
52                 if (!_pCamera->CommandComplete())
53                 {
54                         Trace("CKodakDC210Photo: Did not get command complete signal.\n");
55                         ASSERT(FALSE);
56                         return;
57                 }
58
59                 memcpy(_szFilename, &(_bRawPictureData[32]), 12);
60                 _szFilename[12] = 0;
61                 _iNumber = iPictureNumber;
62         }
63         else
64         {
65
66  takepicture:
67                 Trace("I should take a picture!\n\n");
68
69         }
70
71         _fInitialized = true;
72 }
73
74
75 CKodakDC210Photo::~CKodakDC210Photo(void)
76 {
77         _fInitialized = false;
78         _iNumber = 0;
79 }
80
81
82 DC210_COMPRESSION_STATE CKodakDC210Photo::GetCompression(void)
83 {
84         switch (_bRawPictureData[4])
85         {
86                 case 0:
87                         return(compression_none);
88                         break;
89                 case 1:
90                         return(compression_low);
91                         break;
92                 case 2:
93                         return(compression_medium);
94                         break;
95                 case 3:
96                         return(compression_high);
97                         break;
98                 default:
99                         Trace("GetCompression: Illegal compression value.\n");
100                         ASSERT(false);
101                         return(compression_none);
102         }
103 }
104
105
106 PICTURE_FORMAT CKodakDC210Photo::GetFormat(void)
107 {
108         if (2 == _bRawPictureData[2])
109         {
110                 return(pictureformat_raw);
111         }
112         else if (3 == _bRawPictureData[2])
113         {
114                 return(pictureformat_jpeg);
115         }
116         else if (4 == _bRawPictureData[2])
117         {
118                 return(pictureformat_flashpix);
119         }
120         else
121         {
122                 Trace("GetFormat: Illegal file format value.\n");
123                 ASSERT(false);
124                 return(pictureformat_unknown);
125         }
126 }
127
128
129 int CKodakDC210Photo::GetSize(void)
130 {
131         return(_bRawPictureData[11] |
132                    (_bRawPictureData[10] << 8) |
133                    (_bRawPictureData[9] << 16) |
134                    (_bRawPictureData[8] << 24));
135 }
136
137 int CKodakDC210Photo::GetTimeStamp(void)
138 {
139         return(_bRawPictureData[15] |
140                    (_bRawPictureData[14] << 8) |
141                    (_bRawPictureData[13] << 16) |
142                    (_bRawPictureData[12] << 24));
143 }
144
145 bool CKodakDC210Photo::GetResolution(int *piWidth, int *piHeight)
146 {
147         if (0 == _bRawPictureData[3])
148         {
149                 *piWidth = 640;
150                 *piHeight = 480;
151         }
152         else if (1 == _bRawPictureData[3])
153         {
154                 *piWidth = 1152;
155                 *piHeight = 864;
156         }
157         else
158         {
159                 Trace("GetResolution: Illegal resolution setting.\n");
160                 *piWidth = *piHeight = 0;
161                 ASSERT(FALSE);
162                 return(false);
163         }
164
165         //
166         // Success
167         //
168         return(true);
169 }
170
171 bool CKodakDC210Photo::WasFlashUsed(void)
172 {
173         return(_bRawPictureData[16]);
174 }
175
176 DC210_FLASH_STATE CKodakDC210Photo::GetFlashMode(void)
177 {
178         switch(_bRawPictureData[17])
179         {
180                 case 0:
181                         return(flash_auto);
182                         break;
183                 case 1:
184                         return(flash_fill);
185                         break;
186                 case 2:
187                         return(flash_off);
188                         break;
189                 case 3:
190                         return(flash_auto_redeye);
191                         break;
192                 case 4:
193                         return(flash_fill_redeye);
194                         break;
195                 default:
196                         Trace("GetFlashMode: Bad flash setting.\n");
197                         ASSERT(false);
198                         return(flash_unknown);
199         }
200 }
201
202 int CKodakDC210Photo::GetLightValueData(void)
203 {
204         return(_bRawPictureData[18]);
205 }
206
207
208 float CKodakDC210Photo::GetCalculatedLightValue(void)
209 {
210         return ((6.5 + (0.125 * ((float)_bRawPictureData[18]))));
211 }
212
213 DC210_ZOOM_STATE CKodakDC210Photo::GetZoomPosition(void)
214 {
215         switch (_bRawPictureData[21])
216         {
217                 case 0:
218                         return(zoom_fully_zoomed);
219                         break;
220                 case 1:
221                         return(zoom_3);
222                         break;
223                 case 2:
224                         return(zoom_2);
225                         break;
226                 case 3:
227                         return(zoom_1);
228                         break;
229                 case 4:
230                         return(zoom_wideangle);
231                         break;
232                 case 5:
233                         return(zoom_closeup);
234                         break;
235                 default:
236                         Trace("GetZoomPosition: Illegal zoom setting.\n");
237                         ASSERT(false);
238                         return(zoom_unknown);
239         }
240 }
241
242 int CKodakDC210Photo::GetExposureValue(void)
243 {
244         return(_bRawPictureData[23]);
245 }
246
247 int CKodakDC210Photo::GetApertureValue(void)
248 {
249         return(_bRawPictureData[26]);
250 }
251
252 int CKodakDC210Photo::GetExposureTimeMs(void)
253 {
254         return ((_bRawPictureData[31] |
255                         (_bRawPictureData[30] << 8) |
256                         (_bRawPictureData[29] << 16) |
257                         (_bRawPictureData[28] << 24)) * 10);
258 }
259
260 float CKodakDC210Photo::GetExposureCompensationValue(void)
261 {
262         float x;
263
264         if (_bRawPictureData[46] & 0x40)
265         {
266                 x = -1.0;
267         }
268         else
269         {
270                 x = 1.0;
271         }
272
273         x *= (_bRawPictureData[46] * 0.5);
274
275         return(x);
276 }
277
278
279 bool CKodakDC210Photo::GetData(BYTE **ppData, int *piLength)
280 {
281         int iSize;
282         int iBytesRead = 0;
283         BYTE bMsb, bLsb;
284         BYTE *buf = NULL;
285         BYTE *pbWritePos = NULL;
286
287         //
288         // Figure out how big the image is (in bytes).
289         //
290         if (0 == (iSize = GetSize()))
291         {
292                 Trace("GetData: picture has no size?\n");
293                 return(false);
294         }
295
296         //
297         // Allocate the buffer
298         //
299         buf = (BYTE *) malloc(1024 * ((iSize / 1024) + 1));
300         if (!buf)
301         {
302                 Trace("GetData: Out of memory.\n");
303                 return(false);
304         }
305         pbWritePos = buf;
306
307         //
308         // Ask the camera to send the image.
309         //
310         bMsb = ((_iNumber - 1) & 0xff00) >> 8;
311         bLsb = (_iNumber - 1) & 0x00ff;
312         if (!_pCamera->SendCommand(DC210_PICTURE_DOWNLOAD, bMsb, bLsb, 0, 0))
313         {
314                 Trace("GetData: failed to send command.\n");
315                 ASSERT(FALSE);
316                 return(false);
317         }
318
319         printf("Downloading: \n\n");
320         iBytesRead = 0;
321         while (iBytesRead < iSize)
322         {
323                 printf("%d / %d\n", iBytesRead, iSize);
324                 if (!_pCamera->ReadPacket(pbWritePos, 1024))
325                 {
326                         Trace("GetData: error reading data packets from camera.\n");
327                         ASSERT(FALSE);
328                         return(false);
329                 }
330                 pbWritePos += 1024;
331                 iBytesRead += 1024;
332         }
333
334         buf = (BYTE *) realloc(buf, iSize);
335         if (!buf)
336         {
337                 Trace("GetData: Out of memory in realloc\n");
338                 return(false);
339         }
340
341         //
342         // Wait for the camera's command complete code.
343         //
344         if (!_pCamera->CommandComplete())
345         {
346                 Trace("GetData: Did not receive command completion signal.\n");
347                 return(false);
348         }
349
350         //
351         // Success
352         //
353         *ppData = buf;
354         *piLength = iSize;
355         return(true);
356 }
357
358 #if 0
359 bool CKodakDC210Photo::WriteData(void)
360 {
361         int iSize;
362         int iFd;
363         int iBytesRead = 0;
364         int iBytesWritten = 0;
365         int iAmountNeeded;
366         BYTE bMsb, bLsb;
367         BYTE buf[1024];
368         char szFileName[13];
369
370         if (!GetImageName(szFileName))
371         {
372                 return(false);
373         }
374
375         //
376         // Open a file to write this data to.
377         //
378         iFd = open(szFileName, O_WRONLY | O_APPEND | O_CREAT | O_TRUNC | O_EXLOCK);
379         if (!iFd)
380         {
381                 Trace("GetData: can't open file.\n");
382                 return(false);
383         }
384         fchmod(iFd, S_IRWXU);
385
386         //
387         // Figure out how big the image is (in bytes).
388         //
389         if ((!GetSize(&iSize)) || (iSize <= 0))
390         {
391                 Trace("GetData: picture has no size?\n");
392                 return(false);
393         }
394
395         //
396         // Ask the camera to send the image.
397         //
398         bMsb = (m_iNumber - 1) & 0xff00;
399         bLsb = (m_iNumber - 1) & 0x00ff;
400         if (!s_pCamera->SendCommand(DC210_PICTURE_DOWNLOAD, bMsb, bLsb, 0, 0))
401         {
402                 Trace("GetData: failed to send command.\n");
403                 return(false);
404         }
405
406         printf("Downloading: \n\n");
407         iBytesRead = 0;
408         while (iBytesRead < iSize)
409         {
410                 printf("%d / %d\n", iBytesRead, iSize);
411                 if (!s_pCamera->ReadPacket(buf, 1024))
412                 {
413                         Trace("GetData: error reading data packets from camera.\n");
414                         return(false);
415                 }
416                 iBytesRead += 1024;
417
418                 //
419                 // This was the last packet, only get the part we need...
420                 //
421                 if (iBytesRead > iSize)
422                 {
423                         iAmountNeeded = (1024 - (iBytesRead - iSize));
424                         iBytesWritten = write(iFd, buf, iAmountNeeded);
425                         if (iBytesWritten != iAmountNeeded)
426                         {
427                                 Trace("GetData: write error.\n");
428                                 return(false);
429                         }
430                         break;
431                 }
432                 else
433                 {
434                         iBytesWritten = write(iFd, buf, 1024);
435                         if (iBytesWritten != 1024)
436                         {
437                                 Trace("GetData: write error.\n");
438                                 return(false);
439                         }
440                 }
441         }
442
443         //
444         // Wait for the camera's command complete code.
445         //
446         if (!s_pCamera->CommandComplete())
447         {
448                 Trace("GetData: Did not receive command completion signal.\n");
449                 return(false);
450         }
451
452         //
453         // Success
454         //
455         close(iFd);
456         return(true);
457 }
458
459 bool CDC210Picture::Delete(void)
460 {
461         return(true);
462 }
463 #endif