/** This file is a part of r0nk, atlas_moon, and 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 . */ #ifndef MATRIX_IMAGE_THUMBNAIL_MAKER_HPP #define MATRIX_IMAGE_THUMBNAIL_MAKER_HPP #include #include #include //size_t #include "matrix/upload_info.hpp" #define THUMBSIZE 500 namespace matrix::internal{ static std::vector _create_image_thumbnail(fipImage& image, FREE_IMAGE_FORMAT type, size_t target_size){ image.makeThumbnail(target_size); FreeImageIO fileout; std::vector buffer; fileout.write_proc = [](void* ptr, unsigned int size, unsigned int nmemb, void* fp) -> unsigned int{ std::vector& buffer = *reinterpret_cast*>(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(); case FIF_PNG: return image.saveToHandle(type, &fileout, &buffer, PNG_Z_BEST_COMPRESSION) ? buffer : std::vector(); default: return image.saveToHandle(type, &fileout, &buffer) ? buffer : std::vector(); }; } static std::vector _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 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