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.
Scenario #1 PCF_AutoCorrect
This is the simplest way to use Perfectly Clear library suite. This protocol is more suitable for developing a server type software project or in scenarios where every image will be corrected with one set of parameters (no user-facing option to adjust correction parameters).
- Setup the license protection:
if (bVerbose)
printf("License Code Status is %d\n", code);
- Initialize parameters:
- Populate image data into PFCIMAGE structure:
im.
data = (
unsigned char*)pbd->Scan0;
- Perform full correction using the auto function:
if (bVerbose)
{
if (status == 0)
{
printf("Image processed successfully\n");
}
else if (status > 0)
{
printf(
" Pre-calculation on noise returns %d\n",
PFCNR_STATUS(status));
printf(
" Pre-calculation on face details returns %d\n",
PFCFB_STATUS(status));
printf(
" Pre-calculation on red eye returns %d\n",
PFCRE_STATUS(status));
}
else if (status < 0)
{
printf("Image processed failed with return code: %d\n", status);
}
}
Scenario #2 Separate Calc and Apply
More advanced way to use Perfectly Clear library suite. This protocol is more suitable for developing a desktop photo editing type software where user parameters will vary under the same session of an image.
- Setup the license protection:
if (bVerbose)
printf("License Code Status is %d\n", code);
- Create process engine:
if (bVerbose)
{
{
printf("Engine created successfully.\n");
}
else
{
{
printf("Face Beautification library not available.");
}
}
}
- Initialize the parameter structure (using default parameter values): The parameters can be imported from a preset file.
std::string profilenameUtf8 = utf8FromW(std::wstring(profilename));
if (bVerbose)
printf("PFC_ReadPresets returns %d\n", ret);
Alternatively the parameters can be initialized using PFC_SetParam() function. Example:
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
{
}
- Populate image data into PFCIMAGE structure:
im.
data = (
unsigned char*)pbd->Scan0;
- Perform pre-calculation of image specific profile:
if (bVerbose)
{
if (pProfile->Status != 0)
{
{
printf(" Pre-calculation on noise returns %d\n", pProfile->NR_Status);
}
{
printf(" Core pre-calculation returns %d\n", pProfile->CORE_Status);
}
{
printf(" Pre-calculation on face details returns %d\n", pProfile->FB_Status);
}
{
printf(" Pre-calculation on red eye returns %d\n", pProfile->RE_Status);
}
}
printf("Calc returns %d\n", pProfile->Status);
}
(pEngine is created from step 1.)
- Apply the calculated profile and parameters to 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);
}
}
- Release resources used by PFCPROFILE:
- Release the engine to release resource:
Scenario #3 .NET Core Samples
We have included two samples that run in .NET Core v3.1. These are essentially the same applications as Sample 2 shown above. In DotNetCore_Sample2 - we use standard windows bitmaps to load and save the image data, greatly simplified here:
Bitmap bm = new Bitmap(inname);
Pfc.Apply(ref bm, 100);
bm.Save(outname);
In DotNetCore_Sample3, we use the PfCImageFile class instead, shown in a simplified form here:
Pfc.Apply(ref file, 100);
file.SaveImageFile(outname,90, false, false);
If you are incorporating Perfectly Clear into an application that already is using BitMaps, then Sample2 will make it easy to incorporate Perfectly Clear into it.
Sample 3's use of the PfCImageFile class will handle color space conversion and metadata management automatically
Scenario #4 Using MemoryStreams
In SDK v8.5, we have introduced the ability to read and write image data using .NET native MemoryStreams. If the application you are working on already has image file data loaded into MemoryStreams, this method allows you to use those directly, instead of needing to write image files to disk.
MemoryStream inMemoryCopy = new MemoryStream();
using (FileStream fs = File.OpenRead(inname))
{
fs.CopyTo(inMemoryCopy);
}
inMemoryCopy.Position = 0;
{
Console.WriteLine("Unable to open image.");
ShowHelp();
return;
}
Now, you can process the PFCImageFile like in the other samples, the most simple way would be:
Pfc.AutoCorrect(ref file, "my_preset.preset");
Now, the 'file' object has been corrected, so to get the corrected image data back into a MemoryStream:
{
outStream.Seek(0, SeekOrigin.Begin);
FileStream outFile = File.Create(outname);
outStream.CopyTo(outFile);
}
.NET Core Version Issues
In the .NET Core examples, there are PreBuildEvent steps to copy the required libraries into the proper build location. For example:
<PropertyGroup>
<PreBuildEvent>
xcopy /C /E /Y /R ..\..\..\..\..\bin\x64\PerfectlyClearPro.dll .\PerfectlyClearPro.dll*
xcopy /C /E /Y /R ..\..\..\..\..\bin\x64\PerfectlyClearAdapter.dll .
xcopy /C /E /Y /R ..\..\..\..\..\bin\x64\PFCImageFile.dll .
xcopy /C /E /Y /R ..\..\..\..\..\bin\x64\exiv2.dll .
</PreBuildEvent>
</PropertyGroup>
Depending on which version of Visual Studio and .NET Core version you are using, these steps might cause errors when you build. This is due to an extra folder level that Visual Studio builds in some cases. The solution is to remove one set of "..\" from these steps, like this:
<PropertyGroup>
<PreBuildEvent>
xcopy /C /E /Y /R ..\..\..\..\bin\x64\PerfectlyClearPro.dll .\PerfectlyClearPro.dll*
xcopy /C /E /Y /R ..\..\..\..\bin\x64\PerfectlyClearAdapter.dll .
xcopy /C /E /Y /R ..\..\..\..\bin\x64\PFCImageFile.dll .
xcopy /C /E /Y /R ..\..\..\..\bin\x64\exiv2.dll .
</PreBuildEvent>
</PropertyGroup>
- Copyright
- Copyright (C) 2020 EyeQ Imaging Inc