Select Git revision
Code owners
Assign users and groups as approvers for specific file changes. Learn more.
Demo2.cpp 8.50 KiB
/*
./bgs_demo -i test45/ -a 100 -o test45/results/
based on original demo.cpp
*/
// c
#include <sys/types.h>
#include <sys/stat.h>
#include <archive.h>
#include <archive_entry.h>
#include <fcntl.h>
#include <string.h>
#include <unistd.h>
//cpp c
#include <iostream>
#include <algorithm>
#include <vector>
#include <cstdlib>
#include <stdio.h> /* printf, scanf, puts, NULL */
#include <stdlib.h> /* srand, rand */
#include <time.h> /* time */
#include <ctime>
#define PROCESS_CENTER_VERSION_MAJOR 0
#define PROCESS_CENTER_VERSION_MINOR 2
//opencv
#include <opencv2/opencv.hpp>
//bgslibrary
#include "package_bgs/bgslibrary.h"
//my class
#include "package_bgs/Tapter.h"
#include "package_bgs/ttoolbox.h"
using namespace cv;
using namespace std;
char* getCmdOption(char ** begin, char ** end, const std::string & option)
{
char ** itr = std::find(begin, end, option);
if (itr != end && ++itr != end)
{
return *itr;
}
return 0;
}
bool cmdOptionExists(char** begin, char** end, const std::string& option)
{
return std::find(begin, end, option) != end;
}
static std::vector<char> copyDataInBuffer(struct archive *aw);
//static void errmsg(const char *);
//static void fail(const char *, const char *, int);
//static void msg(const char *);
//static void warn(const char *, const char *);
int main(int argc, char * argv[])
{
std::cout << "using produce bk " <<PROCESS_CENTER_VERSION_MAJOR <<"."<< PROCESS_CENTER_VERSION_MINOR << endl;
std::cout << "Using OpenCV " << CV_MAJOR_VERSION << "." << CV_MINOR_VERSION << "." << CV_SUBMINOR_VERSION << std::endl;
//!** parse programm input****************/
if(cmdOptionExists(argv, argv+argc, "-h"))
{
cout <<" error: please use command as\n./bgs_demo -i pathToInputDir -a amountOfJpgFiles -c exactCenterConfFile.xml -o outPutPath"<<endl;
return EXIT_FAILURE;
}
if(!cmdOptionExists(argv, argv+argc, "-i")||!cmdOptionExists(argv, argv+argc, "-a")||!cmdOptionExists(argv, argv+argc, "-o") )
{
cout <<" error: please use command as\n./bgs_demo -i pathToInputDir -a amountOfJpgFiles -c exactCenterConfFile.xml -o outPutPath"<<endl;
return EXIT_FAILURE;
}
char *testInputDir = getCmdOption(argv, argv + argc, "-i");
string inputDir(".");
if (testInputDir)
{
//test dir exists
inputDir = string(testInputDir);
}
int amountFiles = -1;
char *testFileAmount = getCmdOption(argv, argv + argc, "-a");
if (testFileAmount)
{
string s(testFileAmount);
stringstream foo(s);
foo >> amountFiles;
}
char *centerFile = getCmdOption(argv, argv + argc, "-c");
string centerFileString(".");
if (centerFile)
{
//test dir exists
centerFileString = string(centerFile);
}
char *testOutputDir = getCmdOption(argv, argv + argc, "-o");
string outputDir(".");
if (testOutputDir)
{
//test dir exists
outputDir = string(testOutputDir);
}
struct archive *archive;
struct archive *ext;
struct archive_entry *entry;
int r;
int flags = ARCHIVE_EXTRACT_TIME; // see https://linux.die.net/man/3/archive_write_disk for more flags
const char *filename = "data_sized.tar";
archive = archive_read_new();
ext = archive_write_disk_new();
archive_write_disk_set_options(ext, flags);
archive_read_support_format_tar(archive);
//we get the filename
if (filename != NULL && strcmp(filename, "-") == 0)
filename = NULL;
//and open the handler
if ((r = archive_read_open_filename(archive, filename, 10240)))
{
cerr<<"archive_read_open_filename: error: "<< archive_error_string(archive) <<" will abort"<< endl;
exit(1);
// fail("archive_read_open_filename()",
// archive_error_string(a), r);
}
int counter = 0;
for (;;) {
r = archive_read_next_header(archive, &entry);
if (r == ARCHIVE_EOF)
break;
if (r != ARCHIVE_OK)
{
cerr<<"archive_read_next_header: error: "<< archive_error_string(archive) <<" will abort"<< endl;
exit(1);
// fail("archive_read_next_header()",
// archive_error_string(a), 1);
}
//msg("x ");
///char *buffer = malloc (sizeof(char) * 400000);
//Mat img =Mat::zeros(1936,1456,CV_8UC3);
//new way
//std::ifstream file("img.jpg");
std::vector<char> vec;
// file >> std::noskipws;
// std::copy(std::istream_iterator<char>(file), std::istream_iterator<char>(), std::back_inserter(data));
const char *fileNamePtr = archive_entry_pathname(entry);
std::string filename(fileNamePtr,strlen(fileNamePtr) );
cout << "fileName: "<< filename <<endl;
//TODO we should test what file we have, this filenumber n = n - 1 ??
//msg(archive_entry_pathname(entry));
r = archive_write_header(ext, entry);
if (r != ARCHIVE_OK)
{
cerr<<"archive_write_header() error: "<< archive_error_string(ext)<<endl;
}
else
{
vec = copyDataInBuffer(archive);//no // we cast the data pointer to void, because we know what we do ??
if(vec.empty())
cerr << "error during load data into buffer";
r = archive_write_finish_entry(ext);
if (r != ARCHIVE_OK)
{
cerr<<"archive_write_finish_entry: error: "<< archive_error_string(ext) <<" will abort"<< endl;
exit(1);
// fail("archive_write_finish_entry()",
// archive_error_string(ext), 1);
}
}
//we read the image buffer as a jpg picture and decode it
Mat img2 = imdecode(Mat(vec), 1);
// //here we could use our image
// //we show what we got
// namedWindow( "Display window", WINDOW_AUTOSIZE );// Create a window for display.
// imshow( "Display window", img2 ); // Show our image inside it.
// cv::imwrite("foo.jpg", img2);
// //msg("\n");
if(counter > 10) exit(1);
counter ++;
// waitKey(0);
}
archive_read_close(archive);
archive_read_free(archive);
archive_write_close(ext);
archive_write_free(ext);
return 0;
}
static std::vector<char> copyDataInBuffer(struct archive *aw)
{
int r;
const void *buff;
std::vector<char> vec;
size_t size;
#if ARCHIVE_VERSION_NUMBER >= 3000000
int64_t offset;
#else
off_t offset;
#endif
// unsigned int myOffsetCounter = 0;
// int counterIteration = 0;
// ofstream myfile;
// myfile.open ("tmpPicture.jpg", ios::out | ios::binary);
// if (!myfile.is_open())
// return vec;
for (;;) {
r = archive_read_data_block(aw, &buff, &size, &offset);
if (r == ARCHIVE_EOF)
{
return vec; // everything fine, were are just at the end
}
if (r != ARCHIVE_OK)
{
cerr << "error during read archive "<<endl;
return vec;
}
//r = archive_write_data_block(aw, buff, size, offset);
//we mem copy the buffer
//cout <<counterIteration++<< " offset: "<< offset<< " size: "<< size<<endl;
//memcpy( &buffer[myOffsetCounter], (char*) buff, size * sizeof( char ) );
//we simply copy it to the end
//std::copy ( buff, buff+size, vec.end() );
//myOffsetCounter += size;
//a good discussion : https://stackoverflow.com/questions/259297/how-do-you-copy-the-contents-of-an-array-to-a-stdvector-in-c-without-looping
// Method 4: vector::insert
{
//we rename our pointer to avoid a weird compiler warning
char *foo = (char*) buff;
vec.insert(vec.end(), &foo[0], &foo[size]);
}
//myfile.write ((char*)buff,size);
// if (r != ARCHIVE_OK) {
// warn("archive_write_data_block()",
// archive_error_string(aw));
// return (r);
// }
}
//myfile.close();
return vec;
}
//static void
//msg(const char *m)
//{
// write(1, m, strlen(m));
//}
//static void
//errmsg(const char *m)
//{
// write(2, m, strlen(m));
//}
//static void
//warn(const char *f, const char *m)
//{
// errmsg(f);
// errmsg(" failed: ");
// errmsg(m);
// errmsg("\n");
//}
//static void
//fail(const char *f, const char *m, int r)
//{
// warn(f, m);
// exit(r);
//}