File indexing completed on 2024-04-21 15:12:10

0001 /* This file is part of the KDE Project
0002    Copyright (C) 2002 Klaas Freitag <freitag@suse.de>
0003 
0004    This library is free software; you can redistribute it and/or
0005    modify it under the terms of the GNU Library General Public
0006    License as published by the Free Software Foundation; either
0007    version 2 of the License, or (at your option) any later version.
0008 
0009    This library is distributed in the hope that it will be useful,
0010    but WITHOUT ANY WARRANTY; without even the implied warranty of
0011    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0012    Library General Public License for more details.
0013 
0014    You should have received a copy of the GNU Library General Public License
0015    along with this library; see the file COPYING.LIB.  If not, write to
0016    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
0017    Boston, MA 02110-1301, USA.
0018 */
0019 
0020 #ifndef KGAMMATABLE_H
0021 #define KGAMMATABLE_H
0022 
0023 #include "kookascan_export.h"
0024 
0025 #include <qobject.h>
0026 #include <qvector.h>
0027 
0028 /**
0029  * @short A gamma table.
0030  *
0031  * The gamma table maps input intensity values to output values.
0032  * The transfer function is defined by three parameters gamma, brightness
0033  * and contrast.  These values are retained internally.
0034  *
0035  * When the data from the table is required, either for display or for
0036  * sending to the scanner, it is calculated and made available as a vector
0037  * of integers.  Each of these values is in the range 0..(valueRange-1).
0038  *
0039  * The @c gamma values specified and returned are expressed as a
0040  * percentage, so 100 is a linear transfer function.  Reasonable values
0041  * for the gamma are within the range 30..300 (corresponding to
0042  * "conventional" gamma values of 0.3..3.0).
0043  *
0044  * Reasonable values for the @c brightness and @c contrast are within
0045  * the range -50..+50.
0046  *
0047  * @author Klaas Freitag
0048  * @author Jonathan Marten
0049  **/
0050 
0051 class KOOKASCAN_EXPORT KGammaTable : public QObject
0052 {
0053     Q_OBJECT
0054 
0055     Q_PROPERTY(int g READ getGamma WRITE setGamma)
0056     Q_PROPERTY(int c READ getContrast WRITE setContrast)
0057     Q_PROPERTY(int b READ getBrightness WRITE setBrightness)
0058 
0059 public:
0060     /**
0061      * Constructor.
0062      *
0063      * Create a new gamma table object, with default parameters or
0064      * as specified.
0065      *
0066      * @param gamma Initial gamma value
0067      * @param brightness Initial brightness value
0068      * @param contrast Initial contrast value
0069      *
0070      * @note The data array is not allocated at this point.
0071      **/
0072     explicit KGammaTable(int gamma = 100, int brightness = 0, int contrast = 0);
0073 
0074     /**
0075      * Copy constructor.
0076      *
0077      * @param other Gamma table to be copied
0078      *
0079      * @note Only the gamma/brightness/contrast parameters are copied,
0080      * the data array is neither copied nor allocated.
0081      **/
0082     KGammaTable(const KGammaTable &other);
0083 
0084     /**
0085      * Set all of the gamma table parameters in one operation.
0086      *
0087      * @param gamma New gamma value
0088      * @param brightness New brightness value
0089      * @param contrast New contrast value
0090      **/
0091     void setAll(int gamma, int brightness, int contrast);
0092 
0093     /**
0094      * Set the gamma table parameters from a string representation.
0095      *
0096      * @param str String value, in the form "gamma,brightness,contrast"
0097      * where each parameter is a decimal integer.
0098      *
0099      * @return @c true if the string format is valid.  If it is not
0100      * valid, then @c false is returned and the gamma table parameters
0101      * will not have been changed.
0102      *
0103      * @see toString
0104      **/
0105     bool setFromString(const QString &str);
0106 
0107     /**
0108      * Convert the gamma table parameters to a string representation.
0109      *
0110      * @return The string representation, in the form "gamma,brightness,contrast".
0111      *
0112      * @see setFromString
0113      **/
0114     QString toString() const;
0115 
0116     /**
0117      * Get the current gamma value.
0118      *
0119      * @return The gamma value
0120      **/
0121     int getGamma() const
0122     {
0123         return (mGamma);
0124     }
0125 
0126     /**
0127      * Get the current brightness value.
0128      *
0129      * @return The brightness value
0130      **/
0131     int getBrightness() const
0132     {
0133         return (mBrightness);
0134     }
0135 
0136     /**
0137      * Get the current contrast value.
0138      *
0139      * @return The contrast value
0140      **/
0141     int getContrast() const
0142     {
0143         return (mContrast);
0144     }
0145 
0146     /**
0147      * Get the currently allocated table size.
0148      *
0149      * @return The number of entries in the gamma table.  If the table
0150      * has not yet been calculated (i.e. @c getTable() has never been
0151      * called) then the size returned will be zero.  Otherwise, the size
0152      * returned is the last size requested for @c getTable(), or 256
0153      * if no explicit size has been requested.
0154      *
0155      * @see getTable
0156      **/
0157     int tableSize() const
0158     {
0159         return (mData.size());
0160     }
0161 
0162     /**
0163      * Calculate the gamma table values.
0164      *
0165      * @param size Size of the table required.  If not specified, then
0166      * the previous size requested for the table is retained.  If no
0167      * explicit size has ever been requested, then 256 is assumed.
0168      *
0169      * @return A pointer to the array of values
0170      **/
0171     const int *getTable(int size = -1);
0172 
0173     /**
0174      * The range of values that will be found in the table.
0175      **/
0176     static const int valueRange = 256;
0177 
0178 public slots:
0179     /**
0180      * Set a new brightness value.
0181      *
0182      * @param brightness New brightness value
0183      **/
0184     void setBrightness(int brightness);
0185 
0186     /**
0187      * Set a new contrast value.
0188      *
0189      * @param contrast New contrast value
0190      **/
0191     void setContrast(int contrast);
0192 
0193     /**
0194      * Set a new gamma value.
0195      *
0196      * @param gamma New gamma value
0197      **/
0198     void setGamma(int gamma);
0199 
0200 signals:
0201     /**
0202      * Emitted when any of the table parameters (gamma, brightness or
0203      * contrast) have changed.
0204      **/
0205     void tableChanged();
0206 
0207 private:
0208     void init();
0209     void calcTable();
0210 
0211     int mGamma;
0212     int mBrightness;
0213     int mContrast;
0214     bool mDirty;
0215 
0216     QVector<int> mData;
0217 };
0218 
0219 #endif                          // KGAMMATABLE_H