/* * This does not literally comrpess the image * But it shows how the image would look like after compressing and decomressing it, useful * to see how different algorithms afect the image. */ #include #include #include #include extern "C" { #include } #include #include using namespace std; int main(){ av_register_all(); auto image = to_raw_rgb_image(crop_to_square(open_image("image.jpg")), 512, 512); std::vector vector(make_u(image.width() * image.height())); // for every color for(unsigned int i = 0; i < 3; ++i){ for(unsigned int n = 0; n < make_u(image.width() * image.height()); ++n){ vector[n] = 2.0 * image.data[3*n + i] / double(255) - 1.0; } wvlt::wavelet_2D(vector.data(), make_u(image.width()), make_u(image.height())); auto copy = vector; for(auto & x : copy) x = std::abs(x); unsigned int n_coefficients[] = {20, 20, 20}; std::nth_element(copy.begin(), copy.begin() + n_coefficients[i], copy.end(), std::greater()); for(auto & x : vector){ if(std::abs(x) < copy[n_coefficients[i]]) x = 0; } wvlt::unwavelet_2D(vector.data(), make_u(image.width()), make_u(image.height())); for(unsigned int n = 0; n < make_u(image.width() * image.height()); ++n){ image.data[3*n + i] = to_uint8_t(0.5 * vector[n] + 0.5); } } save_as_jpg(image.frame, "output.jpg"); }