use std::vector instead of plain C pointers. constexpr for image length

This commit is contained in:
Andrei Medar 2020-08-13 14:44:18 +00:00
parent ce5b7a3789
commit 28d671efd7
3 changed files with 41 additions and 356 deletions

View File

@ -18,132 +18,16 @@
#include <cassert>
#include <filesystem>
#include <iostream>
// #include <libgen.h>
#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;
// unsigned int error, width1, height1;
// unsigned char *image1 = 0;
// unsigned int width2, height2;
// unsigned char *image2 = 0;
// error = lodepng_decode32_file(&image1, &width1, &height1, f1.c_str());
// if (error) {
// std::cerr << "error " << error << ": " << lodepng_error_text(error)
// << std::endl;
// return false;
// }
// 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 (width1 != width2 || height1 != height2) {
// std::cerr << "DIMENSIONS DIFFER\n";
// return false;
// }
// 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;
// }
// // 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());
// if (error) {
// std::cerr << "error " << error << ": " << lodepng_error_text(error)
// << std::endl;
// return;
// }
// error = lodepng_encode32_file(filename2.c_str(), image, width, height);
// if (error) {
// std::cerr << "error " << error << ": " << lodepng_error_text(error)
// << std::endl;
// return;
// }
// free(image);
// }
// 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";
// 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(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);
// /*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);
// }
constexpr unsigned int img_len(unsigned int width, unsigned int height) {
return width * height * 4;
}
void generate_one_step(const std::string &images_path) {
unsigned int width = 512, height = 512;
unsigned char *image = (unsigned char *)malloc(width * height * 4);
assert(image);
std::vector<unsigned char> image(img_len(width, height));
for (int y = 0; y < height; ++y) {
for (int x = 0; x < width; ++x) {
@ -157,7 +41,7 @@ void generate_one_step(const std::string &images_path) {
// encode the image
std::string filename = images_path + "/out_generated1.png";
unsigned int result =
lodepng_encode32_file(filename.c_str(), image, width, height);
lodepng_encode32_file(filename.c_str(), image.data(), width, height);
assert(!result);
@ -175,19 +59,15 @@ void generate_one_step(const std::string &images_path) {
assert(height2 == height);
// now, we can compare the values
for (size_t i = 0; i < width * height * 4; ++i) {
for (size_t i = 0; i < img_len(width, height); ++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);
std::vector<unsigned char> image(img_len(width, height));
for (int y = 0; y < height; ++y) {
for (int x = 0; x < width; ++x) {
@ -203,7 +83,8 @@ void generate_two_steps(const std::string &images_path) {
unsigned char *png;
size_t pngsize;
unsigned int result = lodepng_encode32(&png, &pngsize, image, width, height);
unsigned int result =
lodepng_encode32(&png, &pngsize, image.data(), width, height);
assert(!result);
@ -221,31 +102,19 @@ void generate_two_steps(const std::string &images_path) {
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<unsigned char *> 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) {
for (size_t i = 0; i < img_len(width, height); ++i) {
assert(image2[i] == image[i]);
}
free(image);
}
int main(int argc, char *argv[]) {

View File

@ -29,187 +29,13 @@ ABSL_DECLARE_FLAG(string, sandbox2_danger_danger_permit_all_and_log);
ABSL_FLAG(string, images_path, std::filesystem::current_path().string(),
"path to the folder containing test images");
// // takes a png image (f1), decodes it and ecodes it into f2.
// // can be viewed as copying f1 into f2. This function has a basic usage
// // of the decode and encode functions.
// void decode_and_encode32(SapiLodepngSandbox &sandbox, LodepngApi &api,
// const std::string &f1, const std::string &f2) {
// sapi::v::UInt width(0), height(0);
// sapi::v::ConstCStr filename1(f1.c_str()), filename2(f2.c_str());
// absl::Status ret;
// // in order to pass unsigned char ** to the function, we pass a variable
// that
// // contains the pointer.
// sapi::v::IntBase<unsigned char *> image(0);
// if (!api.lodepng_decode32_file(image.PtrBoth(), width.PtrBoth(),
// height.PtrBoth(), filename1.PtrBefore())
// .ok()) {
// std::cerr << "decode failed" << std::endl;
// exit(1);
// }
// // after the function is called, we need to access the data stored at the
// // address to which the previous variable points. To do that, we have to
// // transfer the data from the sandbox memory to this process's memory.
// sapi::v::RemotePtr remote_out_ptr(reinterpret_cast<void
// *>(image.GetValue())); sapi::v::Array<unsigned char>
// out_img(width.GetValue() * height.GetValue());
// out_img.SetRemote(remote_out_ptr.GetValue());
// if (!sandbox.TransferFromSandboxee(&out_img).ok()) {
// std::cerr << "Transfer From Sandboxee failed" << std::endl;
// exit(1);
// }
// // now the values are available at out_img.GetData()
// // when calling the encoding function, we need only an unsigned char *
// // (instead of **) so we can simply use the sapi::v::Array defined before
// // (PtrBefore will give us a pointer).
// if (!api.lodepng_encode32_file(filename2.PtrBefore(), out_img.PtrBefore(),
// width.GetValue(), height.GetValue())
// .ok()) {
// std::cerr << "encode failed" << std::endl;
// exit(1);
// }
// // Since in this function we do not actually used the pixels, we could
// simply
// // call the encode function with the pointer from before (the remote
// pointer,
// // since the memory has already been allocated on the sandboxed process).
// // However, most of the use cases of this library require accessing the
// pixels
// // which is why the solution in which data is transferred around is used.
// }
// // 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;
// // }
// 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 * 4);
// 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();
// 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.
// bool cmp_images32(SapiLodepngSandbox &sandbox, LodepngApi &api,
// const std::string &f1, const std::string &f2) {
// std::cout << "COMPARING IMAGES " << basename(f1.c_str()) << " --- "
// << basename(f2.c_str()) << std::endl;
// sapi::v::UInt width1, height1, width2, height2;
// sapi::v::ConstCStr filename1(f1.c_str()), filename2(f2.c_str());
// sapi::v::IntBase<unsigned char *> image1_ptr(0), image2_ptr(0);
// // absl::Status ret;
// if (!api.lodepng_decode32_file(image1_ptr.PtrBoth(), width1.PtrBoth(),
// height1.PtrBoth(), filename1.PtrBefore())
// .ok()) {
// std::cerr << "decode failed" << std::endl;
// exit(1);
// }
// if (!api.lodepng_decode32_file(image2_ptr.PtrBoth(), width2.PtrBoth(),
// height2.PtrBoth(), filename2.PtrBefore())
// .ok()) {
// std::cerr << "decode failed" << std::endl;
// exit(1);
// }
// sapi::v::RemotePtr remote_out_ptr1(
// reinterpret_cast<void *>(image1_ptr.GetValue()));
// sapi::v::Array<char> pixels1(width1.GetValue() * height1.GetValue());
// pixels1.SetRemote(remote_out_ptr1.GetValue());
// if (!sandbox.TransferFromSandboxee(&pixels1).ok()) {
// std::cerr << "Transfer From Sandboxee failed" << std::endl;
// exit(1);
// }
// sapi::v::RemotePtr remote_out_ptr2(
// reinterpret_cast<void *>(image2_ptr.GetValue()));
// sapi::v::Array<char> pixels2(width2.GetValue() * height2.GetValue());
// pixels2.SetRemote(remote_out_ptr2.GetValue());
// if (!sandbox.TransferFromSandboxee(&pixels2).ok()) {
// std::cerr << "Transfer From Sandboxee failed" << std::endl;
// exit(1);
// }
// if (width1.GetValue() != width2.GetValue() ||
// height1.GetValue() != height2.GetValue()) {
// std::cerr << "DIMENSIONS DIFFER\n";
// return false;
// }
// for (size_t i = 0; i < width1.GetValue() * height1.GetValue(); ++i) {
// if (pixels1.GetData()[i] != pixels2.GetData()[i]) {
// std::cerr << "PIXELS DIFFER AT i = " << i << std::endl;
// return false;
// }
// }
// return true;
// }
// // this test simply copies the png from filename1 to filename2 and filename3
// // and then decodes those 2 files and compares the pixels. If those pixels
// are
// // equal, then encoding and decoding worked.
// void test1(SapiLodepngSandbox &sandbox, LodepngApi &api,
// const std::string &images_path) {
// 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(sandbox, api, filename1, filename2);
// decode_and_encode32(sandbox, api, filename1, filename3);
// if (!cmp_images32(sandbox, api, filename1, filename2)) {
// std::cout << "files are different" << std::endl;
// } else {
// std::cout << "files are not different" << std::endl;
// }
// if (!cmp_images32(sandbox, api, filename3, filename2)) {
// std::cout << "files are different" << std::endl;
// } else {
// std::cout << "files are not different" << std::endl;
// }
// }
constexpr unsigned int img_len(unsigned int width, unsigned int height) {
return width * height * 4;
}
void generate_one_step(SapiLodepngSandbox &sandbox, LodepngApi &api) {
unsigned int width = 512, height = 512;
unsigned char *image = (unsigned char *)malloc(width * height * 4);
assert(image);
std::vector<unsigned char> image(img_len(width, height));
for (int y = 0; y < height; ++y) {
for (int x = 0; x < width; ++x) {
@ -221,7 +47,7 @@ void generate_one_step(SapiLodepngSandbox &sandbox, LodepngApi &api) {
}
// encode the image
sapi::v::Array<unsigned char> sapi_image(image, width * height * 4);
sapi::v::Array<unsigned char> sapi_image(image.data(), img_len(width, height));
sapi::v::UInt sapi_width(width), sapi_height(height);
std::string filename = "/output/out_generated1.png";
sapi::v::ConstCStr sapi_filename(filename.c_str());
@ -259,8 +85,8 @@ void generate_one_step(SapiLodepngSandbox &sandbox, LodepngApi &api) {
// 4) transfer the memory to this process (this step is why we need
// the pointer and the length)
sapi::v::RemotePtr sapi_remote_out_ptr(sapi_image_ptr.GetValue());
sapi::v::Array<unsigned char> sapi_pixels(sapi_width2.GetValue() *
sapi_height2.GetValue() * 4);
sapi::v::Array<unsigned char> sapi_pixels(
img_len(sapi_width2.GetValue(), sapi_height2.GetValue()));
sapi_pixels.SetRemote(sapi_remote_out_ptr.GetValue());
assert(sandbox.TransferFromSandboxee(&sapi_pixels).ok());
@ -270,19 +96,15 @@ void generate_one_step(SapiLodepngSandbox &sandbox, LodepngApi &api) {
unsigned char *pixels_ptr = sapi_pixels.GetData();
// now, we can compare the values
for (size_t i = 0; i < width * height * 4; ++i) {
for (size_t i = 0; i < img_len(width, height); ++i) {
assert(pixels_ptr[i] == image[i]);
}
free(image);
}
void generate_two_steps(SapiLodepngSandbox &sandbox, LodepngApi &api) {
// generate the values
unsigned int width = 512, height = 512;
unsigned char *image = (unsigned char *)malloc(width * height * 4);
assert(image);
std::vector<unsigned char> image(img_len(width, height));
for (int y = 0; y < height; ++y) {
for (int x = 0; x < width; ++x) {
@ -294,7 +116,7 @@ void generate_two_steps(SapiLodepngSandbox &sandbox, LodepngApi &api) {
}
// encode the image into memory first
sapi::v::Array<unsigned char> sapi_image(image, width * height * 4);
sapi::v::Array<unsigned char> sapi_image(image.data(), img_len(width, height));
sapi::v::UInt sapi_width(width), sapi_height(height);
std::string filename = "/output/out_generated2.png";
sapi::v::ConstCStr sapi_filename(filename.c_str());
@ -367,8 +189,8 @@ void generate_two_steps(SapiLodepngSandbox &sandbox, LodepngApi &api) {
// transfer the pixels so they can be used
sapi::v::RemotePtr sapi_remote_out_ptr3(sapi_png_ptr3.GetValue());
sapi::v::Array<unsigned char> sapi_pixels(sapi_width2.GetValue() *
sapi_height2.GetValue() * 4);
sapi::v::Array<unsigned char> sapi_pixels(
img_len(sapi_width2.GetValue(), sapi_height2.GetValue()));
sapi_pixels.SetRemote(sapi_remote_out_ptr3.GetValue());
@ -377,11 +199,9 @@ void generate_two_steps(SapiLodepngSandbox &sandbox, LodepngApi &api) {
unsigned char *pixels_ptr = sapi_pixels.GetData();
// compare values
for (size_t i = 0; i < width * height * 4; ++i) {
for (size_t i = 0; i < img_len(width, height); ++i) {
assert(pixels_ptr[i] == image[i]);
}
free(image);
}
int main(int argc, char *argv[]) {

View File

@ -23,14 +23,16 @@
#include "sandboxed_api/util/flag.h"
#include "sandboxed_api/util/status_matchers.h"
using testing::NotNull;
using testing::Eq;
using sapi::IsOk;
using testing::Eq;
using testing::NotNull;
namespace {
constexpr unsigned int img_len(unsigned int width, unsigned int height) {
return width * height * 4;
}
// use the current path
std::string images_path = std::filesystem::current_path().string();
@ -48,9 +50,7 @@ TEST(generate_image, encode_decode_compare_one_step) {
// generate the values
unsigned int width = 512, height = 512;
unsigned char *image = (unsigned char *)malloc(width * height * 4);
ASSERT_THAT(image, NotNull());
std::vector<unsigned char> image(img_len(width, height));
for (int y = 0; y < height; ++y) {
for (int x = 0; x < width; ++x) {
@ -62,7 +62,8 @@ TEST(generate_image, encode_decode_compare_one_step) {
}
// encode the image
sapi::v::Array<unsigned char> sapi_image(image, width * height * 4);
sapi::v::Array<unsigned char> sapi_image(image.data(),
img_len(width, height));
sapi::v::UInt sapi_width(width), sapi_height(height);
std::string filename = "/output/out_generated1.png";
sapi::v::ConstCStr sapi_filename(filename.c_str());
@ -101,8 +102,8 @@ TEST(generate_image, encode_decode_compare_one_step) {
// 4) transfer the memory to this process (this step is why we need
// the pointer and the length)
sapi::v::RemotePtr sapi_remote_out_ptr(sapi_image_ptr.GetValue());
sapi::v::Array<unsigned char> sapi_pixels(sapi_width2.GetValue() *
sapi_height2.GetValue() * 4);
sapi::v::Array<unsigned char> sapi_pixels(
img_len(sapi_width2.GetValue(), sapi_height2.GetValue()));
sapi_pixels.SetRemote(sapi_remote_out_ptr.GetValue());
ASSERT_THAT(sandbox.TransferFromSandboxee(&sapi_pixels), IsOk());
@ -112,11 +113,9 @@ TEST(generate_image, encode_decode_compare_one_step) {
unsigned char *pixels_ptr = sapi_pixels.GetData();
// now, we can compare the values
for (size_t i = 0; i < width * height * 4; ++i) {
for (size_t i = 0; i < img_len(width, height); ++i) {
EXPECT_THAT(pixels_ptr[i], Eq(image[i]));
}
free(image);
}
// similar to the previous test, only that we use encoding by saving the data in
@ -129,9 +128,7 @@ TEST(generate_image, encode_decode_compare_two_steps) {
// generate the values
unsigned int width = 512, height = 512;
unsigned char *image = (unsigned char *)malloc(width * height * 4);
ASSERT_THAT(image, NotNull());
std::vector<unsigned char> image(img_len(width, height));
for (int y = 0; y < height; ++y) {
for (int x = 0; x < width; ++x) {
@ -143,7 +140,8 @@ TEST(generate_image, encode_decode_compare_two_steps) {
}
// encode the image into memory first
sapi::v::Array<unsigned char> sapi_image(image, width * height * 4);
sapi::v::Array<unsigned char> sapi_image(image.data(),
img_len(width, height));
sapi::v::UInt sapi_width(width), sapi_height(height);
std::string filename = "/output/out_generated2.png";
sapi::v::ConstCStr sapi_filename(filename.c_str());
@ -218,8 +216,8 @@ TEST(generate_image, encode_decode_compare_two_steps) {
// transfer the pixels so they can be used
sapi::v::RemotePtr sapi_remote_out_ptr3(sapi_png_ptr3.GetValue());
sapi::v::Array<unsigned char> sapi_pixels(sapi_width2.GetValue() *
sapi_height2.GetValue() * 4);
sapi::v::Array<unsigned char> sapi_pixels(
img_len(sapi_width2.GetValue(), sapi_height2.GetValue()));
sapi_pixels.SetRemote(sapi_remote_out_ptr3.GetValue());
@ -228,11 +226,9 @@ TEST(generate_image, encode_decode_compare_two_steps) {
unsigned char *pixels_ptr = sapi_pixels.GetData();
// compare values
for (size_t i = 0; i < width * height * 4; ++i) {
for (size_t i = 0; i < img_len(width, height); ++i) {
EXPECT_THAT(pixels_ptr[i], Eq(image[i]));
}
free(image);
}
} // namespace