1 /** -*- mode: c++ ; c-basic-offset: 2 -*-
2  *
3  *  @file ImageTools.cpp
4  *
5  *  Copyright 2017 Sebastien Fourey
6  *
7  *  This file is part of G'MIC-Qt, a generic plug-in for raster graphics
8  *  editors, offering hundreds of filters thanks to the underlying G'MIC
9  *  image processing framework.
10  *
11  *  gmic_qt is free software: you can redistribute it and/or modify
12  *  it under the terms of the GNU General Public License as published by
13  *  the Free Software Foundation, either version 3 of the License, or
14  *  (at your option) any later version.
15  *
16  *  gmic_qt is distributed in the hope that it will be useful,
17  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
18  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  *  GNU General Public License for more details.
20  *
21  *  You should have received a copy of the GNU General Public License
22  *  along with gmic_qt.  If not, see <http://www.gnu.org/licenses/>.
23  *
24  */
25 #include "ImageTools.h"
26 #include <QDebug>
27 #include <QImage>
28 #include <QPainter>
29 #include "GmicStdlib.h"
30 #include "gmic.h"
31 
32 /*
33  * Part of this code is much inspired by the original source code
34  * of the GTK version of the gmic plug-in for GIMP by David Tschumperl\'e.
35  */
36 
37 namespace GmicQt
38 {
39 
buildPreviewImage(const cimg_library::CImgList<float> & images,cimg_library::CImg<float> & result)40 void buildPreviewImage(const cimg_library::CImgList<float> & images, cimg_library::CImg<float> & result)
41 {
42   cimg_library::CImgList<gmic_pixel_type> preview_input_images;
43   if (images.size() > 0) {
44     preview_input_images.push_back(images[0]);
45     int spectrum = 0;
46     cimglist_for(preview_input_images, l) { spectrum = std::max(spectrum, preview_input_images[l].spectrum()); }
47     spectrum += (spectrum == 1 || spectrum == 3);
48     cimglist_for(preview_input_images, l) { calibrateImage(preview_input_images[l], spectrum, true); }
49     result.swap(preview_input_images.front());
50   } else {
51     result.assign();
52   }
53 }
54 
checkImageSpectrumAtMost4(const cimg_library::CImgList<float> & images,unsigned int & index)55 bool checkImageSpectrumAtMost4(const cimg_library::CImgList<float> & images, unsigned int & index)
56 {
57   for (unsigned int i = 0; i < images.size(); ++i) {
58     if (images[i].spectrum() > 4) {
59       index = i;
60       return false;
61     }
62   }
63   return true;
64 }
65 
hasAlphaChannel(const cimg_library::CImg<T> & image)66 template <typename T> bool hasAlphaChannel(const cimg_library::CImg<T> & image)
67 {
68   return image.spectrum() == 2 || image.spectrum() == 4;
69 }
70 
71 template bool hasAlphaChannel(const cimg_library::CImg<float> &);
72 template bool hasAlphaChannel(const cimg_library::CImg<unsigned char> &);
73 
74 } // namespace GmicQt
75