changed style and added status checks for sapi calls

This commit is contained in:
Andrei Medar 2020-08-07 12:25:15 +00:00
parent d63ada79cf
commit 6ef819133e
2 changed files with 113 additions and 87 deletions

View File

@ -12,32 +12,36 @@
// See the License for the specific language governing permissions and // See the License for the specific language governing permissions and
// limitations under the License. // limitations under the License.
#include "lodepng-master/lodepng.h"
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <iostream> #include <iostream>
#include "lodepng-master/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()) << " -> " << basename(f2.c_str()) << std::endl; std::cout << "COMPARING IMAGES " << basename(f1.c_str()) << " -> "
<< 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::endl; std::cerr << "error " << error << ": " << lodepng_error_text(error)
<< 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::endl; std::cerr << "error " << error << ": " << lodepng_error_text(error)
<< std::endl;
return false; return false;
} }
@ -56,29 +60,30 @@ bool cmp_images32(const std::string &f1, const std::string &f2) {
} }
// copies an image into another and compares them // copies an image into another and compares them
void decode_and_encode32(const std::string &filename1, const std::string &filename2) { void decode_and_encode32(const std::string &filename1,
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::endl; std::cerr << "error " << error << ": " << lodepng_error_text(error)
<< 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::endl; std::cerr << "error " << error << ": " << lodepng_error_text(error)
<< 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;
@ -86,7 +91,6 @@ void test1(const std::string &images_path) {
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);
@ -101,12 +105,9 @@ void test1(const std::string &images_path) {
} else { } else {
std::cout << "files are not different" << std::endl; std::cout << "files are not different" << std::endl;
} }
} }
int main(int argc, char *argv[]) { int main(int argc, char *argv[]) {
if (argc != 2) { if (argc != 2) {
std::cout << "usage: " << basename(argv[0]) << " images_folder_path" std::cout << "usage: " << basename(argv[0]) << " images_folder_path"
<< std::endl; << std::endl;

View File

@ -31,14 +31,18 @@ void decode_and_encode32(SapiLodepngSandbox &sandbox, LodepngApi &api,
const std::string &f1, const std::string &f2) { const std::string &f1, const std::string &f2) {
sapi::v::UInt width(0), height(0); sapi::v::UInt width(0), height(0);
sapi::v::ConstCStr filename1(f1.c_str()), filename2(f2.c_str()); 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 // in order to pass unsigned char ** to the function, we pass a variable that
// contains the pointer. // contains the pointer.
sapi::v::IntBase<unsigned char *> image(0); sapi::v::IntBase<unsigned char *> image(0);
api.lodepng_decode32_file(image.PtrBoth(), width.PtrBoth(), height.PtrBoth(), if (!api.lodepng_decode32_file(image.PtrBoth(), width.PtrBoth(),
filename1.PtrBefore()) height.PtrBoth(), filename1.PtrBefore())
.IgnoreError(); .ok()) {
std::cerr << "decode failed" << std::endl;
exit(1);
}
// after the function is called, we need to access the data stored at the // 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 // address to which the previous variable points. To do that, we have to
@ -46,16 +50,23 @@ void decode_and_encode32(SapiLodepngSandbox &sandbox, LodepngApi &api,
sapi::v::RemotePtr remote_out_ptr(reinterpret_cast<void *>(image.GetValue())); sapi::v::RemotePtr remote_out_ptr(reinterpret_cast<void *>(image.GetValue()));
sapi::v::Array<char> out_img(width.GetValue() * height.GetValue()); sapi::v::Array<char> out_img(width.GetValue() * height.GetValue());
out_img.SetRemote(remote_out_ptr.GetValue()); out_img.SetRemote(remote_out_ptr.GetValue());
sandbox.TransferFromSandboxee(&out_img).IgnoreError();
if (!sandbox.TransferFromSandboxee(&out_img).ok()) {
std::cerr << "Transfer From Sandboxee failed" << std::endl;
exit(1);
}
// now the pixels are available at out_img.GetData() // now the pixels are available at out_img.GetData()
// when calling the encoding function, we need only an unsigned char * // when calling the encoding function, we need only an unsigned char *
// (instead of **) so we can simply use the sapi::v::Array defined before // (instead of **) so we can simply use the sapi::v::Array defined before
// (PtrBefore will give us a pointer). // (PtrBefore will give us a pointer).
api.lodepng_encode32_file(filename2.PtrBefore(), out_img.PtrBefore(), if (!api.lodepng_encode32_file(filename2.PtrBefore(), out_img.PtrBefore(),
width.GetValue(), height.GetValue()) width.GetValue(), height.GetValue())
.IgnoreError(); .ok()) {
std::cerr << "encode failed" << std::endl;
exit(1);
}
// Since in this function we do not actually used the pixels, we could simply // 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, // call the encode function with the pointer from before (the remote pointer,
@ -73,26 +84,41 @@ bool cmp_images32(SapiLodepngSandbox &sandbox, LodepngApi &api,
sapi::v::UInt width1, height1, width2, height2; sapi::v::UInt width1, height1, width2, height2;
sapi::v::ConstCStr filename1(f1.c_str()), filename2(f2.c_str()); sapi::v::ConstCStr filename1(f1.c_str()), filename2(f2.c_str());
sapi::v::IntBase<unsigned char *> image1_ptr(0), image2_ptr(0); sapi::v::IntBase<unsigned char *> image1_ptr(0), image2_ptr(0);
// absl::Status ret;
api.lodepng_decode32_file(image1_ptr.PtrBoth(), width1.PtrBoth(), if (!api.lodepng_decode32_file(image1_ptr.PtrBoth(), width1.PtrBoth(),
height1.PtrBoth(), filename1.PtrBefore()) height1.PtrBoth(), filename1.PtrBefore())
.IgnoreError(); .ok()) {
std::cerr << "decode failed" << std::endl;
exit(1);
}
api.lodepng_decode32_file(image2_ptr.PtrBoth(), width2.PtrBoth(), if (!api.lodepng_decode32_file(image2_ptr.PtrBoth(), width2.PtrBoth(),
height2.PtrBoth(), filename2.PtrBefore()) height2.PtrBoth(), filename2.PtrBefore())
.IgnoreError(); .ok()) {
std::cerr << "decode failed" << std::endl;
exit(1);
}
sapi::v::RemotePtr remote_out_ptr1( sapi::v::RemotePtr remote_out_ptr1(
reinterpret_cast<void *>(image1_ptr.GetValue())); reinterpret_cast<void *>(image1_ptr.GetValue()));
sapi::v::Array<char> pixels1(width1.GetValue() * height1.GetValue()); sapi::v::Array<char> pixels1(width1.GetValue() * height1.GetValue());
pixels1.SetRemote(remote_out_ptr1.GetValue()); pixels1.SetRemote(remote_out_ptr1.GetValue());
sandbox.TransferFromSandboxee(&pixels1).IgnoreError();
if (!sandbox.TransferFromSandboxee(&pixels1).ok()) {
std::cerr << "Transfer From Sandboxee failed" << std::endl;
exit(1);
}
sapi::v::RemotePtr remote_out_ptr2( sapi::v::RemotePtr remote_out_ptr2(
reinterpret_cast<void *>(image2_ptr.GetValue())); reinterpret_cast<void *>(image2_ptr.GetValue()));
sapi::v::Array<char> pixels2(width2.GetValue() * height2.GetValue()); sapi::v::Array<char> pixels2(width2.GetValue() * height2.GetValue());
pixels2.SetRemote(remote_out_ptr2.GetValue()); pixels2.SetRemote(remote_out_ptr2.GetValue());
sandbox.TransferFromSandboxee(&pixels2).IgnoreError();
if (!sandbox.TransferFromSandboxee(&pixels2).ok()) {
std::cerr << "Transfer From Sandboxee failed" << std::endl;
exit(1);
}
if (width1.GetValue() != width2.GetValue() || if (width1.GetValue() != width2.GetValue() ||
height1.GetValue() != height2.GetValue()) { height1.GetValue() != height2.GetValue()) {
@ -120,13 +146,6 @@ void test1(SapiLodepngSandbox &sandbox, LodepngApi &api,
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";
// const char *filename1 =
// "/usr/local/google/home/amedar/sapi_lodepng/test_images/test1.png"; const
// char *filename2 =
// "/usr/local/google/home/amedar/sapi_lodepng/test_images/out/test1_1out.png";
// const char *filename3 =
// "/usr/local/google/home/amedar/sapi_lodepng/test_images/out/test1_2out.png";
decode_and_encode32(sandbox, api, filename1, filename2); decode_and_encode32(sandbox, api, filename1, filename2);
decode_and_encode32(sandbox, api, filename1, filename3); decode_and_encode32(sandbox, api, filename1, filename3);
@ -146,6 +165,7 @@ void test1(SapiLodepngSandbox &sandbox, LodepngApi &api,
// at the moment there are no arguments when running // at the moment there are no arguments when running
int main(int argc, char *argv[]) { int main(int argc, char *argv[]) {
gflags::ParseCommandLineFlags(&argc, &argv, true); gflags::ParseCommandLineFlags(&argc, &argv, true);
absl::Status ret;
if (argc != 2) { if (argc != 2) {
std::cout << "usage: " << basename(argv[0]) << " images_folder_path" std::cout << "usage: " << basename(argv[0]) << " images_folder_path"
@ -156,7 +176,12 @@ int main(int argc, char *argv[]) {
std::string images_path(argv[1]); std::string images_path(argv[1]);
SapiLodepngSandbox sandbox(images_path); SapiLodepngSandbox sandbox(images_path);
sandbox.Init().IgnoreError(); ret = sandbox.Init();
if (!ret.ok()) {
std::cerr << "error code: " << ret.code() << std::endl
<< "message: " << ret.message() << std::endl;
exit(1);
}
LodepngApi api(&sandbox); LodepngApi api(&sandbox);