Sample code for both C and C# projects are provided along with the SDK. The examples below demonstrate the general usage of the Perfectly Clear SDK.
Simple AI Sample - Using Scene Detection
The simplest way to use Perfectly Clear AI. See the "SimpleAiSample" sample for a full implementation of this.
- Setup the license protection
if (bVerbose)
printf("License Code Status is %d\n", code);
- Load image to correct:
Bitmap *pBM = Bitmap::FromFile(inname);
if (pBM == NULL)
{
printf("Unable to open input file.\n");
exit(0);
}
BitmapData *pbd = new BitmapData;
pBM->LockBits(&Rect(0, 0, pBM->GetWidth(), pBM->GetHeight()), ImageLockModeWrite, pBM->GetPixelFormat(), pbd);
int width, height;
width = pbd->Width;
height = pbd->Height;
- Initialize the AI Engines
std::string binPath = ".";
printf("Cannot load AI Scene Detection. Make sure the eff_1.pnn file, PFCAI and onnx libraries are in the executable directory or change binPath in code and re-compile.\n");
printf("status %d\n", aistatus);
exit(1);
}
printf("Cannot load AI Corrections. Make sure the dynamic.pnn file, PFCAI and onnx libraries are in the executable directory or change binPath in code and re-compile.\n");
printf("status %d\n", aistatus);
exit(1);
}
- Perform full correction using the auto function:
int status =
PFC_AutoCorrect(&im, NULL, ignoredParam, -1, NULL,
false, NULL, pAiEngine);
{
printf("Image processed successfully.\n");
}
{
printf(
"Noise removal status %d\n",
NRRETCODE(status));
printf(
"Perfectly Clear correction status %d\n",
CORERETCODE(status));
printf(
"Face beautification status %d\n",
FBRETCODE(status));
printf(
"Red eye removal status %d\n",
RERETCODE(status));
}
{
printf("Image processing failed with return code: %d\n", status);
}
- Save the corrected image
Detailed AI Sample - Using Scene Detection
To extend the example shown above, you can load custom AI Presets to apply, instead of using the default presets provided by this SDK. See the "DetailedAiSample" sample for a full implementation of this.
if (profilename == NULL)
{
if (bVerbose) {
printf("Using default presets for detected scenes\n");
}
} else {
char* scenePresetPath = NULL;
std::string profilenameUtf8;
if (profilename) {
profilenameUtf8 = utf8FromW(std::wstring(profilename));
scenePresetPath = (char*)profilenameUtf8.c_str();
}
if (0 != retLoadPresets) {
printf("Cannot load scene presets file '%s' error '%i'\n", scenePresetPath, retLoadPresets);
exit(1);
}
else {
printf("Loaded scene presets file '%s'\n", scenePresetPath);
}
}
And you can determine the scene ID and label:
int version = 0;
printf("AI version '%i' assigned scene '%i' to the image.\n", version, detectedScene);
AI Image Correction without Scene Detection
You can correct photos without scene detection, but still using the AI Image corrections that are new in this V10 SDK. This is implemented in the "PresetSample" sample code provided with this SDK.
- load the correction parameters to apply from a .preset file
if (profilename != NULL)
{
std::string profilenameUtf8 = utf8FromW(std::wstring(profilename));
if (bVerbose)
printf("PFC_ReadPresets returns %d\n", ret);
if (ret != 0)
{
if (ret == -2)
{
printf("Unable to open preset file %s. Using default settings.\n", profilename);
}
else if (ret == -8)
{
printf("Invalid preset file.\n");
exit(0);
}
else
{
printf("Unable to read preset file.\n");
exit(0);
}
}
}
else
{
}
- then use these parameters when correcting the image
if (bVerbose)
{
{
printf("Image processed successfully.\n");
}
{
int code;
printf("Noise removal status %d\n", code);
printf("Perfectly Clear correction status %d\n", code);
printf("Face beautification status %d\n", code);
printf("Red eye removal status %d\n", code);
}
else
{
printf("Image processing failed with return code: %d\n", status);
}
}
Image Resizing
This sample loads and corrects the image, but also resizes the output images, as seen in "ResizeSample".
Bitmap *pResizedBM = new Bitmap(800, 600, pbd->PixelFormat);
BitmapData *pbdrs = new BitmapData;
pResizedBM->LockBits(&Rect(0, 0, pResizedBM->GetWidth(), pResizedBM->GetHeight()), ImageLockModeWrite, pResizedBM->GetPixelFormat(), pbdrs);
imageResized.
width = pbdrs->Width;
imageResized.
height = pbdrs->Height;
imageResized.
format = im.format;
imageResized.
stride = pbdrs->Stride;
imageResized.
data = (LPBYTE)pbdrs->Scan0;
if (names[1] == NULL) {
names[1] = GetDefaultName(names[0]);
}
SaveImage(pResizedBM, names[1]);
} else {
printf("Image couldn't be resized, failed with return code: %d\n", status);
}
PDF Image Correcting
This sample loads a PDF file and corrects the images it finds, replaces them into the file and saves it back, as seen in "PDFSample".
- First load the PDF file and setup the correction engine
printf("Cannot load path %s\n", inputPath);
return 1;
}
int i = 0;
- Then, iterate over the images, correcting each one
printf(" working on image %d\n", i);
printf(" -status %d\n", status);
i++;
}
- Last, write the new PDF containing all the corrected images to disk:
printf("Cannot save to %s\n", outputPath);
return 1;
}
Loading From Bitmaps or with PfCImageFile
You can either load image data using our PFCImageFile class, or you can use standard Windows bitmaps. SimpleAiSample shows the bitmap method. If you are incorporating Perfectly Clear into an application that already is using Bitmaps, then this method will make it easy to incorporate Perfectly Clear into this.
Bitmap *pBM = Bitmap::FromFile(inname);
if (pBM == NULL)
{
printf("Unable to open input file.\n");
exit(0);
}
BitmapData *pbd = new BitmapData;
pBM->LockBits(&Rect(0, 0, pBM->GetWidth(), pBM->GetHeight()), ImageLockModeWrite, pBM->GetPixelFormat(), pbd);
int width, height;
width = pbd->Width;
height = pbd->Height;
In DotNetCore_PFCImageFile, we use the PfCImageFile class instead, shown in a simplified form here:
bool bConvertToSRGB = true;
const char* pathToSupportFilesFolder = NULL;
#ifdef WIN32
std::string innameUtf8 = utf8FromW(std::wstring(inname));
#else
#endif
printf("Unable to convert input file to sRGB, processing in original color space\n");
} else {
printf("Unable to open input file, ABORTING.\n");
exit(1);
}
}
PfCImageFile class will handle color space conversion and metadata management automatically.
- Copyright
- Copyright (C) 2021 EyeQ Imaging Inc