File indexing completed on 2024-05-19 04:56:12

0001 /**
0002  * \file pictureframe.h
0003  * Frame containing picture.
0004  *
0005  * \b Project: Kid3
0006  * \author Urs Fleisch
0007  * \date 03 Mar 2008
0008  *
0009  * Copyright (C) 2008-2018  Urs Fleisch
0010  *
0011  * This file is part of Kid3.
0012  *
0013  * Kid3 is free software; you can redistribute it and/or modify
0014  * it under the terms of the GNU General Public License as published by
0015  * the Free Software Foundation; either version 2 of the License, or
0016  * (at your option) any later version.
0017  *
0018  * Kid3 is distributed in the hope that it will be useful,
0019  * but WITHOUT ANY WARRANTY; without even the implied warranty of
0020  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0021  * GNU General Public License for more details.
0022  *
0023  * You should have received a copy of the GNU General Public License
0024  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
0025  */
0026 
0027 #pragma once
0028 
0029 #include "frame.h"
0030 
0031 /** Frame containing picture. */
0032 class KID3_CORE_EXPORT PictureFrame : public Frame {
0033 public:
0034   /**
0035    * Additional properties for METADATA_BLOCK_PICTURE.
0036    */
0037   class KID3_CORE_EXPORT ImageProperties {
0038   public:
0039     /** Default constructor. */
0040     ImageProperties()
0041       : m_width(0), m_height(0), m_depth(0), m_numColors(0), m_imageHash(0) {}
0042 
0043     /**
0044      * Construct from properties in METADATA_BLOCK_PICTURE.
0045      * @param width width of picture in pixels
0046      * @param height height of picture in pixels
0047      * @param depth color depth of picture in bits-per-pixel
0048      * @param numColors number of colors used for indexed-color pictures
0049      *                  (e.g. GIF), or 0 for non-indexed pictures
0050      * @param data image data
0051      */
0052     ImageProperties(uint width, uint height, uint depth, uint numColors,
0053                     const QByteArray& data)
0054       : m_width(width), m_height(height), m_depth(depth), m_numColors(numColors),
0055         m_imageHash(qHash(data)) {}
0056 
0057     /**
0058      * Construct properties from a new image.
0059      * @param data image data
0060      */
0061     explicit ImageProperties(const QByteArray& data);
0062 
0063     /**
0064      * Check if the image properties are not set.
0065      * @return true if not set.
0066      */
0067     bool isNull() const {
0068       return m_width == 0 && m_height == 0 && m_depth == 0 &&
0069           m_numColors == 0 && m_imageHash == 0;
0070     }
0071 
0072     /**
0073      * Check if image properties are valid for an image.
0074      * @param data image data
0075      * @return true if valid.
0076      */
0077     bool isValidForImage(const QByteArray& data) const {
0078       return !isNull() && qHash(data) == m_imageHash;
0079     }
0080 
0081     /** Width of picture in pixels. */
0082     int width() const { return m_width; }
0083 
0084     /** Height of picture in pixels. */
0085     int height() const { return m_height; }
0086 
0087     /** Color depth of picture in bits-per-pixel.*/
0088     int depth() const { return m_depth; }
0089 
0090     /**
0091      * Number of colors used for indexed-color pictures (e.g. GIF),
0092      * or 0 for non-indexed pictures.
0093      */
0094     int numColors() const { return m_numColors; }
0095 
0096   private:
0097     bool loadFromData(const QByteArray& data);
0098 
0099     int m_width;
0100     int m_height;
0101     int m_depth;
0102     int m_numColors;
0103     uint m_imageHash;
0104   };
0105 
0106 
0107   /**
0108    * Constructor.
0109    *
0110    * @param data        binary picture data
0111    * @param description description
0112    * @param pictureType picture type
0113    * @param mimeType    MIME type
0114    * @param enc         text encoding
0115    * @param imgFormat   image format
0116    */
0117   explicit PictureFrame(
0118     const QByteArray& data = QByteArray(),
0119     const QString& description = QLatin1String(""),
0120     PictureType pictureType = PT_CoverFront,
0121     const QString& mimeType = QLatin1String("image/jpeg"),
0122     TextEncoding enc = TE_ISO8859_1,
0123     const QString& imgFormat = QLatin1String("JPG"));
0124 
0125   /**
0126    * Constructor.
0127    *
0128    * @param frame general frame
0129    */
0130   explicit PictureFrame(const Frame& frame);
0131 
0132   /**
0133    * Set all properties.
0134    *
0135    * @param frame       frame to set
0136    * @param enc         text encoding
0137    * @param imgFormat   image format
0138    * @param mimeType    MIME type
0139    * @param pictureType picture type
0140    * @param description description
0141    * @param data        binary picture data
0142    * @param imgProps    optional METADATA_BLOCK_PICTURE image properties
0143    */
0144   static void setFields(
0145     Frame& frame,
0146     TextEncoding enc = TE_ISO8859_1, const QString& imgFormat = QLatin1String("JPG"),
0147     const QString& mimeType = QLatin1String("image/jpeg"), PictureType pictureType = PT_CoverFront,
0148     const QString& description = QLatin1String(""), const QByteArray& data = QByteArray(),
0149     const ImageProperties* imgProps = nullptr);
0150 
0151   /**
0152    * Set all fields of a GEOB frame.
0153    *
0154    * @param frame       frame to set
0155    * @param enc         text encoding
0156    * @param mimeType    MIME type
0157    * @param fileName    file name
0158    * @param description description
0159    * @param data        binary data
0160    */
0161   static void setGeobFields(
0162       Frame& frame, TextEncoding enc = TE_ISO8859_1,
0163       const QString& mimeType = QLatin1String(""),
0164       const QString& fileName = QLatin1String(""),
0165       const QString& description = QLatin1String(""),
0166       const QByteArray& data = QByteArray());
0167 
0168   /**
0169    * Get all properties.
0170    * Unavailable fields are not set.
0171    *
0172    * @param frame       frame to get
0173    * @param enc         text encoding
0174    * @param imgFormat   image format
0175    * @param mimeType    MIME type
0176    * @param pictureType picture type
0177    * @param description description
0178    * @param data        binary picture data
0179    * @param imgProps    optional METADATA_BLOCK_PICTURE image properties
0180    */
0181   static void getFields(const Frame& frame,
0182                         TextEncoding& enc, QString& imgFormat,
0183                         QString& mimeType, PictureType& pictureType,
0184                         QString& description, QByteArray& data,
0185                         ImageProperties* imgProps = nullptr);
0186 
0187   /**
0188    * Check if all the fields of two picture frames are equal.
0189    * @param f1 first picture frame
0190    * @param f2 second picture frame
0191    * @return true if equal.
0192    */
0193   static bool areFieldsEqual(const Frame& f1, const Frame& f2);
0194 
0195   /**
0196    * Set text encoding.
0197    *
0198    * @param frame frame to set
0199    * @param enc   text encoding
0200    *
0201    * @return true if field found and set.
0202    */
0203   static bool setTextEncoding(Frame& frame, TextEncoding enc);
0204 
0205   /**
0206    * Get text encoding.
0207    *
0208    * @param frame frame to get
0209    * @param enc   the text encoding is returned here
0210    *
0211    * @return true if field found.
0212    */
0213   static bool getTextEncoding(const Frame& frame, TextEncoding& enc);
0214 
0215   /**
0216    * Set image format.
0217    *
0218    * @param frame     frame to set
0219    * @param imgFormat image format
0220    *
0221    * @return true if field found and set.
0222    */
0223   static bool setImageFormat(Frame& frame, const QString& imgFormat);
0224 
0225   /**
0226    * Get image format.
0227    *
0228    * @param frame     frame to get
0229    * @param imgFormat the image format is returned here
0230    *
0231    * @return true if field found.
0232    */
0233   static bool getImageFormat(const Frame& frame, QString& imgFormat);
0234 
0235   /**
0236    * Set MIME type.
0237    *
0238    * @param frame    frame to set
0239    * @param mimeType MIME type
0240    *
0241    * @return true if field found and set.
0242    */
0243   static bool setMimeType(Frame& frame, const QString& mimeType);
0244 
0245   /**
0246    * Get MIME type.
0247    *
0248    * @param frame    frame to get
0249    * @param mimeType the MIME type is returned here
0250    *
0251    * @return true if field found.
0252    */
0253   static bool getMimeType(const Frame& frame, QString& mimeType);
0254 
0255   /**
0256    * Set picture type.
0257    *
0258    * @param frame       frame to set
0259    * @param pictureType picture type
0260    *
0261    * @return true if field found and set.
0262    */
0263   static bool setPictureType(Frame& frame, PictureType pictureType);
0264 
0265   /**
0266    * Get picture type.
0267    *
0268    * @param frame       frame to get
0269    * @param pictureType the picture type is returned here
0270    *
0271    * @return true if field found.
0272    */
0273   static bool getPictureType(const Frame& frame, PictureType& pictureType);
0274 
0275   /**
0276    * Set description.
0277    *
0278    * @param frame       frame to set
0279    * @param description description
0280    *
0281    * @return true if field found and set.
0282    */
0283   static bool setDescription(Frame& frame, const QString& description);
0284 
0285   /**
0286    * Get description.
0287    *
0288    * @param frame       frame to get
0289    * @param description the description is returned here
0290    *
0291    * @return true if field found.
0292    */
0293   static bool getDescription(const Frame& frame, QString& description);
0294 
0295   /**
0296    * Set binary data.
0297    *
0298    * @param frame frame to set
0299    * @param data  binary data
0300    *
0301    * @return true if field found and set.
0302    */
0303   static bool setData(Frame& frame, const QByteArray& data);
0304 
0305   /**
0306    * Get binary data.
0307    *
0308    * @param frame frame to get
0309    * @param data  the binary data is returned here
0310    *
0311    * @return true if field found.
0312    */
0313   static bool getData(const Frame& frame, QByteArray& data);
0314 
0315   /**
0316    * Read binary data from file.
0317    *
0318    * @param frame frame to set
0319    * @param fileName name of data file
0320    *
0321    * @return true if file read, field found and set.
0322    */
0323   static bool setDataFromFile(Frame& frame, const QString& fileName);
0324 
0325   /**
0326    * Save binary data to a file.
0327    *
0328    * @param frame    frame
0329    * @param fileName name of data file to save
0330    *
0331    * @return true if field found and saved.
0332    */
0333   static bool writeDataToFile(const Frame& frame, const QString& fileName);
0334 
0335   /**
0336    * Get the MIME type and image format from a file.
0337    *
0338    * @param fileName name of data file
0339    * @param imgFormat if not null, the ID3v2.2 PIC image format ("JGP" or "PNG")
0340    * is set here
0341    *
0342    * @return mime type of file, null if not recognized.
0343    */
0344   static QString getMimeTypeForFile(const QString& fileName,
0345                                     QString* imgFormat = nullptr);
0346 
0347   /**
0348    * Set the MIME type and image format from a file.
0349    *
0350    * @param frame frame to set
0351    * @param fileName name of data file
0352    *
0353    * @return true if field found and set.
0354    */
0355   static bool setMimeTypeFromFileName(Frame& frame, const QString& fileName);
0356 
0357   /**
0358    * Set picture from a base64 string.
0359    *
0360    * @param frame       frame to set
0361    * @param base64Value base64 string
0362    */
0363   static void setFieldsFromBase64(Frame& frame, const QString& base64Value);
0364 
0365   /**
0366    * Get picture to a base64 string.
0367    *
0368    * @param frame       frame to get
0369    * @param base64Value base64 string to set
0370    */
0371   static void getFieldsToBase64(const Frame& frame, QString& base64Value);
0372 
0373   /**
0374    * Get a translated string for a picture type.
0375    *
0376    * @param type picture type
0377    *
0378    * @return picture type, null string if unknown.
0379    */
0380   static QString getPictureTypeName(PictureType type);
0381 
0382   /**
0383    * Get list of picture type strings.
0384    * @return list of picture type names, NULL terminated.
0385    */
0386   static const char* const* getPictureTypeNames();
0387 
0388   /**
0389    * Get an untranslated string for a picture type.
0390    *
0391    * @param type picture type
0392    *
0393    * @return picture type, 0 if unknown.
0394    */
0395   static const char* getPictureTypeString(PictureType type);
0396 
0397   /**
0398    * List of untranslated picture type strings, NULL terminated.
0399    */
0400   static const char* const* getPictureTypeStrings();
0401 
0402   /**
0403    * Get picture type from an untranslated string.
0404    *
0405    * @param str untranslated picture type string
0406    *
0407    * @return picture type, PT_Other if unknown.
0408    */
0409   static PictureType getPictureTypeFromString(const char* str);
0410 };
0411 
0412 Q_DECLARE_METATYPE(PictureFrame::ImageProperties)