diff --git a/oss-internship-2020/sapi_lodepng/README.md b/oss-internship-2020/sapi_lodepng/README.md index 70fa3c8..46f27e0 100644 --- a/oss-internship-2020/sapi_lodepng/README.md +++ b/oss-internship-2020/sapi_lodepng/README.md @@ -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 \ No newline at end of file +- use addDirectoryAt instead of addDirectory + diff --git a/oss-internship-2020/sapi_lodepng/main.cc b/oss-internship-2020/sapi_lodepng/main.cc index d0a8a26..7848622 100644 --- a/oss-internship-2020/sapi_lodepng/main.cc +++ b/oss-internship-2020/sapi_lodepng/main.cc @@ -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; } \ No newline at end of file diff --git a/oss-internship-2020/sapi_lodepng/main_sandboxed.cc b/oss-internship-2020/sapi_lodepng/main_sandboxed.cc index 2c14375..1b312a6 100644 --- a/oss-internship-2020/sapi_lodepng/main_sandboxed.cc +++ b/oss-internship-2020/sapi_lodepng/main_sandboxed.cc @@ -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 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 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. diff --git a/oss-internship-2020/sapi_lodepng/main_unit_test.cc b/oss-internship-2020/sapi_lodepng/main_unit_test.cc index 08bbc01..445bbc6 100644 --- a/oss-internship-2020/sapi_lodepng/main_unit_test.cc +++ b/oss-internship-2020/sapi_lodepng/main_unit_test.cc @@ -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 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 \ No newline at end of file diff --git a/oss-internship-2020/sapi_lodepng/test_images/out/ok.png b/oss-internship-2020/sapi_lodepng/test_images/out/ok.png new file mode 100644 index 0000000..51f307e Binary files /dev/null and b/oss-internship-2020/sapi_lodepng/test_images/out/ok.png differ diff --git a/oss-internship-2020/sapi_lodepng/test_images/out/generate_and_encode_one_step1.png b/oss-internship-2020/sapi_lodepng/test_images/out/ok2.png similarity index 87% rename from oss-internship-2020/sapi_lodepng/test_images/out/generate_and_encode_one_step1.png rename to oss-internship-2020/sapi_lodepng/test_images/out/ok2.png index 274e5e4..e9045c9 100644 Binary files a/oss-internship-2020/sapi_lodepng/test_images/out/generate_and_encode_one_step1.png and b/oss-internship-2020/sapi_lodepng/test_images/out/ok2.png differ