Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

649 lines
17 KiB

  1. //-------------------------------------------------------------------------
  2. //
  3. // Copyright (c) 1999 Microsoft Corporation.
  4. //
  5. // camopen.cpp
  6. //
  7. // Abstract:
  8. //
  9. // Enumerate disk images to emulate camera
  10. //
  11. // Author:
  12. //
  13. // Edward Reus 27/Jul/99
  14. // modeled after code by Mark Enstrom
  15. //
  16. //-------------------------------------------------------------------------
  17. #include <windows.h>
  18. #include <stdio.h>
  19. #include <objbase.h>
  20. #include <tchar.h>
  21. #include "sti.h"
  22. #include "ircamera.h"
  23. #include <irthread.h>
  24. extern HINSTANCE g_hInst; // Global hInstance
  25. #define __GLOBALPROPVARS__
  26. #include "resource.h"
  27. #include "defprop.h"
  28. //-------------------------------------------------------------------------
  29. // IrUsdDevice::CamOpenCamera()
  30. //
  31. // Initialize the IrTran-P camera driver.
  32. //
  33. // This is a helper called by IrUsdDevice::Initialize().
  34. //
  35. // Arguments:
  36. //
  37. // pGenericStatus - camera status
  38. //
  39. // Return Value:
  40. //
  41. // HRESULT - S_OK
  42. //
  43. //-------------------------------------------------------------------------
  44. HRESULT IrUsdDevice::CamOpenCamera( IN OUT CAMERA_STATUS *pCameraStatus )
  45. {
  46. HRESULT hr = S_OK;
  47. SYSTEMTIME SystemTime;
  48. WIAS_TRACE((g_hInst,"IrUsdDevice::CamOpenCamerai()"));
  49. //
  50. // Initialize camera state:
  51. //
  52. memset( pCameraStatus, 0, sizeof(CAMERA_STATUS) );
  53. pCameraStatus->FirmwareVersion = 0x00000001;
  54. pCameraStatus->NumPictTaken = 20;
  55. pCameraStatus->NumPictRemaining = 0;
  56. pCameraStatus->ThumbWidth = 80;
  57. pCameraStatus->ThumbHeight= 60;
  58. pCameraStatus->PictWidth = 300;
  59. pCameraStatus->PictHeight = 300;
  60. GetSystemTime( &(pCameraStatus->CameraTime) );
  61. return hr;
  62. }
  63. //-------------------------------------------------------------------------
  64. // IrUsdDevice::CamBuildImageTree()
  65. //
  66. // Build the tree of camera images by enumerating a disk directory for
  67. // all .JPG files.
  68. //
  69. // Arguments:
  70. //
  71. // pCamStatus - device status
  72. // ppRootFile - return new root of item tree
  73. //
  74. // Return Value:
  75. //
  76. // status
  77. //
  78. //-------------------------------------------------------------------------
  79. HRESULT IrUsdDevice::CamBuildImageTree( OUT CAMERA_STATUS *pCamStatus,
  80. OUT IWiaDrvItem **ppRootFile )
  81. {
  82. HRESULT hr = S_OK;
  83. WIAS_TRACE((g_hInst,"IrUsdDevice::CamBuildImageTree()"));
  84. //
  85. // Create the new image root:
  86. //
  87. BSTR bstrRoot = SysAllocString(L"Root");
  88. if (!bstrRoot)
  89. {
  90. return E_OUTOFMEMORY;
  91. }
  92. //
  93. // Call Wia service library to create new root item:
  94. //
  95. hr = wiasCreateDrvItem( WiaItemTypeFolder | WiaItemTypeRoot | WiaItemTypeDevice,
  96. bstrRoot,
  97. m_bstrRootFullItemName,
  98. (IWiaMiniDrv*)this,
  99. sizeof(IRCAM_IMAGE_CONTEXT),
  100. NULL,
  101. ppRootFile );
  102. SysFreeString(bstrRoot);
  103. if (FAILED(hr))
  104. {
  105. WIAS_ERROR((g_hInst,"ddevBuildDeviceItemTree, CreateDeviceItem failed"));
  106. return hr;
  107. }
  108. //
  109. // Enumerate the root directory:
  110. //
  111. CHAR *pszImageDirectory = GetImageDirectory();
  112. if (!pszImageDirectory)
  113. {
  114. return E_OUTOFMEMORY;
  115. }
  116. #ifdef UNICODE
  117. WCHAR wszPath[MAX_PATH];
  118. mbstowcs( wszPath, pszImageDirectory, strlen(pszImageDirectory) );
  119. hr = EnumDiskImages( *ppRootFile, wszPath );
  120. #else
  121. hr = EnumDiskImages( *ppRootFile, pszImageDirectory );
  122. #endif
  123. // Don't free pszImageDirectory!!
  124. return (hr);
  125. }
  126. //-------------------------------------------------------------------------
  127. // IrUsdDevice::EnumDiskImages()
  128. //
  129. // Walk through camera temp directory looking for JPEG files to pick up.
  130. //
  131. // Arguments:
  132. //
  133. // pRootFile
  134. // pwszDirName
  135. //
  136. // Return Value:
  137. //
  138. // status
  139. //
  140. //-------------------------------------------------------------------------
  141. HRESULT IrUsdDevice::EnumDiskImages( IWiaDrvItem *pRootFile,
  142. TCHAR *pszDirName )
  143. {
  144. HRESULT hr = E_FAIL;
  145. WIN32_FIND_DATA FindData;
  146. TCHAR *pTempName;
  147. WIAS_TRACE((g_hInst,"IrUsdDevice::EnumDiskImages()"));
  148. pTempName = (TCHAR*)ALLOC(MAX_PATH);
  149. if (!pTempName)
  150. {
  151. return E_OUTOFMEMORY;
  152. }
  153. _tcscpy( pTempName, pszDirName);
  154. _tcscat( pTempName, TEXT("\\*.jpg") );
  155. //
  156. // Look for files at the specified directory:
  157. //
  158. HANDLE hFile = FindFirstFile( pTempName, &FindData );
  159. if (hFile != INVALID_HANDLE_VALUE)
  160. {
  161. BOOL bStatus;
  162. do {
  163. //
  164. // Add an image to this folder.
  165. //
  166. // Create file name:
  167. //
  168. _tcscpy(pTempName, pszDirName);
  169. _tcscat(pTempName, TEXT("\\"));
  170. _tcscat(pTempName, FindData.cFileName);
  171. //
  172. // Create a new DrvItem for this image and add it to the
  173. // DrvItem tree.
  174. //
  175. IWiaDrvItem *pNewImage;
  176. hr = CreateItemFromFileName(
  177. WiaItemTypeFile | WiaItemTypeImage,
  178. pTempName,
  179. FindData.cFileName,
  180. &pNewImage);
  181. if (FAILED(hr))
  182. {
  183. break;
  184. }
  185. hr = pNewImage->AddItemToFolder(pRootFile);
  186. pNewImage->Release();
  187. //
  188. // Look for the next image:
  189. //
  190. bStatus = FindNextFile(hFile,&FindData);
  191. } while (bStatus);
  192. FindClose(hFile);
  193. }
  194. //
  195. // Now look for directories,
  196. // add a new PCAMERA_FILE for each sub directory found
  197. //
  198. _tcscpy(pTempName, pszDirName);
  199. _tcscat(pTempName, TEXT("\\*.*"));
  200. hFile = FindFirstFile( pTempName,&FindData );
  201. if (hFile != INVALID_HANDLE_VALUE)
  202. {
  203. BOOL bStatus;
  204. do {
  205. if ( (FindData.cFileName[0] != L'.')
  206. && (FindData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY))
  207. {
  208. //
  209. // Found a subdirectory:
  210. //
  211. _tcscpy(pTempName, pszDirName);
  212. _tcscat(pTempName, TEXT("\\"));
  213. _tcscat(pTempName, FindData.cFileName);
  214. //
  215. // Create a new folder for the sub-directory:
  216. //
  217. IWiaDrvItem *pNewFolder;
  218. hr = CreateItemFromFileName(
  219. WiaItemTypeFolder,
  220. pTempName,
  221. FindData.cFileName,
  222. &pNewFolder);
  223. if (FAILED(hr))
  224. {
  225. continue;
  226. }
  227. hr = pNewFolder->AddItemToFolder(pRootFile);
  228. pNewFolder->Release();
  229. if (hr == S_OK)
  230. {
  231. //
  232. // Enumerate the sub-folder
  233. //
  234. EnumDiskImages(pNewFolder, pTempName);
  235. }
  236. }
  237. bStatus = FindNextFile(hFile,&FindData);
  238. } while (bStatus);
  239. FindClose(hFile);
  240. }
  241. FREE(pTempName);
  242. return S_OK;
  243. }
  244. //-------------------------------------------------------------------------
  245. // IrUsdDevice::CreateItemFromFileName()
  246. //
  247. // Helper funtion used by EnumDiskImages to create dev items and names.
  248. //
  249. // Arguments:
  250. //
  251. // FolderType - type of item to create
  252. // pszPath - complete path name
  253. // pszName - file name
  254. // ppNewFolder - return new item
  255. //
  256. // Return Value:
  257. //
  258. // status
  259. //
  260. //-------------------------------------------------------------------------
  261. HRESULT IrUsdDevice::CreateItemFromFileName( LONG FolderType,
  262. TCHAR *pszPath,
  263. TCHAR *pszName,
  264. IWiaDrvItem **ppNewFolder )
  265. {
  266. HRESULT hr = S_OK;
  267. WCHAR wszFullItemName[MAX_PATH];
  268. WCHAR wszTemp[MAX_PATH];
  269. BSTR bstrItemName;
  270. BSTR bstrFullItemName;
  271. IWiaDrvItem *pNewFolder = 0;
  272. WIAS_TRACE((g_hInst,"IrUsdDevice::CreateItemFromFileName()"));
  273. *ppNewFolder = NULL;
  274. //
  275. // Convert path to wide char
  276. //
  277. CHAR *pszImageDirectory = ::GetImageDirectory();
  278. if (!pszImageDirectory)
  279. {
  280. return E_OUTOFMEMORY;
  281. }
  282. DWORD dwImageDirectoryLen = strlen(pszImageDirectory);
  283. #ifndef UNICODE
  284. MultiByteToWideChar( CP_ACP,
  285. 0,
  286. pszPath + dwImageDirectoryLen,
  287. strlen(pszPath) - dwImageDirectoryLen - 4,
  288. wszTemp,
  289. MAX_PATH);
  290. #else
  291. wcscpy(wszTemp, pszPath+dwImageDirectoryLen);
  292. #endif
  293. if (FolderType & ~WiaItemTypeFolder)
  294. {
  295. wszTemp[_tcslen(pszPath) - strlen(pszImageDirectory) - 4] = 0;
  296. }
  297. wcscpy(wszFullItemName, m_bstrRootFullItemName);
  298. wcscat(wszFullItemName, wszTemp);
  299. //
  300. // Convert item name to wide char:
  301. //
  302. #ifndef UNICODE
  303. MultiByteToWideChar( CP_ACP,
  304. 0,
  305. pszName,
  306. strlen(pszName)-4,
  307. wszTemp,
  308. MAX_PATH);
  309. #else
  310. wcscpy(wszTemp, pszName);
  311. #endif
  312. if (FolderType & ~WiaItemTypeFolder)
  313. {
  314. wszTemp[_tcslen(pszName)-4] = 0;
  315. }
  316. bstrItemName = SysAllocString(wszTemp);
  317. if (bstrItemName)
  318. {
  319. bstrFullItemName = SysAllocString(wszFullItemName);
  320. if (bstrFullItemName)
  321. {
  322. //
  323. // Call WIA to create new DrvItem
  324. //
  325. IRCAM_IMAGE_CONTEXT *pContext = 0;
  326. hr = wiasCreateDrvItem( FolderType,
  327. bstrItemName,
  328. bstrFullItemName,
  329. (IWiaMiniDrv *)this,
  330. sizeof(IRCAM_IMAGE_CONTEXT),
  331. (BYTE **)&pContext,
  332. &pNewFolder);
  333. if (hr == S_OK)
  334. {
  335. //
  336. // init device specific context (image path)
  337. //
  338. pContext->pszCameraImagePath = _tcsdup(pszPath);
  339. }
  340. else
  341. {
  342. WIAS_ERROR((g_hInst,"ddevBuildDeviceItemTree, wiasCreateDrvItem failed"));
  343. }
  344. SysFreeString(bstrFullItemName);
  345. }
  346. else
  347. {
  348. WIAS_ERROR((g_hInst,"ddevBuildDeviceItemTree, unable to allocate full item name"));
  349. hr = E_OUTOFMEMORY;
  350. }
  351. SysFreeString(bstrItemName);
  352. }
  353. else
  354. {
  355. WIAS_ERROR((g_hInst,"ddevBuildDeviceItemTree, unable to allocate item name"));
  356. hr = E_OUTOFMEMORY;
  357. }
  358. //
  359. // Assign output value and cleanup
  360. //
  361. if (hr == S_OK)
  362. {
  363. *ppNewFolder = pNewFolder;
  364. }
  365. else
  366. {
  367. //
  368. // delete item
  369. //
  370. }
  371. return hr;
  372. }
  373. //-------------------------------------------------------------------------
  374. // SetItemSize()
  375. //
  376. // Helper function to call wias to calc new item size
  377. //
  378. // Arguments:
  379. //
  380. // pWiasContext - item
  381. //
  382. // Return Value:
  383. //
  384. // Status
  385. //
  386. //-------------------------------------------------------------------------
  387. HRESULT SetItemSize( BYTE* pWiasContext )
  388. {
  389. HRESULT hr;
  390. MINIDRV_TRANSFER_CONTEXT drvTranCtx;
  391. memset( &drvTranCtx, 0, sizeof(MINIDRV_TRANSFER_CONTEXT) );
  392. hr = wiasReadPropGuid( pWiasContext,
  393. WIA_IPA_FORMAT,
  394. (GUID*)&drvTranCtx.guidFormatID,
  395. NULL,
  396. false );
  397. if (FAILED(hr))
  398. {
  399. return hr;
  400. }
  401. hr = wiasReadPropLong( pWiasContext,
  402. WIA_IPA_TYMED,
  403. (LONG*)&drvTranCtx.tymed,
  404. NULL,
  405. false );
  406. if (FAILED(hr))
  407. {
  408. return hr;
  409. }
  410. WIAS_TRACE((g_hInst,"SetItemSize(): tymed: %d",drvTranCtx.tymed));
  411. //
  412. // wias works for DIB and TIFF formats.
  413. //
  414. // Driver doesn't support JPEG
  415. //
  416. hr = wiasGetImageInformation(pWiasContext,
  417. WIAS_INIT_CONTEXT,
  418. &drvTranCtx);
  419. if (hr == S_OK)
  420. {
  421. WIAS_TRACE((g_hInst,"SetItemSize(): lItemSize: %d",drvTranCtx.lItemSize));
  422. hr = wiasWritePropLong(pWiasContext, WIA_IPA_ITEM_SIZE, drvTranCtx.lItemSize);
  423. hr = wiasWritePropLong(pWiasContext, WIA_IPA_BYTES_PER_LINE, drvTranCtx.cbWidthInBytes);
  424. }
  425. return hr;
  426. }
  427. //-------------------------------------------------------------------------
  428. // IrUsdDevice::InitImageInformation()
  429. //
  430. // Init image properties
  431. //
  432. // Arguments:
  433. //
  434. //
  435. // Return Value:
  436. //
  437. // Status
  438. //
  439. //-------------------------------------------------------------------------
  440. HRESULT IrUsdDevice::InitImageInformation( BYTE *pWiasContext,
  441. IRCAM_IMAGE_CONTEXT *pContext,
  442. LONG *plDevErrVal)
  443. {
  444. int i;
  445. HRESULT hr = S_OK;
  446. CAMERA_PICTURE_INFO camInfo;
  447. PROPVARIANT propVar;
  448. WIAS_TRACE((g_hInst,"IrUsdDevice::InitImageInformation()"));
  449. //
  450. // GET image info
  451. //
  452. hr = CamGetPictureInfo( pContext,
  453. &camInfo );
  454. if (hr != S_OK)
  455. {
  456. return hr;
  457. }
  458. //
  459. // Use WIA services to write image properties:
  460. //
  461. wiasWritePropLong( pWiasContext,
  462. WIA_IPC_THUMB_WIDTH,
  463. camInfo.ThumbWidth);
  464. wiasWritePropLong( pWiasContext,
  465. WIA_IPC_THUMB_HEIGHT,
  466. camInfo.ThumbHeight );
  467. wiasWritePropLong( pWiasContext,
  468. WIA_IPA_PIXELS_PER_LINE,
  469. camInfo.PictWidth );
  470. wiasWritePropLong( pWiasContext,
  471. WIA_IPA_NUMBER_OF_LINES,
  472. camInfo.PictHeight );
  473. wiasWritePropGuid( pWiasContext,
  474. WIA_IPA_PREFERRED_FORMAT,
  475. WiaImgFmt_JPEG );
  476. wiasWritePropLong( pWiasContext,
  477. WIA_IPA_DEPTH,
  478. camInfo.PictBitsPerPixel );
  479. wiasWritePropBin( pWiasContext,
  480. WIA_IPA_ITEM_TIME,
  481. sizeof(SYSTEMTIME),
  482. (PBYTE)&camInfo.TimeStamp );
  483. wiasWritePropLong( pWiasContext,
  484. WIA_IPA_DATATYPE,
  485. WIA_DATA_COLOR );
  486. wiasWritePropLong( pWiasContext,
  487. WIA_IPA_ITEM_SIZE,
  488. camInfo.PictCompSize );
  489. wiasWritePropLong( pWiasContext,
  490. WIA_IPA_BYTES_PER_LINE,
  491. camInfo.PictBytesPerRow );
  492. //
  493. // Calculate item size
  494. //
  495. // hr = SetItemSize(pWiasContext); BUGBUG
  496. //
  497. // Load a thumbnail of the image:
  498. //
  499. PBYTE pThumb;
  500. LONG lSize;
  501. hr = CamLoadThumbnail(pContext, &pThumb, &lSize);
  502. if (hr == S_OK)
  503. {
  504. //
  505. // write thumb property
  506. //
  507. PROPSPEC propSpec;
  508. PROPVARIANT propVar;
  509. propVar.vt = VT_VECTOR | VT_UI1;
  510. propVar.caub.cElems = lSize;
  511. propVar.caub.pElems = pThumb;
  512. propSpec.ulKind = PRSPEC_PROPID;
  513. propSpec.propid = WIA_IPC_THUMBNAIL;
  514. hr = wiasWriteMultiple(pWiasContext, 1, &propSpec, &propVar);
  515. FREE(pThumb);
  516. }
  517. //
  518. // Use WIA services to set the extended property access and
  519. // valid value information from gWiaPropInfoDefaults.
  520. //
  521. hr = wiasSetItemPropAttribs(pWiasContext,
  522. NUM_CAM_ITEM_PROPS,
  523. gPropSpecDefaults,
  524. gWiaPropInfoDefaults);
  525. return hr;
  526. }