sandboxed-api/oss-internship-2020/sapi_lodepng/main.cc

152 lines
4.3 KiB
C++
Raw Normal View History

2020-08-07 19:01:41 +08:00
// Copyright 2020 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include <stdio.h>
#include <stdlib.h>
2020-08-07 19:01:41 +08:00
#include <iostream>
#include "lodepng-master/lodepng.h"
2020-08-07 19:01:41 +08:00
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;
2020-08-07 19:01:41 +08:00
unsigned int error, width1, height1;
unsigned char *image1 = 0;
2020-08-07 19:01:41 +08:00
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;
}
2020-08-07 19:01:41 +08:00
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;
2020-08-07 19:01:41 +08:00
}
}
return true;
2020-08-07 19:01:41 +08:00
}
// 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;
2020-08-07 19:01:41 +08:00
error = lodepng_decode32_file(&image, &width, &height, filename1.c_str());
2020-08-07 19:01:41 +08:00
if (error) {
std::cerr << "error " << error << ": " << lodepng_error_text(error)
<< std::endl;
return;
}
2020-08-07 19:01:41 +08:00
error = lodepng_encode32_file(filename2.c_str(), image, width, height);
2020-08-07 19:01:41 +08:00
if (error) {
std::cerr << "error " << error << ": " << lodepng_error_text(error)
<< std::endl;
return;
}
2020-08-07 19:01:41 +08:00
free(image);
}
2020-08-07 19:01:41 +08:00
void test1(const std::string &images_path) {
std::cout << "test1" << std::endl;
2020-08-07 19:01:41 +08:00
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";
2020-08-07 19:01:41 +08:00
decode_and_encode32(filename1, filename2);
decode_and_encode32(filename1, filename3);
2020-08-07 19:01:41 +08:00
if (!cmp_images32(filename1, filename2)) {
2020-08-07 19:01:41 +08:00
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);
}
2020-08-07 19:01:41 +08:00
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(argv[1]);
test1(images_path);
test2();
return 0;
2020-08-07 19:01:41 +08:00
}