added encoding basic example (not working currently)

This commit is contained in:
Andrei Medar 2020-08-11 07:15:53 +00:00
parent 100d7125aa
commit f1361d5d0e
6 changed files with 77 additions and 41 deletions

View File

@ -9,4 +9,5 @@ TODO
- improve tests (images, generating images etc.)
- clear redundant includes
- check if security policy can be stricter
- use addDirectoryAt instead of addDirectory
- use addDirectoryAt instead of addDirectory

View File

@ -109,6 +109,34 @@ void test1(const std::string &images_path) {
}
}
void encodeOneStep(const char *filename, const unsigned char *image,
unsigned width, unsigned height) {
/*Encode the image*/
unsigned error = lodepng_encode32_file(filename, image, width, height);
/*if there's an error, display it*/
if (error) printf("error %u: %s\n", error, lodepng_error_text(error));
}
void test2() {
const char *filename = "test_images/out/ok.png";
unsigned width = 512, height = 512;
unsigned char *image = (unsigned char *)malloc(width * height * 4);
unsigned x, y;
for (y = 0; y < height; y++) {
for (x = 0; x < width; x++) {
image[4 * width * y + 4 * x + 0] = 255 * !(x & y);
image[4 * width * y + 4 * x + 1] = x ^ y;
image[4 * width * y + 4 * x + 2] = x | y;
image[4 * width * y + 4 * x + 3] = 255;
}
}
/*run an example*/
// encodeOneStep(filename, image, width, height);
lodepng_encode32_file(filename, image, width, height);
}
int main(int argc, char *argv[]) {
if (argc != 2) {
std::cout << "usage: " << basename(argv[0]) << " images_folder_path"
@ -119,5 +147,6 @@ int main(int argc, char *argv[]) {
std::string images_path(argv[1]);
test1(images_path);
test2();
return 0;
}

View File

@ -78,35 +78,38 @@ void decode_and_encode32(SapiLodepngSandbox &sandbox, LodepngApi &api,
// which is why the solution in which data is transferred around is used.
}
void test2(SapiLodepngSandbox &sandbox, LodepngApi &api, const std::string &images_path) {
// this seems to not work as intended at the moment
void test2(SapiLodepngSandbox &sandbox, LodepngApi &api,
const std::string &images_path) {
// srand(time(NULL)); // maybe use something else
// int width = 1024, height = 1024;
unsigned int width = 512, height = 512;
unsigned char *image = (unsigned char *)malloc(width * height * 4);
// for (int i = 0; i < width * height; ++i) {
// image[i] = rand() % 256;
// }
srand(time(NULL)); // maybe use something else
// int width = 1024, height = 1024;
unsigned int width = 512, height = 512;
unsigned char *image = (unsigned char*)malloc(width * height * 4);
// for (int i = 0; i < width * height; ++i) {
// image[i] = rand() % 256;
// }
for(int y = 0; y < height; y++)
for(int x = 0; x < width; x++) {
image[4 * width * y + 4 * x + 0] = 255 * !(x & y);
image[4 * width * y + 4 * x + 1] = x ^ y;
image[4 * width * y + 4 * x + 2] = x | y;
image[4 * width * y + 4 * x + 3] = 255;
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
image[4 * width * y + 4 * x + 0] = 255 * !(x & y);
image[4 * width * y + 4 * x + 1] = x ^ y;
image[4 * width * y + 4 * x + 2] = x | y;
image[4 * width * y + 4 * x + 3] = 255;
}
}
sapi::v::Array<unsigned char> image_(image, width * height);
sapi::v::UInt width_(width), height_(height);
std::string filename = images_path + "/out/generate_and_encode_one_step1.png";
sapi::v::ConstCStr filename_(filename.c_str());
sapi::v::Array<unsigned char> image_(image, width * height);
sapi::v::UInt width_(width), height_(height);
std::string filename = images_path + "/out/ok2.png";
sapi::v::ConstCStr filename_(filename.c_str());
sandbox.Allocate(&image_).IgnoreError();
sandbox.TransferToSandboxee(&image_).IgnoreError();
// sandbox.Allocate(&image_).IgnoreError();
// sandbox.TransferToSandboxee(&image_).IgnoreError();
auto res = api.lodepng_encode32_file(filename_.PtrBefore(), image_.PtrBefore(), width_.GetValue(), height_.GetValue()).value();
std::cout << "res = " << res << std::endl;
free(image);
api.lodepng_encode32_file(filename_.PtrBefore(), image_.PtrBefore(),
width_.GetValue(), height_.GetValue())
.IgnoreError();
free(image);
}
// compares the pixels of the f1 and f2 png files.

View File

@ -26,20 +26,13 @@
// defining the flag does not work as intended (always has the default value)
// ignore for now
ABSL_FLAG(string, images_path, std::filesystem::current_path().string(),
"path to the folder containing test images");
// ABSL_FLAG(string, images_path, std::filesystem::current_path().string(),
// "path to the folder containing test images");
namespace {
// TODO find how to not use it like this.
// TODO change this into pwd/something else
std::string images_path = "/usr/local/google/home/amedar/internship/sandboxed-api/oss-internship-2020/sapi_lodepng/test_images";
TEST(addition, basic) {
EXPECT_EQ(2, 1 + 1);
// std::cout << "flag=" << std::string(absl::GetFlag(FLAGS_images_path))
// << std::endl;
}
TEST(initSandbox, basic) {
SapiLodepngSandbox sandbox(images_path);
@ -54,18 +47,28 @@ TEST(encode32, generate_and_encode_one_step) {
srand(time(NULL)); // maybe use something else
int width = 1024, height = 1024;
unsigned char *image = (unsigned char*)malloc(width * height);
for (int i = 0; i < width * height; ++i) {
image[i] = rand() % 256;
unsigned int width = 512, height = 512;
unsigned char *image = (unsigned char*)malloc(width * height * 4);
for(int y = 0; y < height; ++y) {
for(int x = 0; x < width; ++x) {
image[4 * width * y + 4 * x + 0] = 255 * !(x & y);
image[4 * width * y + 4 * x + 1] = x ^ y;
image[4 * width * y + 4 * x + 2] = x | y;
image[4 * width * y + 4 * x + 3] = 255;
}
}
sapi::v::Array<unsigned char> image_(image, width * height);
sapi::v::UInt width_(width), height_(height);
std::string filename = images_path + " /out/generate_and_encode_one_step1.png";
std::string filename = images_path + "/out/generate_and_encode_one_step1.png";
sapi::v::ConstCStr filename_(filename.c_str());
ASSERT_TRUE(api.lodepng_encode32_file(filename_.PtrBefore(), image_.PtrBefore(), width_.GetValue(), height_.GetValue()).ok());
ASSERT_TRUE(sandbox.Allocate(&image_).ok());
ASSERT_TRUE(sandbox.TransferToSandboxee(&image_).ok());
auto res = api.lodepng_encode32_file(filename_.PtrBefore(), image_.PtrBefore(), width_.GetValue(), height_.GetValue()).value();
free(image);
}
} // namespace

Binary file not shown.

After

Width:  |  Height:  |  Size: 10 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 594 KiB

After

Width:  |  Height:  |  Size: 594 KiB