diff --git a/oss-internship-2020/sapi_lodepng/main.cc b/oss-internship-2020/sapi_lodepng/main.cc index 554a492..9823881 100644 --- a/oss-internship-2020/sapi_lodepng/main.cc +++ b/oss-internship-2020/sapi_lodepng/main.cc @@ -15,116 +15,137 @@ #include #include +#include +#include #include #include "lodepng/lodepng.h" -bool cmp_images32(const std::string &f1, const std::string &f2) { - std::cout << "COMPARING IMAGES " << basename(f1.c_str()) << " -> " - << basename(f2.c_str()) << std::endl; +// bool cmp_images32(const std::string &f1, const std::string &f2) { +// std::cout << "COMPARING IMAGES " << basename(f1.c_str()) << " -> " +// << basename(f2.c_str()) << std::endl; - unsigned int error, width1, height1; - unsigned char *image1 = 0; +// unsigned int error, width1, height1; +// unsigned char *image1 = 0; - unsigned int width2, height2; - unsigned char *image2 = 0; +// unsigned int width2, height2; +// unsigned char *image2 = 0; - error = lodepng_decode32_file(&image1, &width1, &height1, f1.c_str()); +// error = lodepng_decode32_file(&image1, &width1, &height1, f1.c_str()); - if (error) { - std::cerr << "error " << error << ": " << lodepng_error_text(error) - << std::endl; - return false; - } +// if (error) { +// std::cerr << "error " << error << ": " << lodepng_error_text(error) +// << std::endl; +// return false; +// } - error = lodepng_decode32_file(&image2, &width2, &height2, f2.c_str()); +// error = lodepng_decode32_file(&image2, &width2, &height2, f2.c_str()); - if (error) { - std::cerr << "error " << error << ": " << lodepng_error_text(error) - << std::endl; - return false; - } +// if (error) { +// std::cerr << "error " << error << ": " << lodepng_error_text(error) +// << std::endl; +// return false; +// } - if (width1 != width2 || height1 != height2) { - std::cerr << "DIMENSIONS DIFFER\n"; - return false; - } +// if (width1 != width2 || height1 != height2) { +// std::cerr << "DIMENSIONS DIFFER\n"; +// return false; +// } - std::cout << "width height = " << width1 << " " << height1 << std::endl; +// std::cout << "width height = " << width1 << " " << height1 << std::endl; - for (int i = 0; i < width1 * height1; ++i) { - if (image1[i] != image2[i]) { - std::cerr << "PIXELS DIFFER AT i = " << i << std::endl; - return false; - } - } - return true; -} +// for (int i = 0; i < width1 * height1; ++i) { +// if (image1[i] != image2[i]) { +// std::cerr << "PIXELS DIFFER AT i = " << i << std::endl; +// return false; +// } +// } +// return true; +// } -// copies an image into another and compares them -void decode_and_encode32(const std::string &filename1, - const std::string &filename2) { - unsigned int error, width, height; - unsigned char *image = 0; +// // copies an image into another and compares them +// void decode_and_encode32(const std::string &filename1, +// const std::string &filename2) { +// unsigned int error, width, height; +// unsigned char *image = 0; - error = lodepng_decode32_file(&image, &width, &height, filename1.c_str()); +// error = lodepng_decode32_file(&image, &width, &height, filename1.c_str()); - if (error) { - std::cerr << "error " << error << ": " << lodepng_error_text(error) - << std::endl; - return; - } +// if (error) { +// std::cerr << "error " << error << ": " << lodepng_error_text(error) +// << std::endl; +// return; +// } - error = lodepng_encode32_file(filename2.c_str(), image, width, height); +// error = lodepng_encode32_file(filename2.c_str(), image, width, height); - if (error) { - std::cerr << "error " << error << ": " << lodepng_error_text(error) - << std::endl; - return; - } +// if (error) { +// std::cerr << "error " << error << ": " << lodepng_error_text(error) +// << std::endl; +// return; +// } - free(image); -} +// free(image); +// } -void test1(const std::string &images_path) { - std::cout << "test1" << std::endl; +// void test1(const std::string &images_path) { +// std::cout << "test1" << std::endl; - std::string filename1 = images_path + "/test1.png"; - std::string filename2 = images_path + "/out/test1_1out.png"; - std::string filename3 = images_path + "/out/test1_2out.png"; +// std::string filename1 = images_path + "/test1.png"; +// std::string filename2 = images_path + "/out/test1_1out.png"; +// std::string filename3 = images_path + "/out/test1_2out.png"; - decode_and_encode32(filename1, filename2); - decode_and_encode32(filename1, filename3); +// decode_and_encode32(filename1, filename2); +// decode_and_encode32(filename1, filename3); - if (!cmp_images32(filename1, filename2)) { - std::cout << "files are different" << std::endl; - } else { - std::cout << "files are not different" << std::endl; - } +// if (!cmp_images32(filename1, filename2)) { +// std::cout << "files are different" << std::endl; +// } else { +// std::cout << "files are not different" << std::endl; +// } - if (!cmp_images32(filename3, filename2)) { - std::cout << "files are different" << std::endl; - } else { - std::cout << "files are not different" << std::endl; - } -} +// if (!cmp_images32(filename3, filename2)) { +// std::cout << "files are different" << std::endl; +// } else { +// std::cout << "files are not different" << std::endl; +// } +// } -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); +// 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)); -} +// /*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; +// 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); +// } + +void generate_one_step(const std::string &images_path) { + unsigned int 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++) { + assert(image); + + 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; @@ -132,21 +153,107 @@ void test2() { } } - /*run an example*/ - // encodeOneStep(filename, image, width, height); - lodepng_encode32_file(filename, image, width, height); + // encode the image + std::string filename = images_path + "/out_generated1.png"; + unsigned int result = + lodepng_encode32_file(filename.c_str(), image, width, height); + + assert(!result); + + // after the image has been encoded, decode it to check that the + // pixel values are the same + + unsigned int width2, height2; + unsigned char *image2 = 0; + + result = lodepng_decode32_file(&image2, &width2, &height2, filename.c_str()); + + assert(!result); + + assert(width2 == width); + assert(height2 == height); + + // now, we can compare the values + for (size_t i = 0; i < width * height * 4; ++i) { + assert(image2[i] == image[i]); + } + + free(image); +} + +void generate_two_steps(const std::string &images_path) { + // generate the values + unsigned int width = 512, height = 512; + unsigned char *image = (unsigned char *)malloc(width * height * 4); + + assert(image); + + 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; + } + } + + // encode the image into memory first + std::string filename = images_path + "/out_generated2.png"; + unsigned char *png; + size_t pngsize; + + unsigned int result = lodepng_encode32(&png, &pngsize, image, width, height); + + assert(!result); + + // write the image into the file (from memory) + result = lodepng_save_file(png, pngsize, filename.c_str()); + + assert(!result); + + // now, decode the image using the 2 steps in order to compare the values + unsigned int width2, height2; + unsigned char *png2; + size_t pngsize2; + + // load the file in memory + result = lodepng_load_file(&png2, &pngsize2, filename.c_str()); + + assert(!result); + + assert(pngsize == pngsize2); + + // after the file is loaded, decode it so we have access to the values + // directly + // sapi::v::IntBase sapi_png_ptr3(0); + // result = api.lodepng_decode32( + // sapi_png_ptr3.PtrBoth(), sapi_width2.PtrBoth(), + // sapi_height2.PtrBoth(), sapi_png_array2.PtrBefore(), + // sapi_pngsize2.GetValue()); + + unsigned char *image2; + result = lodepng_decode32(&image2, &width2, &height2, png2, pngsize2); + + assert(!result); + + assert(width2 == width); + assert(height2 == height); + + // compare values + for (size_t i = 0; i < width * height * 4; ++i) { + assert(image2[i] == image[i]); + } + + free(image); } int main(int argc, char *argv[]) { - if (argc != 2) { - std::cout << "usage: " << basename(argv[0]) << " images_folder_path" - << std::endl; - return 1; - } + std::string images_path = std::filesystem::current_path().string(); - std::string images_path(argv[1]); + std::cout << "flag = " << images_path << std::endl; + + generate_one_step(images_path); + generate_two_steps(images_path); - 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 c665e58..b7f87c9 100644 --- a/oss-internship-2020/sapi_lodepng/main_sandboxed.cc +++ b/oss-internship-2020/sapi_lodepng/main_sandboxed.cc @@ -15,6 +15,7 @@ #include #include +#include #include #include #include @@ -205,13 +206,11 @@ ABSL_FLAG(string, images_path, std::filesystem::current_path().string(), // } // } -bool generate_one_step(SapiLodepngSandbox &sandbox, LodepngApi &api, +void generate_one_step(SapiLodepngSandbox &sandbox, LodepngApi &api, const std::string &images_path) { unsigned int width = 512, height = 512; unsigned char *image = (unsigned char *)malloc(width * height * 4); - if (image == NULL) { - return false; - } + assert(image); for (int y = 0; y < height; ++y) { for (int x = 0; x < width; ++x) { @@ -232,9 +231,8 @@ bool generate_one_step(SapiLodepngSandbox &sandbox, LodepngApi &api, sapi_filename.PtrBefore(), sapi_image.PtrBefore(), sapi_width.GetValue(), sapi_height.GetValue()); - if (!result.ok() || result.value()) { - return false; - } + assert(result.ok()); + assert(!result.value()); // after the image has been encoded, decode it to check that the // pixel values are the same @@ -245,14 +243,11 @@ bool generate_one_step(SapiLodepngSandbox &sandbox, LodepngApi &api, result = api.lodepng_decode32_file( sapi_image_ptr.PtrBoth(), sapi_width2.PtrBoth(), sapi_height2.PtrBoth(), sapi_filename.PtrBefore()); + assert(result.ok()); + assert(!result.value()); - if (!result.ok() || result.value()) { - return false; - } - - if (sapi_width2.GetValue() != width || sapi_height2.GetValue() != height) { - return false; - } + assert(sapi_width2.GetValue() == width); + assert(sapi_height2.GetValue() == height); // the pixels have been allocated inside the sandboxed process // memory, so we need to transfer them to this process. @@ -264,9 +259,7 @@ bool generate_one_step(SapiLodepngSandbox &sandbox, LodepngApi &api, sapi_height2.GetValue() * 4); sapi_pixels.SetRemote(sapi_remote_out_ptr.GetValue()); - if (!sandbox.TransferFromSandboxee(&sapi_pixels).ok()) { - return false; - } + assert(sandbox.TransferFromSandboxee(&sapi_pixels).ok()); // after the memory has been transferred, we can access it // using the GetData function @@ -274,24 +267,19 @@ bool generate_one_step(SapiLodepngSandbox &sandbox, LodepngApi &api, // now, we can compare the values for (size_t i = 0; i < width * height * 4; ++i) { - if (pixels_ptr[i] != image[i]) { - return false; - } + assert(pixels_ptr[i] == image[i]); } free(image); - return true; } -bool generate_two_steps(SapiLodepngSandbox &sandbox, LodepngApi &api, +void generate_two_steps(SapiLodepngSandbox &sandbox, LodepngApi &api, const std::string &images_path) { // generate the values unsigned int width = 512, height = 512; unsigned char *image = (unsigned char *)malloc(width * height * 4); - if (image == NULL) { - return false; - } + assert(image); for (int y = 0; y < height; ++y) { for (int x = 0; x < width; ++x) { @@ -312,14 +300,13 @@ bool generate_two_steps(SapiLodepngSandbox &sandbox, LodepngApi &api, sapi::v::IntBase sapi_png_ptr(0); // encode it into memory - sapi::StatusOr result = api.lodepng_encode32( sapi_png_ptr.PtrBoth(), sapi_pngsize.PtrBoth(), sapi_image.PtrBefore(), sapi_width.GetValue(), sapi_height.GetValue()); - if (!result.ok() || result.value()) { - return false; - } + assert(result.ok()); + assert(!result.value()); + // the new array (pointed to by sapi_png_ptr) is allocated // inside the sandboxed process so we need to transfer it to this // process @@ -330,20 +317,17 @@ bool generate_two_steps(SapiLodepngSandbox &sandbox, LodepngApi &api, sapi_png_array.SetRemote(sapi_remote_out_ptr.GetValue()); - if (!sandbox.TransferFromSandboxee(&sapi_png_array).ok()) { - return false; - } + assert(sandbox.TransferFromSandboxee(&sapi_png_array).ok()); // write the image into the file (from memory) result = api.lodepng_save_file(sapi_png_array.PtrBefore(), sapi_pngsize.GetValue(), sapi_filename.PtrBefore()); - if (!result.ok() || result.value()) { - return false; - } - // now, decode the image using the 2 steps in order to compare the values + assert(result.ok()); + assert(!result.value()); + // now, decode the image using the 2 steps in order to compare the values sapi::v::UInt sapi_width2, sapi_height2; sapi::v::IntBase sapi_png_ptr2(0); sapi::v::ULLong sapi_pngsize2; @@ -352,13 +336,11 @@ bool generate_two_steps(SapiLodepngSandbox &sandbox, LodepngApi &api, result = api.lodepng_load_file(sapi_png_ptr2.PtrBoth(), sapi_pngsize2.PtrBoth(), sapi_filename.PtrBefore()); - if (!result.ok() || result.value()) { - return false; - } - if (sapi_pngsize.GetValue() != sapi_pngsize2.GetValue()) { - return false; - } + assert(result.ok()); + assert(!result.value()); + + assert(sapi_pngsize.GetValue() == sapi_pngsize2.GetValue()); // transfer the png array sapi::v::RemotePtr sapi_remote_out_ptr2( @@ -367,9 +349,7 @@ bool generate_two_steps(SapiLodepngSandbox &sandbox, LodepngApi &api, sapi_png_array2.SetRemote(sapi_remote_out_ptr2.GetValue()); - if (!sandbox.TransferFromSandboxee(&sapi_png_array2).ok()) { - return false; - } + assert(sandbox.TransferFromSandboxee(&sapi_png_array2).ok()); // after the file is loaded, decode it so we have access to the values // directly @@ -378,13 +358,11 @@ bool generate_two_steps(SapiLodepngSandbox &sandbox, LodepngApi &api, sapi_png_ptr3.PtrBoth(), sapi_width2.PtrBoth(), sapi_height2.PtrBoth(), sapi_png_array2.PtrBefore(), sapi_pngsize2.GetValue()); - if (!result.ok() || result.value()) { - return false; - } + assert(result.ok()); + assert(!result.value()); - if (sapi_width2.GetValue() != width || sapi_height2.GetValue() != height) { - return false; - } + assert(sapi_width2.GetValue() == width); + assert(sapi_height2.GetValue() == height); // transfer the pixels so they can be used sapi::v::RemotePtr sapi_remote_out_ptr3( @@ -394,22 +372,16 @@ bool generate_two_steps(SapiLodepngSandbox &sandbox, LodepngApi &api, sapi_pixels.SetRemote(sapi_remote_out_ptr3.GetValue()); - if (!sandbox.TransferFromSandboxee(&sapi_pixels).ok()) { - return false; - } + assert(sandbox.TransferFromSandboxee(&sapi_pixels).ok()); unsigned char *pixels_ptr = sapi_pixels.GetData(); // compare values for (size_t i = 0; i < width * height * 4; ++i) { - if (pixels_ptr[i] != image[i]) { - return false; - } + assert(pixels_ptr[i] == image[i]); } free(image); - - return true; } int main(int argc, char *argv[]) { @@ -430,17 +402,8 @@ int main(int argc, char *argv[]) { LodepngApi api(&sandbox); - if (generate_one_step(sandbox, api, images_path)) { - std::cout << "first example ok" << std::endl; - } else { - std::cout << "first example did not work properly" << std::endl; - } - - if (generate_two_steps(sandbox, api, images_path)) { - std::cout << "second example ok" << std::endl; - } else { - std::cout << "second example did not work properly" << std::endl; - } + generate_one_step(sandbox, api, images_path); + generate_two_steps(sandbox, api, images_path); return EXIT_SUCCESS; } \ No newline at end of file diff --git a/oss-internship-2020/sapi_lodepng/main_unit_test.cc b/oss-internship-2020/sapi_lodepng/main_unit_test.cc index 55cc687..cbe9aa5 100644 --- a/oss-internship-2020/sapi_lodepng/main_unit_test.cc +++ b/oss-internship-2020/sapi_lodepng/main_unit_test.cc @@ -141,7 +141,6 @@ TEST(generate_image, encode_decode_compare_two_step) { sapi::v::IntBase sapi_png_ptr(0); // encode it into memory - sapi::StatusOr result = api.lodepng_encode32( sapi_png_ptr.PtrBoth(), sapi_pngsize.PtrBoth(), sapi_image.PtrBefore(), sapi_width.GetValue(), sapi_height.GetValue()); @@ -170,7 +169,6 @@ TEST(generate_image, encode_decode_compare_two_step) { ASSERT_EQ(result.value(), 0); // now, decode the image using the 2 steps in order to compare the values - sapi::v::UInt sapi_width2, sapi_height2; sapi::v::IntBase sapi_png_ptr2(0); sapi::v::ULLong sapi_pngsize2;