This is an example of how to use the Anonymizer library. This example shows how to explicitly link anonymizer library and run anonymization of a JPEG buffer and an image buffer. Example uses default AnParams parameters
Example example-buffers.cpp uses OpenCV library for image IO operation. Necessary documentation how to set up OpenCV is in examples/example-buffer/ directory.
#include <stdio.h>
#include <string>
#include <opencv2/opencv.hpp>
using namespace std;
#include <er_explink.h>
#include <Anonymizer.h>
#define ANONYMIZER_SDK_DIR "../../AnonymizerSDK/"
#define ANONYMIZER_LIB ANONYMIZER_SDK_DIR "/lib/" ER_LIB_PREFIX "anonymizer-" ER_LIB_TARGET ER_LIB_EXT
#define ANONYMIZER_INI "config.ini"
int fread_buffer( char *name, unsigned char **ppBuffer, size_t *length);
int fwrite_buffer(char *name, unsigned char *pBuffer, size_t length);
int main (int argc, const char ** argv)
{
string output = "image_anonymized.jpg";
const string outputOpt = "-o=";
size_t outputOptLen = outputOpt.length();
const string helpOpt = "--help";
size_t helpOptLen = helpOpt.length();
string input = "../../data/image.jpg";
for( int i = 1; i < argc; i++ )
{
int res = 0;
if( outputOpt.compare( 0, outputOptLen, argv[i], outputOptLen ) == 0 )
output.assign( argv[i] + outputOptLen);
else if( (res = helpOpt.compare( 0, helpOptLen, argv[i], helpOptLen )) == 0 ){
printf("USAGE: %s [options] [filename]\n"
" options: -o=filename sets the output filename for anonymized image\n"
" filename: input image filename, default ../../data/image.jpg\n"
"\nEXAMPLE:\n %s\n", argv[0], argv[0]);
return 0;
}
else if( argv[i][0] == '-' )
{
printf("WARNING: Unknown option %s\nSee `%s --help' for more information.\n",argv[i], argv[0]);
}
else
input.assign( argv[i] );
}
if( input.empty() ){
printf("ERROR: Input filename is missing.\nTry `%s --help' for more information.\n", argv[0]);
return -1;
}
printf("Loading library and functions\n");
shlib_hnd hDll;
ER_OPEN_SHLIB(hDll, ANONYMIZER_LIB);
if (!hDll){
printf(" library '%s' not loaded!\n", ANONYMIZER_LIB);
return -1;
}
ER_LOAD_SHFCN(fcnAnInit,
fcn_anInit, hDll,
"anInit");
ER_LOAD_SHFCN(fcnAnFree,
fcn_anFree, hDll,
"anFree");
if (!fcnAnInit || !fcnAnFree || !fcnAnAnonymizeImageBuffer || !fcnAnAnonymizeJpegBuffer || !fcnAnVersion || !fcnAnGetErrorMsg){
printf(" Loading Anonymizer functions failed!\n");
return -1;
}
printf(" ... done\n");
printf("Loaded %s\n", fcnAnVersion(0));
int ern;
printf("Anonymizer initialization:\n");
ern=fcnAnInit((char*)ANONYMIZER_SDK_DIR, ANONYMIZER_INI, &state);
if (ern != 0){
printf("failed!\n%s\n", fcnAnGetErrorMsg(ern));
return -1;
}else
printf(" ... done.\n");
printf("JPEG buffer anonymization\n");
if (fread_buffer((
char*)input.c_str(), &(srcBuffer.
raw_data), &(srcBuffer.
length)) != 0) {
fcnAnFree(state);
return -1;
}
ern=fcnAnAnonymizeJpegBuffer(srcBuffer, NULL, state, &dstBuffer);
if (ern != 0) {
printf(" ... failed!\n%s\n",fcnAnGetErrorMsg(ern));
fcnAnFree(state);
return -1;
}
if (fwrite_buffer((
char*)((output +
".JpegBuffer.jpg").c_str()), dstBuffer.
raw_data, dstBuffer.
length) != 0) {
fcnAnFree(state);
return -1;
}
fcnAnFreeBuffer(dstBuffer);
printf(" ...done\n");
printf("Image data buffer anonymization\n");
cv::Mat imageBGR, imageAnonymized;
imageBGR = cv::imread(input.c_str(), cv::IMREAD_COLOR);
cv::Size s = imageBGR.size();
if (fcnAnAnonymizeImageBuffer(srcBuffer, s.width,s.height, NULL, state, &dstBuffer) != 0) {
printf(" ... failed!\n%s\n",fcnAnGetErrorMsg(ern));
fcnAnFree(state);
return -1;
}
memcpy(imageBGR.data, dstBuffer.
raw_data, imageBGR.total()*imageBGR.elemSize());
cv::imwrite(output + ".Buffer.jpg", imageBGR);
fcnAnFreeBuffer(dstBuffer);
printf(" ...done\n");
printf("Cleaning up\n");
fcnAnFree(state);
ER_FREE_LIB(hDll);
printf(" ... done.\n");
return 0;
}
int fread_buffer(char *name, unsigned char **buffer_, size_t *length)
{
FILE *file;
file = fopen(name, "rb");
if (!file)
{
printf("Unable to open file %s", name);
return -1;
}
fseek(file, 0, SEEK_END);
*length = ftell(file);
fseek(file, 0, SEEK_SET);
unsigned char *buffer;
try {
buffer = new unsigned char[*length + 1];
}
catch (std::bad_alloc e) {
printf("%s\n", e.what());
fclose(file);
return -1;
}
fread(buffer, *length, 1, file);
fclose(file);
*buffer_ = buffer;
return 0;
}
int fwrite_buffer(char *name, unsigned char *pBuffer, size_t length)
{
FILE *file = fopen(name, "wb");
if (!file)
{
printf("Writing buffer to file failed!\n");
return -1;
}
fwrite(pBuffer, length, 1, file);
fclose(file);
return 0;
}