Skip to content

Instantly share code, notes, and snippets.

@Nuclearfossil
Last active May 23, 2019 17:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Nuclearfossil/6d83dc16a8c2a8c78be6e9adc63c67a6 to your computer and use it in GitHub Desktop.
Save Nuclearfossil/6d83dc16a8c2a8c78be6e9adc63c67a6 to your computer and use it in GitHub Desktop.
cr3convert
// cr3convert.cpp : This file contains the 'main' function. Program execution begins and ends there.
//
#include <iostream>
#include <unordered_map>
#include <vector>
#include <string>
#include <EDSDK.h>
#include "utils.h"
/// Super untested command line arg parser.
/// Not smart, fairly dumb implementation.
/// Help test isn't automatically build - you pass it in the string you want to display.
class ArgParser
{
public:
ArgParser(const char* helpText)
{
if (helpText != nullptr)
{
m_helpText = helpText;
}
}
~ArgParser() {}
void Parse(int argc, char* argv[])
{
if (argc <= 1)
return;
for (int index = 1; index < argc; index++)
{
if (argv[index][0] == '-'
|| argv[index][0] == '/'
|| argv[index][0] == '\\')
{
// we have a flag, store the flag name in the internal dictionary.
if (argc > index &&
(argv[index + 1][0] != '-'
|| argv[index + 1][0] == '/'
|| argv[index + 1][0] == '\\'))
{
m_flags[argv[index]] = argv[++index];
}
else
{
m_flags[argv[index]] = "True";
}
}
else
{
// otherwise we are storing a positional argument
m_positionalArgs.push_back(argv[index]);
}
}
}
const std::string& getPositionalArg(unsigned int index)
{
if (index < m_positionalArgs.size())
{
return m_positionalArgs[index];
}
return m_emptyString;
}
const std::string& getArg(const char* key)
{
auto found = m_flags.find(key);
if (found != m_flags.end())
{
return found->second;
}
return m_emptyString;
}
private:
std::string m_helpText;
std::unordered_map<std::string, std::string> m_flags;
std::vector<std::string> m_positionalArgs;
const std::string m_emptyString;
};
int main(int argc, char* argv[])
{
ArgParser parser("No help text provided");
parser.Parse(argc, argv);
const char* srcFilename = parser.getPositionalArg(0).c_str();
const char* destFilename = parser.getArg("-o").c_str();
// ensure that the file exists
if (!Exists(srcFilename))
{
std::cout << "Unable to locate input file: " << srcFilename << "\n";
return -1;
}
if (strcmp(destFilename, "") == 0)
{
std::cout << "Provide an output file. None specified\n";
return -1;
}
EdsInitializeSDK();
EdsError status;
EdsStreamRef imageFileStream;
status = EdsCreateFileStream(srcFilename, kEdsFileCreateDisposition_OpenExisting, kEdsAccess_Read, &imageFileStream);
if (status != EDS_ERR_OK)
{
EdsRelease(imageFileStream);
std::cout << "Unable to read input file: " << srcFilename << "\n";
return -1;
}
EdsImageRef imageRef;
status = EdsCreateImageRef(imageFileStream, &imageRef);
if (status != EDS_ERR_OK)
{
EdsRelease(imageRef);
EdsRelease(imageFileStream);
std::cout << "Unable to read input file: " << srcFilename << "\n";
return -1;
}
EdsImageInfo imageInfo;
status = EdsGetImageInfo(imageRef, kEdsImageSrc_FullView, &imageInfo);
if (status != EDS_ERR_OK)
{
EdsRelease(imageRef);
EdsRelease(imageFileStream);
std::cout << "Unable to process input file: " << srcFilename << "\n";
return -1;
}
std::cout << "Loading input file to process: " << srcFilename << "\n";
EdsUInt32 dataSize;
EdsUInt32 imageQuality;
EdsDataType dataType;
status = EdsGetPropertySize(imageRef, kEdsPropID_ImageQuality, 0, &dataType, &dataSize);
status = EdsGetPropertyData(imageRef, kEdsPropID_ImageQuality, 0, dataSize, &imageQuality);
EdsRect srcRect;
EdsSize dstSize;
srcRect.point.x = imageInfo.effectiveRect.point.x;
srcRect.point.y = imageInfo.effectiveRect.point.y;
srcRect.size.width = imageInfo.effectiveRect.size.width;
srcRect.size.height = imageInfo.effectiveRect.size.height;
dstSize.width = imageInfo.width;
dstSize.height = imageInfo.height;
EdsStreamRef memoryImageRef;
EdsInt64 imageBufferSize = imageInfo.effectiveRect.size.width * imageInfo.effectiveRect.size.height *
imageInfo.numOfComponents * imageInfo.componentDepth / 8;
status = EdsCreateMemoryStream(imageBufferSize, &memoryImageRef);
std::cout << "image width and height: " << imageInfo.effectiveRect.size.width << "x" << imageInfo.effectiveRect.size.height << "\n";
if (status != EDS_ERR_OK)
{
EdsRelease(memoryImageRef);
EdsRelease(imageRef);
EdsRelease(imageFileStream);
std::cout << "Unable to process input file: " << srcFilename << "\n";
return -1;
}
status = EdsGetImage(imageRef, kEdsImageSrc_FullView, kEdsTargetImageType_RGB16, srcRect, dstSize, memoryImageRef);
if (status != EDS_ERR_OK)
{
EdsRelease(memoryImageRef);
EdsRelease(imageRef);
EdsRelease(imageFileStream);
std::cout << "Unable to process input file: " << srcFilename << "\n";
return -1;
}
int imagebuffer[1024];
EdsUInt64 readSize;
status = EdsRead(memoryImageRef, 1024, &imagebuffer, &readSize);
if (status != EDS_ERR_OK)
{
EdsRelease(memoryImageRef);
EdsRelease(imageRef);
EdsRelease(imageFileStream);
std::cout << "Unable to read image data from the memory stream.\n";
return -1;
}
EdsStreamRef destFileStream;
status = EdsCreateFileStream(destFilename, kEdsFileCreateDisposition_CreateAlways, kEdsAccess_Write, &destFileStream);
if (status != EDS_ERR_OK)
{
EdsRelease(memoryImageRef);
EdsRelease(imageRef);
EdsRelease(imageFileStream);
std::cout << "Unable to create an output memory stream.\n";
return -1;
}
EdsSaveImageSetting saveSettings;
saveSettings.JPEGQuality = 10;
saveSettings.iccProfileStream = 0;
saveSettings.reserved = 0;
status = EdsSaveImage(memoryImageRef, kEdsTargetImageType_TIFF16, saveSettings, destFileStream);
if (status != EDS_ERR_OK)
{
EdsRelease(destFileStream);
EdsRelease(memoryImageRef);
EdsRelease(imageRef);
EdsRelease(imageFileStream);
std::cout << "Unable to read image data from the memory stream.\n";
return -1;
}
EdsRelease(destFileStream);
EdsRelease(memoryImageRef);
EdsRelease(imageRef);
EdsRelease(imageFileStream);
EdsTerminateSDK();
std::cout << "Image file loaded without error \n";
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment