matrix_thing/include/matrix/image_thumbnail_maker.hpp
2019-09-02 12:03:48 -07:00

74 lines
2.5 KiB
C++

/**
This file is a part of rexy's matrix client
Copyright (C) 2019 rexy712
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef MATRIX_IMAGE_THUMBNAIL_MAKER_HPP
#define MATRIX_IMAGE_THUMBNAIL_MAKER_HPP
#include <FreeImagePlus.h>
#include <vector>
#include <cstdlib> //size_t
#include "matrix/upload_info.hpp"
#define THUMBSIZE 500
namespace matrix::internal{
static std::vector<char> _create_image_thumbnail(fipImage& image, FREE_IMAGE_FORMAT type, size_t target_size){
image.makeThumbnail(target_size);
FreeImageIO fileout;
std::vector<char> buffer;
fileout.write_proc = [](void* ptr, unsigned int size, unsigned int nmemb, void* fp) -> unsigned int{
std::vector<char>& buffer = *reinterpret_cast<std::vector<char>*>(fp);
buffer.insert(buffer.end(), (char*)ptr, ((char*)ptr)+size*nmemb);
return size*nmemb;
};
switch(type){
case FIF_JPEG:
return image.saveToHandle(type, &fileout, &buffer, JPEG_QUALITYGOOD | JPEG_SUBSAMPLING_411) ? buffer : std::vector<char>();
case FIF_PNG:
return image.saveToHandle(type, &fileout, &buffer, PNG_Z_BEST_COMPRESSION) ? buffer : std::vector<char>();
default:
return image.saveToHandle(type, &fileout, &buffer) ? buffer : std::vector<char>();
};
}
static std::vector<char> _load_image_thumbnail_info(fipImage& image, image_info& info, FREE_IMAGE_FORMAT type){
//create and upload thumbnail
if(info.width > THUMB_SIZE || info.height > THUMB_SIZE){
std::vector<char> thumb_data = _create_image_thumbnail(image, type, THUMB_SIZE);
if(!thumb_data.size()){
info.thumb_width = info.width;
info.thumb_height = info.height;
info.thumbsize = info.filesize;
}else{
info.thumb_width = image.getWidth();
info.thumb_height = image.getHeight();
info.thumbsize = thumb_data.size();
return thumb_data;
}
}else{
info.thumb_width = info.width;
info.thumb_height = info.height;
info.thumbsize = info.filesize;
}
return {};
}
}
#endif