mirror of
https://github.com/google/sandboxed-api.git
synced 2024-03-22 13:11:30 +08:00
added same examples to normal main.cc. Modified code to use assert
This commit is contained in:
parent
c13895e5a8
commit
27fcf0eb02
|
@ -15,116 +15,137 @@
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
#include <cassert>
|
||||||
|
#include <filesystem>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
|
||||||
#include "lodepng/lodepng.h"
|
#include "lodepng/lodepng.h"
|
||||||
|
|
||||||
bool cmp_images32(const std::string &f1, const std::string &f2) {
|
// bool cmp_images32(const std::string &f1, const std::string &f2) {
|
||||||
std::cout << "COMPARING IMAGES " << basename(f1.c_str()) << " -> "
|
// std::cout << "COMPARING IMAGES " << basename(f1.c_str()) << " -> "
|
||||||
<< basename(f2.c_str()) << std::endl;
|
// << basename(f2.c_str()) << std::endl;
|
||||||
|
|
||||||
unsigned int error, width1, height1;
|
// unsigned int error, width1, height1;
|
||||||
unsigned char *image1 = 0;
|
// unsigned char *image1 = 0;
|
||||||
|
|
||||||
unsigned int width2, height2;
|
// unsigned int width2, height2;
|
||||||
unsigned char *image2 = 0;
|
// 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) {
|
// if (error) {
|
||||||
std::cerr << "error " << error << ": " << lodepng_error_text(error)
|
// std::cerr << "error " << error << ": " << lodepng_error_text(error)
|
||||||
<< std::endl;
|
// << std::endl;
|
||||||
return false;
|
// return false;
|
||||||
}
|
// }
|
||||||
|
|
||||||
error = lodepng_decode32_file(&image2, &width2, &height2, f2.c_str());
|
// error = lodepng_decode32_file(&image2, &width2, &height2, f2.c_str());
|
||||||
|
|
||||||
if (error) {
|
// if (error) {
|
||||||
std::cerr << "error " << error << ": " << lodepng_error_text(error)
|
// std::cerr << "error " << error << ": " << lodepng_error_text(error)
|
||||||
<< std::endl;
|
// << std::endl;
|
||||||
return false;
|
// return false;
|
||||||
}
|
// }
|
||||||
|
|
||||||
if (width1 != width2 || height1 != height2) {
|
// if (width1 != width2 || height1 != height2) {
|
||||||
std::cerr << "DIMENSIONS DIFFER\n";
|
// std::cerr << "DIMENSIONS DIFFER\n";
|
||||||
return false;
|
// 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) {
|
// for (int i = 0; i < width1 * height1; ++i) {
|
||||||
if (image1[i] != image2[i]) {
|
// if (image1[i] != image2[i]) {
|
||||||
std::cerr << "PIXELS DIFFER AT i = " << i << std::endl;
|
// std::cerr << "PIXELS DIFFER AT i = " << i << std::endl;
|
||||||
return false;
|
// return false;
|
||||||
}
|
// }
|
||||||
}
|
// }
|
||||||
return true;
|
// return true;
|
||||||
}
|
// }
|
||||||
|
|
||||||
// copies an image into another and compares them
|
// // copies an image into another and compares them
|
||||||
void decode_and_encode32(const std::string &filename1,
|
// void decode_and_encode32(const std::string &filename1,
|
||||||
const std::string &filename2) {
|
// const std::string &filename2) {
|
||||||
unsigned int error, width, height;
|
// unsigned int error, width, height;
|
||||||
unsigned char *image = 0;
|
// 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) {
|
// if (error) {
|
||||||
std::cerr << "error " << error << ": " << lodepng_error_text(error)
|
// std::cerr << "error " << error << ": " << lodepng_error_text(error)
|
||||||
<< std::endl;
|
// << std::endl;
|
||||||
return;
|
// return;
|
||||||
}
|
// }
|
||||||
|
|
||||||
error = lodepng_encode32_file(filename2.c_str(), image, width, height);
|
// error = lodepng_encode32_file(filename2.c_str(), image, width, height);
|
||||||
|
|
||||||
if (error) {
|
// if (error) {
|
||||||
std::cerr << "error " << error << ": " << lodepng_error_text(error)
|
// std::cerr << "error " << error << ": " << lodepng_error_text(error)
|
||||||
<< std::endl;
|
// << std::endl;
|
||||||
return;
|
// return;
|
||||||
}
|
// }
|
||||||
|
|
||||||
free(image);
|
// free(image);
|
||||||
}
|
// }
|
||||||
|
|
||||||
void test1(const std::string &images_path) {
|
// void test1(const std::string &images_path) {
|
||||||
std::cout << "test1" << std::endl;
|
// std::cout << "test1" << std::endl;
|
||||||
|
|
||||||
std::string filename1 = images_path + "/test1.png";
|
// std::string filename1 = images_path + "/test1.png";
|
||||||
std::string filename2 = images_path + "/out/test1_1out.png";
|
// std::string filename2 = images_path + "/out/test1_1out.png";
|
||||||
std::string filename3 = images_path + "/out/test1_2out.png";
|
// std::string filename3 = images_path + "/out/test1_2out.png";
|
||||||
|
|
||||||
decode_and_encode32(filename1, filename2);
|
// decode_and_encode32(filename1, filename2);
|
||||||
decode_and_encode32(filename1, filename3);
|
// decode_and_encode32(filename1, filename3);
|
||||||
|
|
||||||
if (!cmp_images32(filename1, filename2)) {
|
// if (!cmp_images32(filename1, filename2)) {
|
||||||
std::cout << "files are different" << std::endl;
|
// std::cout << "files are different" << std::endl;
|
||||||
} else {
|
// } else {
|
||||||
std::cout << "files are not different" << std::endl;
|
// std::cout << "files are not different" << std::endl;
|
||||||
}
|
// }
|
||||||
|
|
||||||
if (!cmp_images32(filename3, filename2)) {
|
// if (!cmp_images32(filename3, filename2)) {
|
||||||
std::cout << "files are different" << std::endl;
|
// std::cout << "files are different" << std::endl;
|
||||||
} else {
|
// } else {
|
||||||
std::cout << "files are not different" << std::endl;
|
// std::cout << "files are not different" << std::endl;
|
||||||
}
|
// }
|
||||||
}
|
// }
|
||||||
|
|
||||||
void encodeOneStep(const char *filename, const unsigned char *image,
|
// void encodeOneStep(const char *filename, const unsigned char *image,
|
||||||
unsigned width, unsigned height) {
|
// unsigned width, unsigned height) {
|
||||||
/*Encode the image*/
|
// /*Encode the image*/
|
||||||
unsigned error = lodepng_encode32_file(filename, image, width, height);
|
// unsigned error = lodepng_encode32_file(filename, image, width, height);
|
||||||
|
|
||||||
/*if there's an error, display it*/
|
// /*if there's an error, display it*/
|
||||||
if (error) printf("error %u: %s\n", error, lodepng_error_text(error));
|
// if (error) printf("error %u: %s\n", error, lodepng_error_text(error));
|
||||||
}
|
// }
|
||||||
|
|
||||||
void test2() {
|
// void test2() {
|
||||||
const char *filename = "test_images/out/ok.png";
|
// const char *filename = "test_images/out/ok.png";
|
||||||
unsigned width = 512, height = 512;
|
// 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 char *image = (unsigned char *)malloc(width * height * 4);
|
||||||
unsigned x, y;
|
assert(image);
|
||||||
for (y = 0; y < height; y++) {
|
|
||||||
for (x = 0; x < width; x++) {
|
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 + 0] = 255 * !(x & y);
|
||||||
image[4 * width * y + 4 * x + 1] = 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 + 2] = x | y;
|
||||||
|
@ -132,21 +153,107 @@ void test2() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/*run an example*/
|
// encode the image
|
||||||
// encodeOneStep(filename, image, width, height);
|
std::string filename = images_path + "/out_generated1.png";
|
||||||
lodepng_encode32_file(filename, image, width, height);
|
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<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) {
|
||||||
|
assert(image2[i] == image[i]);
|
||||||
|
}
|
||||||
|
|
||||||
|
free(image);
|
||||||
}
|
}
|
||||||
|
|
||||||
int main(int argc, char *argv[]) {
|
int main(int argc, char *argv[]) {
|
||||||
if (argc != 2) {
|
std::string images_path = std::filesystem::current_path().string();
|
||||||
std::cout << "usage: " << basename(argv[0]) << " images_folder_path"
|
|
||||||
<< std::endl;
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
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;
|
return 0;
|
||||||
}
|
}
|
|
@ -15,6 +15,7 @@
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
#include <cassert>
|
||||||
#include <cstdlib>
|
#include <cstdlib>
|
||||||
#include <filesystem>
|
#include <filesystem>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
@ -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) {
|
const std::string &images_path) {
|
||||||
unsigned int width = 512, height = 512;
|
unsigned int width = 512, height = 512;
|
||||||
unsigned char *image = (unsigned char *)malloc(width * height * 4);
|
unsigned char *image = (unsigned char *)malloc(width * height * 4);
|
||||||
if (image == NULL) {
|
assert(image);
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
for (int y = 0; y < height; ++y) {
|
for (int y = 0; y < height; ++y) {
|
||||||
for (int x = 0; x < width; ++x) {
|
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_filename.PtrBefore(), sapi_image.PtrBefore(), sapi_width.GetValue(),
|
||||||
sapi_height.GetValue());
|
sapi_height.GetValue());
|
||||||
|
|
||||||
if (!result.ok() || result.value()) {
|
assert(result.ok());
|
||||||
return false;
|
assert(!result.value());
|
||||||
}
|
|
||||||
|
|
||||||
// after the image has been encoded, decode it to check that the
|
// after the image has been encoded, decode it to check that the
|
||||||
// pixel values are the same
|
// pixel values are the same
|
||||||
|
@ -245,14 +243,11 @@ bool generate_one_step(SapiLodepngSandbox &sandbox, LodepngApi &api,
|
||||||
result = api.lodepng_decode32_file(
|
result = api.lodepng_decode32_file(
|
||||||
sapi_image_ptr.PtrBoth(), sapi_width2.PtrBoth(), sapi_height2.PtrBoth(),
|
sapi_image_ptr.PtrBoth(), sapi_width2.PtrBoth(), sapi_height2.PtrBoth(),
|
||||||
sapi_filename.PtrBefore());
|
sapi_filename.PtrBefore());
|
||||||
|
assert(result.ok());
|
||||||
|
assert(!result.value());
|
||||||
|
|
||||||
if (!result.ok() || result.value()) {
|
assert(sapi_width2.GetValue() == width);
|
||||||
return false;
|
assert(sapi_height2.GetValue() == height);
|
||||||
}
|
|
||||||
|
|
||||||
if (sapi_width2.GetValue() != width || sapi_height2.GetValue() != height) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
// the pixels have been allocated inside the sandboxed process
|
// the pixels have been allocated inside the sandboxed process
|
||||||
// memory, so we need to transfer them to this 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_height2.GetValue() * 4);
|
||||||
sapi_pixels.SetRemote(sapi_remote_out_ptr.GetValue());
|
sapi_pixels.SetRemote(sapi_remote_out_ptr.GetValue());
|
||||||
|
|
||||||
if (!sandbox.TransferFromSandboxee(&sapi_pixels).ok()) {
|
assert(sandbox.TransferFromSandboxee(&sapi_pixels).ok());
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
// after the memory has been transferred, we can access it
|
// after the memory has been transferred, we can access it
|
||||||
// using the GetData function
|
// using the GetData function
|
||||||
|
@ -274,24 +267,19 @@ bool generate_one_step(SapiLodepngSandbox &sandbox, LodepngApi &api,
|
||||||
|
|
||||||
// now, we can compare the values
|
// now, we can compare the values
|
||||||
for (size_t i = 0; i < width * height * 4; ++i) {
|
for (size_t i = 0; i < width * height * 4; ++i) {
|
||||||
if (pixels_ptr[i] != image[i]) {
|
assert(pixels_ptr[i] == image[i]);
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
free(image);
|
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) {
|
const std::string &images_path) {
|
||||||
// generate the values
|
// generate the values
|
||||||
unsigned int width = 512, height = 512;
|
unsigned int width = 512, height = 512;
|
||||||
unsigned char *image = (unsigned char *)malloc(width * height * 4);
|
unsigned char *image = (unsigned char *)malloc(width * height * 4);
|
||||||
|
|
||||||
if (image == NULL) {
|
assert(image);
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
for (int y = 0; y < height; ++y) {
|
for (int y = 0; y < height; ++y) {
|
||||||
for (int x = 0; x < width; ++x) {
|
for (int x = 0; x < width; ++x) {
|
||||||
|
@ -312,14 +300,13 @@ bool generate_two_steps(SapiLodepngSandbox &sandbox, LodepngApi &api,
|
||||||
sapi::v::IntBase<unsigned char *> sapi_png_ptr(0);
|
sapi::v::IntBase<unsigned char *> sapi_png_ptr(0);
|
||||||
|
|
||||||
// encode it into memory
|
// encode it into memory
|
||||||
|
|
||||||
sapi::StatusOr<unsigned int> result = api.lodepng_encode32(
|
sapi::StatusOr<unsigned int> result = api.lodepng_encode32(
|
||||||
sapi_png_ptr.PtrBoth(), sapi_pngsize.PtrBoth(), sapi_image.PtrBefore(),
|
sapi_png_ptr.PtrBoth(), sapi_pngsize.PtrBoth(), sapi_image.PtrBefore(),
|
||||||
sapi_width.GetValue(), sapi_height.GetValue());
|
sapi_width.GetValue(), sapi_height.GetValue());
|
||||||
|
|
||||||
if (!result.ok() || result.value()) {
|
assert(result.ok());
|
||||||
return false;
|
assert(!result.value());
|
||||||
}
|
|
||||||
// the new array (pointed to by sapi_png_ptr) is allocated
|
// the new array (pointed to by sapi_png_ptr) is allocated
|
||||||
// inside the sandboxed process so we need to transfer it to this
|
// inside the sandboxed process so we need to transfer it to this
|
||||||
// process
|
// process
|
||||||
|
@ -330,20 +317,17 @@ bool generate_two_steps(SapiLodepngSandbox &sandbox, LodepngApi &api,
|
||||||
|
|
||||||
sapi_png_array.SetRemote(sapi_remote_out_ptr.GetValue());
|
sapi_png_array.SetRemote(sapi_remote_out_ptr.GetValue());
|
||||||
|
|
||||||
if (!sandbox.TransferFromSandboxee(&sapi_png_array).ok()) {
|
assert(sandbox.TransferFromSandboxee(&sapi_png_array).ok());
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
// write the image into the file (from memory)
|
// write the image into the file (from memory)
|
||||||
result =
|
result =
|
||||||
api.lodepng_save_file(sapi_png_array.PtrBefore(), sapi_pngsize.GetValue(),
|
api.lodepng_save_file(sapi_png_array.PtrBefore(), sapi_pngsize.GetValue(),
|
||||||
sapi_filename.PtrBefore());
|
sapi_filename.PtrBefore());
|
||||||
|
|
||||||
if (!result.ok() || result.value()) {
|
assert(result.ok());
|
||||||
return false;
|
assert(!result.value());
|
||||||
}
|
|
||||||
// now, decode the image using the 2 steps in order to compare the values
|
|
||||||
|
|
||||||
|
// now, decode the image using the 2 steps in order to compare the values
|
||||||
sapi::v::UInt sapi_width2, sapi_height2;
|
sapi::v::UInt sapi_width2, sapi_height2;
|
||||||
sapi::v::IntBase<unsigned char *> sapi_png_ptr2(0);
|
sapi::v::IntBase<unsigned char *> sapi_png_ptr2(0);
|
||||||
sapi::v::ULLong sapi_pngsize2;
|
sapi::v::ULLong sapi_pngsize2;
|
||||||
|
@ -352,13 +336,11 @@ bool generate_two_steps(SapiLodepngSandbox &sandbox, LodepngApi &api,
|
||||||
result =
|
result =
|
||||||
api.lodepng_load_file(sapi_png_ptr2.PtrBoth(), sapi_pngsize2.PtrBoth(),
|
api.lodepng_load_file(sapi_png_ptr2.PtrBoth(), sapi_pngsize2.PtrBoth(),
|
||||||
sapi_filename.PtrBefore());
|
sapi_filename.PtrBefore());
|
||||||
if (!result.ok() || result.value()) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (sapi_pngsize.GetValue() != sapi_pngsize2.GetValue()) {
|
assert(result.ok());
|
||||||
return false;
|
assert(!result.value());
|
||||||
}
|
|
||||||
|
assert(sapi_pngsize.GetValue() == sapi_pngsize2.GetValue());
|
||||||
|
|
||||||
// transfer the png array
|
// transfer the png array
|
||||||
sapi::v::RemotePtr sapi_remote_out_ptr2(
|
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());
|
sapi_png_array2.SetRemote(sapi_remote_out_ptr2.GetValue());
|
||||||
|
|
||||||
if (!sandbox.TransferFromSandboxee(&sapi_png_array2).ok()) {
|
assert(sandbox.TransferFromSandboxee(&sapi_png_array2).ok());
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
// after the file is loaded, decode it so we have access to the values
|
// after the file is loaded, decode it so we have access to the values
|
||||||
// directly
|
// 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_ptr3.PtrBoth(), sapi_width2.PtrBoth(), sapi_height2.PtrBoth(),
|
||||||
sapi_png_array2.PtrBefore(), sapi_pngsize2.GetValue());
|
sapi_png_array2.PtrBefore(), sapi_pngsize2.GetValue());
|
||||||
|
|
||||||
if (!result.ok() || result.value()) {
|
assert(result.ok());
|
||||||
return false;
|
assert(!result.value());
|
||||||
}
|
|
||||||
|
|
||||||
if (sapi_width2.GetValue() != width || sapi_height2.GetValue() != height) {
|
assert(sapi_width2.GetValue() == width);
|
||||||
return false;
|
assert(sapi_height2.GetValue() == height);
|
||||||
}
|
|
||||||
|
|
||||||
// transfer the pixels so they can be used
|
// transfer the pixels so they can be used
|
||||||
sapi::v::RemotePtr sapi_remote_out_ptr3(
|
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());
|
sapi_pixels.SetRemote(sapi_remote_out_ptr3.GetValue());
|
||||||
|
|
||||||
if (!sandbox.TransferFromSandboxee(&sapi_pixels).ok()) {
|
assert(sandbox.TransferFromSandboxee(&sapi_pixels).ok());
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
unsigned char *pixels_ptr = sapi_pixels.GetData();
|
unsigned char *pixels_ptr = sapi_pixels.GetData();
|
||||||
|
|
||||||
// compare values
|
// compare values
|
||||||
for (size_t i = 0; i < width * height * 4; ++i) {
|
for (size_t i = 0; i < width * height * 4; ++i) {
|
||||||
if (pixels_ptr[i] != image[i]) {
|
assert(pixels_ptr[i] == image[i]);
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
free(image);
|
free(image);
|
||||||
|
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
int main(int argc, char *argv[]) {
|
int main(int argc, char *argv[]) {
|
||||||
|
@ -430,17 +402,8 @@ int main(int argc, char *argv[]) {
|
||||||
|
|
||||||
LodepngApi api(&sandbox);
|
LodepngApi api(&sandbox);
|
||||||
|
|
||||||
if (generate_one_step(sandbox, api, images_path)) {
|
generate_one_step(sandbox, api, images_path);
|
||||||
std::cout << "first example ok" << std::endl;
|
generate_two_steps(sandbox, api, images_path);
|
||||||
} 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;
|
|
||||||
}
|
|
||||||
|
|
||||||
return EXIT_SUCCESS;
|
return EXIT_SUCCESS;
|
||||||
}
|
}
|
|
@ -141,7 +141,6 @@ TEST(generate_image, encode_decode_compare_two_step) {
|
||||||
sapi::v::IntBase<unsigned char *> sapi_png_ptr(0);
|
sapi::v::IntBase<unsigned char *> sapi_png_ptr(0);
|
||||||
|
|
||||||
// encode it into memory
|
// encode it into memory
|
||||||
|
|
||||||
sapi::StatusOr<unsigned int> result = api.lodepng_encode32(
|
sapi::StatusOr<unsigned int> result = api.lodepng_encode32(
|
||||||
sapi_png_ptr.PtrBoth(), sapi_pngsize.PtrBoth(), sapi_image.PtrBefore(),
|
sapi_png_ptr.PtrBoth(), sapi_pngsize.PtrBoth(), sapi_image.PtrBefore(),
|
||||||
sapi_width.GetValue(), sapi_height.GetValue());
|
sapi_width.GetValue(), sapi_height.GetValue());
|
||||||
|
@ -170,7 +169,6 @@ TEST(generate_image, encode_decode_compare_two_step) {
|
||||||
ASSERT_EQ(result.value(), 0);
|
ASSERT_EQ(result.value(), 0);
|
||||||
|
|
||||||
// now, decode the image using the 2 steps in order to compare the values
|
// now, decode the image using the 2 steps in order to compare the values
|
||||||
|
|
||||||
sapi::v::UInt sapi_width2, sapi_height2;
|
sapi::v::UInt sapi_width2, sapi_height2;
|
||||||
sapi::v::IntBase<unsigned char *> sapi_png_ptr2(0);
|
sapi::v::IntBase<unsigned char *> sapi_png_ptr2(0);
|
||||||
sapi::v::ULLong sapi_pngsize2;
|
sapi::v::ULLong sapi_pngsize2;
|
||||||
|
|
Loading…
Reference in New Issue
Block a user