| 1 | |
|---|
| 2 | |
|---|
| 3 | |
|---|
| 4 | #include "stdafx.h" |
|---|
| 5 | #include "KMLImageDataResource.h" |
|---|
| 6 | #include "UtilityMethods.h" |
|---|
| 7 | #include "ComErrorObject.h" |
|---|
| 8 | #include "LibkmlDataSource.h" |
|---|
| 9 | |
|---|
| 10 | void CKMLImageDataResource::Load() |
|---|
| 11 | { |
|---|
| 12 | try |
|---|
| 13 | { |
|---|
| 14 | static ULONG s_nextMemFile = 0; |
|---|
| 15 | |
|---|
| 16 | |
|---|
| 17 | std::string file_contents; |
|---|
| 18 | m_pParentDataSource->FetchDataRelativeToDataSource(m_imageIdentifier.c_str(), file_contents); |
|---|
| 19 | char mem_file_name[MEM_FILE_NAME_MAX_LENGTH]; |
|---|
| 20 | sprintf_s(mem_file_name, MEM_FILE_NAME_MAX_LENGTH, "/vsimem/LibkmlDSMemFile%u", s_nextMemFile++); |
|---|
| 21 | VSIFCloseL(VSIFileFromMemBuffer(mem_file_name, (GByte*)file_contents.c_str(), file_contents.length(), FALSE)); |
|---|
| 22 | |
|---|
| 23 | |
|---|
| 24 | IFvGDALDataSetPtr GDALDataset(CLSID_FvGDALDataSet); |
|---|
| 25 | GDALDataset->Open(_bstr_t(mem_file_name)); |
|---|
| 26 | IRasterDataSetPtr raster = GDALDataset; |
|---|
| 27 | m_imageData = raster->InterpretAsImage().Detach(); |
|---|
| 28 | VSIUnlink(mem_file_name); |
|---|
| 29 | } |
|---|
| 30 | catch(_com_error& ) |
|---|
| 31 | { |
|---|
| 32 | WriteToLogFile(L"Unable to load image"); |
|---|
| 33 | } |
|---|
| 34 | |
|---|
| 35 | __super::Load(); |
|---|
| 36 | } |
|---|
| 37 | |
|---|
| 38 | void CKMLImageDataResource::GetImageData(VARIANT* imageData) |
|---|
| 39 | { |
|---|
| 40 | if (State() != Resource_Loaded) |
|---|
| 41 | { |
|---|
| 42 | static _variant_t defaultImageData; |
|---|
| 43 | if (defaultImageData.vt == VT_EMPTY) |
|---|
| 44 | { |
|---|
| 45 | const int imgWidth = 16; |
|---|
| 46 | const int imgHeight = 16; |
|---|
| 47 | |
|---|
| 48 | CUtilityMethods::Create32BppBitmap(&defaultImageData, imgWidth, imgHeight); |
|---|
| 49 | |
|---|
| 50 | UCHAR* img = NULL; |
|---|
| 51 | SafeArrayAccessData(defaultImageData.parray, reinterpret_cast<void **>(&img)); |
|---|
| 52 | |
|---|
| 53 | |
|---|
| 54 | memset(img + 40, 0xff, 4*imgWidth*imgHeight); |
|---|
| 55 | |
|---|
| 56 | SafeArrayUnaccessData(defaultImageData.parray); |
|---|
| 57 | } |
|---|
| 58 | *imageData = _variant_t(defaultImageData).Detach(); |
|---|
| 59 | |
|---|
| 60 | return; |
|---|
| 61 | } |
|---|
| 62 | |
|---|
| 63 | *imageData = _variant_t(m_imageData).Detach(); |
|---|
| 64 | } |
|---|