MythTV  0.26-pre
dcrawhandler.cpp
Go to the documentation of this file.
00001 #include "dcrawhandler.h"
00002 
00003 #include "config.h"
00004 
00005 #include <cstddef>
00006 
00007 #include <QByteArray>
00008 #include <QFile>
00009 #include <QImage>
00010 #include <QIODevice>
00011 #include <QString>
00012 
00013 #include "mythsystem.h"
00014 #include "exitcodes.h"
00015 #include "../mythgallery/galleryutil.h"
00016 #include "mythlogging.h"
00017 
00018 namespace
00019 {
00020 
00021 bool getPath(QIODevice *device, QString &path)
00022 {
00023     QFile *file = qobject_cast<QFile *>(device);
00024     if (!file)
00025         return false;
00026     path = file->fileName();
00027     return true;
00028 }
00029 
00030 }    // anonymous namespace
00031 
00032 bool DcrawHandler::canRead() const
00033 {
00034     QString path;
00035     bool isFile = getPath(device(), path);
00036     if (!isFile)
00037         // It would still be possible to process this file,
00038         // but piping the image data to dcraw would be
00039         // difficult. This code path wouldn't be exercised in
00040         // MythGallery anyway. So for simplicity we give up.
00041         return false;
00042 
00043     QString command = "dcraw -i " + path;
00044     return (myth_system(command) == GENERIC_EXIT_OK);
00045 }
00046 
00047 bool DcrawHandler::read(QImage *image)
00048 {
00049     QString path;
00050     bool isFile = getPath(device(), path);
00051     if (!isFile)
00052         // It would still be possible to process this file,
00053         // but piping the image data to dcraw would be
00054         // difficult. This code path wouldn't be exercised in
00055         // MythGallery anyway. So for simplicity we give up.
00056         return false;
00057 
00058     path = "'" + path + "'";
00059     QStringList arguments;
00060     arguments << "-c" << "-w" << "-W";
00061 #ifdef ICC_PROFILE
00062     arguments << "-p" << ICC_PROFILE;
00063 #endif // ICC_PROFILE
00064     arguments << path;
00065 
00066     uint flags = kMSRunShell | kMSStdOut | kMSBuffered;
00067     MythSystem ms("dcraw", arguments, flags);
00068     ms.Run();
00069     if (ms.Wait() != GENERIC_EXIT_OK)
00070         return false;
00071 
00072     QByteArray buffer = ms.ReadAll();
00073     if (buffer.isEmpty())
00074         return false;
00075 
00076     bool loaded = image->loadFromData(buffer, "PPM");
00077     return loaded;
00078 }
00079 
00080 int DcrawHandler::loadThumbnail(QImage *image, QString fileName)
00081 {
00082     QStringList arguments;
00083     arguments << "-e" << "-c";
00084     arguments << "'" + fileName + "'";
00085 
00086     uint flags = kMSRunShell | kMSStdOut | kMSBuffered;
00087     MythSystem ms("dcraw", arguments, flags);
00088     ms.Run();
00089     if (ms.Wait() != GENERIC_EXIT_OK)
00090         return -1;
00091 
00092     QByteArray buffer = ms.ReadAll();
00093     if (buffer.isEmpty())
00094         return -1;
00095 
00096     if (!image->loadFromData(buffer, "JPG"))
00097         return -1;
00098 
00099     int rotateAngle = 0;
00100 
00101 #ifdef EXIF_SUPPORT
00102     const unsigned char *buf = (const unsigned char *)buffer.constData();
00103     int size = buffer.size();
00104     rotateAngle = GalleryUtil::GetNaturalRotation(buf, size);
00105 #endif
00106 
00107     return rotateAngle;
00108 }
00109 
00110 /*
00111  * vim:ts=4:sw=4:ai:et:si:sts=4
00112  */
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends