Skip to content

Instantly share code, notes, and snippets.

@UnaNancyOwen
Last active November 6, 2019 09:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save UnaNancyOwen/4542c1a662befc39e9f41567011e73e3 to your computer and use it in GitHub Desktop.
Save UnaNancyOwen/4542c1a662befc39e9f41567011e73e3 to your computer and use it in GitHub Desktop.
PNG-like lossless image compression/decompression using Zpng
cmake_minimum_required( VERSION 3.6 )
# Language
enable_language( CXX )
# Compiler Settings
set( CMAKE_CXX_STANDARD 11 )
set( CMAKE_CXX_STANDARD_REQUIRED ON )
set( CMAKE_CXX_EXTENSIONS OFF )
# Project
project( zpng LANGUAGES CXX )
add_executable( zpng zpng.h zpng.hpp main.cpp )
# (Option) Start-Up Project for Visual Studio
set_property( DIRECTORY PROPERTY VS_STARTUP_PROJECT "zpng" )
# Find Package
find_package( OpenCV REQUIRED )
# Set Package to Project
if( OpenCV_FOUND )
target_link_libraries( zpng ${OpenCV_LIBS} )
target_link_libraries( zpng ${CMAKE_SOURCE_DIR}/zpnglib.lib )
endif()
#include <iostream>
#include <opencv2/opencv.hpp>
#include "zpng.hpp"
int main( int argc, char* argv[] )
{
cv::Mat src = cv::imread( "../image.jpg" );
if( src.empty() ){
return -1;
}
// Write cv::Mat to ZPNG
zpng::imwrite( "../image.zpng", src );
// Read cv::Mat from ZPNG
cv::Mat image = zpng::imread( "../image.zpng" );
cv::imshow( "image", image );
cv::waitKey( 0 );
return 0;
}
/*
This is utility wrapper to that provides image reader and writer for Zpng.
This utility provides functions to read and write Zpng image for OpenCV cv::Mat.
zpng::imwrite( "./image.zpng", mat );
cv::Mat mat = zpng::imread( "./image.zpng" );
Zpng is an experimental implementation of PNG-like lossless compression using Zstandard for compression and decompression.
It is faster and smaller than PNG.
https://github.com/catid/Zpng
Copyright (c) 2019 Tsukasa Sugiura <t.sugiura0204@gmail.com>
Licensed under the MIT license.
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#ifndef __ZPNG_HPP__
#define __ZPNG_HPP__
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <opencv2/opencv.hpp>
#include "zpng.h"
namespace zpng
{
cv::Mat imread( const std::string& file_name )
{
std::ifstream ifs( file_name, std::ios::binary );
std::vector<char> src( ( std::istreambuf_iterator<char>( ifs ) ), ( std::istreambuf_iterator<char>() ) );
ZPNG_Buffer buffer;
buffer.Data = reinterpret_cast<uint8_t*>( &src[0] );
buffer.Bytes = static_cast<uint32_t>( src.size() );
ZPNG_ImageData image_data = ZPNG_Decompress( buffer );
const int32_t width = image_data.WidthPixels;
const int32_t height = image_data.HeightPixels;
const int32_t type = CV_MAKETYPE( ( image_data.BytesPerChannel == 1 ? CV_8U : CV_16U ), image_data.Channels );
cv::Mat mat = cv::Mat( height, width, type, image_data.Buffer.Data ).clone();
ZPNG_Free( &image_data.Buffer );
return mat;
}
bool imwrite( const std::string& file_name, cv::Mat image )
{
try{
ZPNG_ImageData image_data;
image_data.Buffer.Data = image.data;
image_data.Buffer.Bytes = static_cast<uint32_t>( image.total() * image.elemSize() );
image_data.BytesPerChannel = static_cast<uint32_t>( image.elemSize1() );
image_data.Channels = image.channels();
image_data.HeightPixels = image.rows;
image_data.WidthPixels = image.cols;
image_data.StrideBytes = static_cast<uint32_t>( image.step );
ZPNG_Buffer buffer = ZPNG_Compress( &image_data );
std::ofstream ofs( file_name, std::ios::binary );
ofs.write( reinterpret_cast<char*>( buffer.Data ), buffer.Bytes );
ofs.close();
ZPNG_Free( &buffer );
}
catch( const std::ios::failure& e ){
std::cerr << e.what() << std::endl;
return false;
}
return true;
}
}
#endif // __ZPNG_HPP__
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment