filters

kis_exif_io.cpp

00001 /*
00002  *  Copyright (c) 2006 Cyrille Berger <cberger@cberger.net>
00003  *
00004  *  This program is free software; you can redistribute it and/or modify
00005  *  it under the terms of the GNU General Public License as published by
00006  *  the Free Software Foundation; either version 2 of the License, or
00007  *  (at your option) any later version.
00008  *
00009  *  This program is distributed in the hope that it will be useful,
00010  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012  *  GNU General Public License for more details.
00013  *
00014  *  You should have received a copy of the GNU General Public License
00015  *  along with this program; if not, write to the Free Software
00016  *  Foundation, Inc., 51 Franklin Street, Fifth Floor,
00017  * Boston, MA 02110-1301, USA.
00018  */
00019 
00020 #include "kis_exif_io.h"
00021 
00022 #include <kdebug.h>
00023 
00024 extern "C" {
00025 #include <libexif/exif-tag.h>
00026 #include <libexif/exif-log.h>
00027 }
00028 
00029 KisExifIO::KisExifIO(KisExifInfo* ei) : m_exifInfo(ei)
00030 {
00031 }
00032 
00033 void KisExifIO::readExifFromFile( const char* fileName)
00034 {
00035     readExifData( exif_data_new_from_file(fileName) );
00036 }
00037 
00038 void KisExifIO::readExifFromMem( const unsigned char* data , unsigned int size)
00039 {
00040     readExifData( exif_data_new_from_data(data, size) );
00041 }
00042 
00043 void KisExifIO::saveExifToMem( unsigned char** data, unsigned int *size)
00044 {
00045     ExifData* exifData = exif_data_new();
00046     writeExifData( exifData );
00047     exif_data_save_data( exifData, data, size);
00048 }
00049 
00050 ExifValue::ExifType KisExifIO::format2type(ExifFormat format)
00051 {
00052     switch(format)
00053     {
00054         case EXIF_FORMAT_BYTE:
00055             return ExifValue::EXIF_TYPE_BYTE;
00056         case EXIF_FORMAT_ASCII:
00057             return ExifValue::EXIF_TYPE_ASCII;
00058         case EXIF_FORMAT_SHORT:
00059             return ExifValue::EXIF_TYPE_SHORT;
00060         case EXIF_FORMAT_LONG:
00061             return ExifValue::EXIF_TYPE_LONG;
00062         case EXIF_FORMAT_RATIONAL:
00063             return ExifValue::EXIF_TYPE_RATIONAL;
00064         case EXIF_FORMAT_SBYTE:
00065             return ExifValue::EXIF_TYPE_SBYTE;
00066         case EXIF_FORMAT_SSHORT:
00067             return ExifValue::EXIF_TYPE_SSHORT;
00068         case EXIF_FORMAT_SLONG:
00069             return ExifValue::EXIF_TYPE_SLONG;
00070         case EXIF_FORMAT_SRATIONAL:
00071             return ExifValue::EXIF_TYPE_SRATIONAL;
00072         case EXIF_FORMAT_FLOAT:
00073             return ExifValue::EXIF_TYPE_FLOAT;
00074         case EXIF_FORMAT_DOUBLE:
00075             return ExifValue::EXIF_TYPE_DOUBLE;
00076         default:
00077         case EXIF_FORMAT_UNDEFINED:
00078             return ExifValue::EXIF_TYPE_UNDEFINED;
00079     }
00080 }
00081 
00082 void KisExifIO::readExifData( ExifData* exifData)
00083 {
00084     ExifValue::ByteOrder bO;
00085     if(exif_data_get_byte_order( exifData) == EXIF_BYTE_ORDER_MOTOROLA)
00086     {
00087         bO = ExifValue::BYTE_ORDER_MOTOROLA;
00088     } else {
00089         bO = ExifValue::BYTE_ORDER_INTEL;
00090     }
00091     static ExifIfd ifds[5] = {
00092         EXIF_IFD_0,
00093         EXIF_IFD_1,
00094         EXIF_IFD_EXIF,
00095         EXIF_IFD_INTEROPERABILITY,
00096         EXIF_IFD_GPS
00097     };
00098     for(int ifd = 0; ifd < 5; ifd ++)
00099     {
00100         ExifContent* content = exifData->ifd[ifds[ifd]];
00101         kdDebug() << "There are " << content->count << " values in ifd=" << ifd << endl;
00102         for (uint i = 0; i < content->count; i++)
00103         {
00104             ExifEntry* entry = content->entries[i];
00105             QString tagname = exif_tag_get_name ( entry->tag );
00106 //             kdDebug() << "found tag : " << tagname << endl;
00107             //         QString tagname = exif_tag_get_name_in_ifd ( entry->tag, EXIF_IFD_0 ); TODO: would be better to rely on 0.6.13 when it becomes more common, as it supports better other IFD (GPS and interoperrabilibity)
00108             ExifValue value( format2type(entry->format), entry->data, entry->size, ifds[ifd], entry->components, bO );
00109 //             exif_entry_dump( entry, 4);
00110 //             kdDebug() << "value = " << value.toString() << endl;
00111             m_exifInfo->setValue( tagname, value);
00112         }
00113     }
00114     
00115 }
00116 
00117 ExifFormat KisExifIO::type2format( ExifValue::ExifType type)
00118 {
00119     switch(type)
00120     {
00121         case ExifValue::EXIF_TYPE_BYTE:
00122             return EXIF_FORMAT_BYTE;
00123         case ExifValue::EXIF_TYPE_ASCII:
00124             return EXIF_FORMAT_ASCII;
00125         case ExifValue::EXIF_TYPE_SHORT:
00126             return EXIF_FORMAT_SHORT;
00127         case ExifValue::EXIF_TYPE_LONG:
00128             return EXIF_FORMAT_LONG;
00129         case ExifValue::EXIF_TYPE_RATIONAL:
00130             return EXIF_FORMAT_RATIONAL;
00131         case ExifValue::EXIF_TYPE_SBYTE:
00132             return EXIF_FORMAT_SBYTE;
00133         case ExifValue::EXIF_TYPE_SSHORT:
00134             return EXIF_FORMAT_SSHORT;
00135         case ExifValue::EXIF_TYPE_SLONG:
00136             return EXIF_FORMAT_SLONG;
00137         case ExifValue::EXIF_TYPE_SRATIONAL:
00138             return EXIF_FORMAT_SRATIONAL;
00139         case ExifValue::EXIF_TYPE_FLOAT:
00140             return EXIF_FORMAT_FLOAT;
00141         case ExifValue::EXIF_TYPE_DOUBLE:
00142             return EXIF_FORMAT_DOUBLE;
00143         default:
00144         case ExifValue::EXIF_TYPE_UNDEFINED:
00145             return EXIF_FORMAT_UNDEFINED;
00146     }
00147 }
00148 
00149 
00150 void KisExifIO::writeExifData( ExifData* exifData)
00151 {
00152     ExifValue::ByteOrder bO;
00153     if(exif_data_get_byte_order( exifData) == EXIF_BYTE_ORDER_MOTOROLA)
00154     {
00155         bO = ExifValue::BYTE_ORDER_MOTOROLA;
00156     } else {
00157         bO = ExifValue::BYTE_ORDER_INTEL;
00158     }
00159         
00160     for( KisExifInfo::evMap::const_iterator it = m_exifInfo->begin(); it != m_exifInfo->end(); ++it)
00161     {
00162         ExifValue ev = it.data();
00163         if(ev.ifd() != -1)
00164         {
00165             ExifEntry * entry = exif_entry_new();
00166             ExifContent* content = exifData->ifd[ev.ifd()];
00167             exif_content_add_entry(content, entry);
00168             kdDebug() << "Saving tag:" << it.key() << " " << ev.toString() << endl;
00169             ExifTag tag = exif_tag_from_name( it.key().ascii());
00170             entry->components = ev.components();
00171             entry->format = type2format( ev.type());
00172             entry->tag = tag;
00173 //         exif_entry_dump(entry, 2);
00174             ev.convertToData(&entry->data, &entry->size, bO);
00175         }
00176     }
00177 }
KDE Home | KDE Accessibility Home | Description of Access Keys