//+---------------------------------------------------------------------------- // // File: dc210photo.cpp // // Module: // // Synopsis: // // Copyright (C) 1999-2000 Scott Gasch // // Created: 15 Jan 2000 // //+---------------------------------------------------------------------------- #include #include "global.h" #include "trace.h" #include "debug.h" #include "utils.h" #include "dc210photo.h" CKodakDC210Photo::CKodakDC210Photo(int iPictureNumber, CKodakDC210 *pCamera) { _fInitialized = false; _pCamera = pCamera; if (iPictureNumber) { if (!_pCamera->SendCommand(DC210_PICTURE_INFO, (((iPictureNumber - 1) & 0xFF00) >> 8), (iPictureNumber - 1) & 0x00FF, 0, 0)) { Trace("CKodakDC210Photo: Could not send command to camera.\n"); ASSERT(FALSE); return; } if (!_pCamera->ReadPacket(_bRawPictureData, 256)) { // // if this failes it means we have a bad picture -- the asked for // a number that is not in the camera right now. // iPictureNumber = 0; goto takepicture; } if (!_pCamera->CommandComplete()) { Trace("CKodakDC210Photo: Did not get command complete signal.\n"); ASSERT(FALSE); return; } memcpy(_szFilename, &(_bRawPictureData[32]), 12); _szFilename[12] = 0; _iNumber = iPictureNumber; } else { takepicture: Trace("I should take a picture!\n\n"); } _fInitialized = true; } CKodakDC210Photo::~CKodakDC210Photo(void) { _fInitialized = false; _iNumber = 0; } DC210_COMPRESSION_STATE CKodakDC210Photo::GetCompression(void) { switch (_bRawPictureData[4]) { case 0: return(compression_none); break; case 1: return(compression_low); break; case 2: return(compression_medium); break; case 3: return(compression_high); break; default: Trace("GetCompression: Illegal compression value.\n"); ASSERT(false); return(compression_none); } } PICTURE_FORMAT CKodakDC210Photo::GetFormat(void) { if (2 == _bRawPictureData[2]) { return(pictureformat_raw); } else if (3 == _bRawPictureData[2]) { return(pictureformat_jpeg); } else if (4 == _bRawPictureData[2]) { return(pictureformat_flashpix); } else { Trace("GetFormat: Illegal file format value.\n"); ASSERT(false); return(pictureformat_unknown); } } int CKodakDC210Photo::GetSize(void) { return(_bRawPictureData[11] | (_bRawPictureData[10] << 8) | (_bRawPictureData[9] << 16) | (_bRawPictureData[8] << 24)); } int CKodakDC210Photo::GetTimeStamp(void) { return(_bRawPictureData[15] | (_bRawPictureData[14] << 8) | (_bRawPictureData[13] << 16) | (_bRawPictureData[12] << 24)); } bool CKodakDC210Photo::GetResolution(int *piWidth, int *piHeight) { if (0 == _bRawPictureData[3]) { *piWidth = 640; *piHeight = 480; } else if (1 == _bRawPictureData[3]) { *piWidth = 1152; *piHeight = 864; } else { Trace("GetResolution: Illegal resolution setting.\n"); *piWidth = *piHeight = 0; ASSERT(FALSE); return(false); } // // Success // return(true); } bool CKodakDC210Photo::WasFlashUsed(void) { return(_bRawPictureData[16]); } DC210_FLASH_STATE CKodakDC210Photo::GetFlashMode(void) { switch(_bRawPictureData[17]) { case 0: return(flash_auto); break; case 1: return(flash_fill); break; case 2: return(flash_off); break; case 3: return(flash_auto_redeye); break; case 4: return(flash_fill_redeye); break; default: Trace("GetFlashMode: Bad flash setting.\n"); ASSERT(false); return(flash_unknown); } } int CKodakDC210Photo::GetLightValueData(void) { return(_bRawPictureData[18]); } float CKodakDC210Photo::GetCalculatedLightValue(void) { return ((6.5 + (0.125 * ((float)_bRawPictureData[18])))); } DC210_ZOOM_STATE CKodakDC210Photo::GetZoomPosition(void) { switch (_bRawPictureData[21]) { case 0: return(zoom_fully_zoomed); break; case 1: return(zoom_3); break; case 2: return(zoom_2); break; case 3: return(zoom_1); break; case 4: return(zoom_wideangle); break; case 5: return(zoom_closeup); break; default: Trace("GetZoomPosition: Illegal zoom setting.\n"); ASSERT(false); return(zoom_unknown); } } int CKodakDC210Photo::GetExposureValue(void) { return(_bRawPictureData[23]); } int CKodakDC210Photo::GetApertureValue(void) { return(_bRawPictureData[26]); } int CKodakDC210Photo::GetExposureTimeMs(void) { return ((_bRawPictureData[31] | (_bRawPictureData[30] << 8) | (_bRawPictureData[29] << 16) | (_bRawPictureData[28] << 24)) * 10); } float CKodakDC210Photo::GetExposureCompensationValue(void) { float x; if (_bRawPictureData[46] & 0x40) { x = -1.0; } else { x = 1.0; } x *= (_bRawPictureData[46] * 0.5); return(x); } bool CKodakDC210Photo::GetData(BYTE **ppData, int *piLength) { int iSize; int iBytesRead = 0; BYTE bMsb, bLsb; BYTE *buf = NULL; BYTE *pbWritePos = NULL; // // Figure out how big the image is (in bytes). // if (0 == (iSize = GetSize())) { Trace("GetData: picture has no size?\n"); return(false); } // // Allocate the buffer // buf = (BYTE *) malloc(1024 * ((iSize / 1024) + 1)); if (!buf) { Trace("GetData: Out of memory.\n"); return(false); } pbWritePos = buf; // // Ask the camera to send the image. // bMsb = ((_iNumber - 1) & 0xff00) >> 8; bLsb = (_iNumber - 1) & 0x00ff; if (!_pCamera->SendCommand(DC210_PICTURE_DOWNLOAD, bMsb, bLsb, 0, 0)) { Trace("GetData: failed to send command.\n"); ASSERT(FALSE); return(false); } printf("Downloading: \n\n"); iBytesRead = 0; while (iBytesRead < iSize) { printf("%d / %d\n", iBytesRead, iSize); if (!_pCamera->ReadPacket(pbWritePos, 1024)) { Trace("GetData: error reading data packets from camera.\n"); ASSERT(FALSE); return(false); } pbWritePos += 1024; iBytesRead += 1024; } buf = (BYTE *) realloc(buf, iSize); if (!buf) { Trace("GetData: Out of memory in realloc\n"); return(false); } // // Wait for the camera's command complete code. // if (!_pCamera->CommandComplete()) { Trace("GetData: Did not receive command completion signal.\n"); return(false); } // // Success // *ppData = buf; *piLength = iSize; return(true); } #if 0 bool CKodakDC210Photo::WriteData(void) { int iSize; int iFd; int iBytesRead = 0; int iBytesWritten = 0; int iAmountNeeded; BYTE bMsb, bLsb; BYTE buf[1024]; char szFileName[13]; if (!GetImageName(szFileName)) { return(false); } // // Open a file to write this data to. // iFd = open(szFileName, O_WRONLY | O_APPEND | O_CREAT | O_TRUNC | O_EXLOCK); if (!iFd) { Trace("GetData: can't open file.\n"); return(false); } fchmod(iFd, S_IRWXU); // // Figure out how big the image is (in bytes). // if ((!GetSize(&iSize)) || (iSize <= 0)) { Trace("GetData: picture has no size?\n"); return(false); } // // Ask the camera to send the image. // bMsb = (m_iNumber - 1) & 0xff00; bLsb = (m_iNumber - 1) & 0x00ff; if (!s_pCamera->SendCommand(DC210_PICTURE_DOWNLOAD, bMsb, bLsb, 0, 0)) { Trace("GetData: failed to send command.\n"); return(false); } printf("Downloading: \n\n"); iBytesRead = 0; while (iBytesRead < iSize) { printf("%d / %d\n", iBytesRead, iSize); if (!s_pCamera->ReadPacket(buf, 1024)) { Trace("GetData: error reading data packets from camera.\n"); return(false); } iBytesRead += 1024; // // This was the last packet, only get the part we need... // if (iBytesRead > iSize) { iAmountNeeded = (1024 - (iBytesRead - iSize)); iBytesWritten = write(iFd, buf, iAmountNeeded); if (iBytesWritten != iAmountNeeded) { Trace("GetData: write error.\n"); return(false); } break; } else { iBytesWritten = write(iFd, buf, 1024); if (iBytesWritten != 1024) { Trace("GetData: write error.\n"); return(false); } } } // // Wait for the camera's command complete code. // if (!s_pCamera->CommandComplete()) { Trace("GetData: Did not receive command completion signal.\n"); return(false); } // // Success // close(iFd); return(true); } bool CDC210Picture::Delete(void) { return(true); } #endif