Move kimageformats code to the root directory.

This commit is contained in:
Jenkins CI
2013-12-18 00:45:18 +00:00
parent 29f9dc7e4f
commit 47df9483fd
67 changed files with 0 additions and 0 deletions

3
src/CMakeLists.txt Normal file
View File

@ -0,0 +1,3 @@
# This is a subdirectory so that tests work before installation
# (QtGui expects imageformat plugins to be in a directory called imageformats)
add_subdirectory(imageformats)

14
src/imageformats/AUTHORS Normal file
View File

@ -0,0 +1,14 @@
Sirtaj Singh Kang <taj@kde.org> -- kimgio and jpeg, tiff, png, krl readers
Dirk Schoenberger <> -- eps, netpbm readers
Torben Weis <weis@kde.org> -- XV format reader/writer
Thomas Tanghus <tanghus@kde.org> -- PNG writer
Antonio Larossa <larossa@kde.org> -- initial version of KRL reader
Sven Wiegand <SWiegand@tfh-berlin.de> -- eps output filter (from KSnapshot)
Dominik Seichter <domseichter@web.de> -- TGA format read/write support
Nadeem Hasan <nhasan@kde.org> -- PCX format read/write support
Melchior Franz <mfranz@kde.org> -- SGI format read/write support, port of XCF qimgio
Allen Barnett <allen@lignumcomputing.com> -- XCF format read support (qimgio)
Ignacio Castaño <castano@ludicon.com> -- DDS and PDS format reader.
Christoph Hormann <chris_hormann@gmx.de> -- HDR format read support.
Michael Ritzert <kde@ritzert.de> -- JPEG 2000 format read/write support
Troy Unrau <troy@kde.org> -- Sun RASter read support

View File

@ -0,0 +1,161 @@
# NB: the desktop files are installed for the benefit of KImageIO in KDE4Support.
find_package(Jasper)
set_package_properties(Jasper PROPERTIES DESCRIPTION "Support for JPEG-2000 images"
URL "http://www.ece.uvic.ca/~mdadams/jasper"
TYPE OPTIONAL
)
find_package(OpenEXR)
set_package_properties(OpenEXR PROPERTIES
TYPE OPTIONAL
PURPOSE "QImage plugin for OpenEXR images"
)
set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${KDE_ENABLE_EXCEPTIONS}")
configure_file(config-kimgio.h.cmake ${CMAKE_CURRENT_BINARY_DIR}/config-kimgio.h )
check_include_files(sys/types.h HAVE_SYS_TYPES_H)
check_include_files(stdint.h HAVE_STDINT_H)
if(JASPER_FOUND)
include_directories( ${JASPER_INCLUDE_DIR} )
set(kimg_jp2_LIB_SRCS jp2.cpp)
add_library(kimg_jp2 MODULE ${kimg_jp2_LIB_SRCS})
set_target_properties(kimg_jp2 PROPERTIES PREFIX "")
target_link_libraries(kimg_jp2 Qt5::Gui ${JASPER_LIBRARIES} )
install(TARGETS kimg_jp2 DESTINATION ${QT_PLUGIN_INSTALL_DIR}/imageformats/ )
install(FILES jp2.desktop DESTINATION ${SERVICES_INSTALL_DIR}/qimageioplugins/ )
endif()
##################################
# EPS support depends on the gs utility; non-UNIX systems are unlikely to have
# this available in PATH
if (UNIX)
find_package(Qt5PrintSupport 5.2.0 NO_MODULE)
set_package_properties(Qt5PrintSupport PROPERTIES
DESCRIPTION "Support for Qt printing, necessary for the EPS format"
TYPE OPTIONAL
)
if (Qt5PrintSupport_FOUND)
set(kimg_eps_LIB_SRCS eps.cpp)
add_library(kimg_eps MODULE ${kimg_eps_LIB_SRCS})
set_target_properties(kimg_eps PROPERTIES PREFIX "")
target_link_libraries(kimg_eps Qt5::Gui Qt5::PrintSupport)
install(TARGETS kimg_eps DESTINATION ${QT_PLUGIN_INSTALL_DIR}/imageformats/)
install(FILES eps.desktop DESTINATION ${SERVICES_INSTALL_DIR}/qimageioplugins/)
endif()
endif()
##################################
set(kimg_pcx_LIB_SRCS pcx.cpp)
add_library(kimg_pcx MODULE ${kimg_pcx_LIB_SRCS})
set_target_properties(kimg_pcx PROPERTIES PREFIX "")
target_link_libraries(kimg_pcx Qt5::Gui)
install(TARGETS kimg_pcx DESTINATION ${QT_PLUGIN_INSTALL_DIR}/imageformats/ )
install(FILES pcx.desktop DESTINATION ${SERVICES_INSTALL_DIR}/qimageioplugins/ )
##################################
set(kimg_tga_LIB_SRCS tga.cpp)
add_library(kimg_tga MODULE ${kimg_tga_LIB_SRCS})
set_target_properties(kimg_tga PROPERTIES PREFIX "")
target_link_libraries(kimg_tga Qt5::Gui)
install(TARGETS kimg_tga DESTINATION ${QT_PLUGIN_INSTALL_DIR}/imageformats/ )
install(FILES tga.desktop DESTINATION ${SERVICES_INSTALL_DIR}/qimageioplugins/ )
##################################
set(kimg_ras_LIB_SRCS ras.cpp)
add_library(kimg_ras MODULE ${kimg_ras_LIB_SRCS})
set_target_properties(kimg_ras PROPERTIES PREFIX "")
target_link_libraries(kimg_ras Qt5::Gui)
install(TARGETS kimg_ras DESTINATION ${QT_PLUGIN_INSTALL_DIR}/imageformats/ )
install(FILES ras.desktop DESTINATION ${SERVICES_INSTALL_DIR}/qimageioplugins/ )
##################################
set(kimg_rgb_LIB_SRCS rgb.cpp)
add_library(kimg_rgb MODULE ${kimg_rgb_LIB_SRCS})
set_target_properties(kimg_rgb PROPERTIES PREFIX "")
target_link_libraries(kimg_rgb Qt5::Gui)
install(TARGETS kimg_rgb DESTINATION ${QT_PLUGIN_INSTALL_DIR}/imageformats/ )
install(FILES rgb.desktop DESTINATION ${SERVICES_INSTALL_DIR}/qimageioplugins/ )
##################################
set(kimg_xcf_LIB_SRCS xcf.cpp)
add_library(kimg_xcf MODULE ${kimg_xcf_LIB_SRCS})
set_target_properties(kimg_xcf PROPERTIES PREFIX "")
target_link_libraries(kimg_xcf Qt5::Gui)
install(TARGETS kimg_xcf DESTINATION ${QT_PLUGIN_INSTALL_DIR}/imageformats/ )
install(FILES xcf.desktop DESTINATION ${SERVICES_INSTALL_DIR}/qimageioplugins/ )
##################################
set(kimg_dds_LIB_SRCS dds.cpp)
add_library(kimg_dds MODULE ${kimg_dds_LIB_SRCS})
set_target_properties(kimg_dds PROPERTIES PREFIX "")
target_link_libraries(kimg_dds Qt5::Gui)
install(TARGETS kimg_dds DESTINATION ${QT_PLUGIN_INSTALL_DIR}/imageformats/ )
install(FILES dds.desktop DESTINATION ${SERVICES_INSTALL_DIR}/qimageioplugins/ )
##################################
set(kimg_psd_LIB_SRCS psd.cpp)
add_library(kimg_psd MODULE ${kimg_psd_LIB_SRCS})
set_target_properties(kimg_psd PROPERTIES PREFIX "")
target_link_libraries(kimg_psd Qt5::Gui)
install(TARGETS kimg_psd DESTINATION ${QT_PLUGIN_INSTALL_DIR}/imageformats/ )
install(FILES psd.desktop DESTINATION ${SERVICES_INSTALL_DIR}/qimageioplugins/ )
##################################
set(kimg_xview_LIB_SRCS xview.cpp)
add_library(kimg_xview MODULE ${kimg_xview_LIB_SRCS})
set_target_properties(kimg_xview PROPERTIES PREFIX "")
target_link_libraries(kimg_xview Qt5::Gui)
install(TARGETS kimg_xview DESTINATION ${QT_PLUGIN_INSTALL_DIR}/imageformats/ )
install(FILES xv.desktop DESTINATION ${SERVICES_INSTALL_DIR}/qimageioplugins/ )
##################################
if(OpenEXR_FOUND)
include_directories(${OpenEXR_INCLUDE_DIR})
add_definitions(${OpenEXR_DEFINITIONS})
set(kimg_exr_LIB_SRCS exr.cpp)
add_library(kimg_exr MODULE ${kimg_exr_LIB_SRCS})
set_target_properties(kimg_exr PROPERTIES PREFIX "")
target_link_libraries(kimg_exr Qt5::Gui ${OpenEXR_LIBRARIES})
install(TARGETS kimg_exr DESTINATION ${QT_PLUGIN_INSTALL_DIR}/imageformats/ )
install(FILES exr.desktop DESTINATION ${SERVICES_INSTALL_DIR}/qimageioplugins/ )
endif()
##################################
set(kimg_pic_LIB_SRCS pic_read.cpp pic_write.cpp pic.cpp)
add_library(kimg_pic MODULE ${kimg_pic_LIB_SRCS})
set_target_properties(kimg_pic PROPERTIES PREFIX "")
target_link_libraries(kimg_pic Qt5::Gui)
install(TARGETS kimg_pic DESTINATION ${QT_PLUGIN_INSTALL_DIR}/imageformats/ )
install(FILES pic.desktop DESTINATION ${SERVICES_INSTALL_DIR}/qimageioplugins/ )

9
src/imageformats/README Normal file
View File

@ -0,0 +1,9 @@
This directory contains Qt imageformat plugins. See QImageIOPlugin docs
for how to write a new handler.
It also contains desktop files for the plugins that come with Qt.
The TGA plugin supports more formats than Qt's own TGA plugin;
specifically, the one provided here supports indexed, greyscale and RLE
images (types 1-3 and 9-11), while Qt's plugin only supports type 2
(RGB) files.

View File

@ -0,0 +1,2 @@
#cmakedefine01 HAVE_STDINT_H
#cmakedefine01 HAVE_SYS_TYPES_H

1036
src/imageformats/dds.cpp Normal file

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,7 @@
[Desktop Entry]
Type=Service
X-KDE-ServiceTypes=QImageIOPlugins
X-KDE-ImageFormat=dds
X-KDE-MimeType=image/x-dds
X-KDE-Read=true
X-KDE-Write=false

37
src/imageformats/dds.h Normal file
View File

@ -0,0 +1,37 @@
/* This file is part of the KDE project
Copyright (C) 2003 Ignacio Castaño <castano@ludicon.com>
This program is free software; you can redistribute it and/or
modify it under the terms of the Lesser GNU General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
*/
#ifndef KIMG_DDS_H
#define KIMG_DDS_H
#include <QImageIOPlugin>
class DDSHandler : public QImageIOHandler
{
public:
DDSHandler();
virtual bool canRead() const;
virtual bool read(QImage *image);
static bool canRead(QIODevice *device);
};
class DDSPlugin : public QImageIOPlugin
{
Q_OBJECT
Q_PLUGIN_METADATA(IID "org.qt-project.Qt.QImageIOHandlerFactoryInterface" FILE "dds.json")
public:
virtual Capabilities capabilities(QIODevice *device, const QByteArray &format) const;
virtual QImageIOHandler *create(QIODevice *device, const QByteArray &format = QByteArray()) const;
};
#endif // KIMG_DDS_H

View File

@ -0,0 +1,4 @@
{
"Keys": [ "dds" ],
"MimeTypes": [ "image/x-dds" ]
}

354
src/imageformats/eps.cpp Normal file
View File

@ -0,0 +1,354 @@
/**
* QImageIO Routines to read/write EPS images.
* copyright (c) 1998 Dirk Schoenberger <dirk.schoenberger@freenet.de>
* Copyright (c) 2013 Alex Merry <alex.merry@kdemail.net>
* Includes code by Sven Wiegand <SWiegand@tfh-berlin.de> from KSnapshot
*
* This library is distributed under the conditions of the GNU LGPL.
*/
#include "eps.h"
#include <QDebug>
#include <QImage>
#include <QImageReader>
#include <QLoggingCategory>
#include <QPainter>
#include <QPrinter>
#include <QProcess>
#include <QTemporaryFile>
Q_LOGGING_CATEGORY(EPSPLUGIN, "epsplugin")
//#define EPS_PERFORMANCE_DEBUG 1
#define BBOX_BUFLEN 200
#define BBOX "%%BoundingBox:"
#define BBOX_LEN strlen(BBOX)
static bool seekToCodeStart(QIODevice *io, qint64 &ps_offset, qint64 &ps_size)
{
char buf[4]; // We at most need to read 4 bytes at a time
ps_offset = 0L;
ps_size = 0L;
if (io->read(buf, 2) != 2) { // Read first two bytes
qCDebug(EPSPLUGIN) << "EPS file has less than 2 bytes.";
return false;
}
if (buf[0] == '%' && buf[1] == '!') { // Check %! magic
qCDebug(EPSPLUGIN) << "normal EPS file";
} else if (buf[0] == char(0xc5) && buf[1] == char(0xd0)) { // Check start of MS-DOS EPS magic
// May be a MS-DOS EPS file
if (io->read(buf + 2, 2) != 2) { // Read further bytes of MS-DOS EPS magic
qCDebug(EPSPLUGIN) << "potential MS-DOS EPS file has less than 4 bytes.";
return false;
}
if (buf[2] == char(0xd3) && buf[3] == char(0xc6)) { // Check last bytes of MS-DOS EPS magic
if (io->read(buf, 4) != 4) { // Get offset of PostScript code in the MS-DOS EPS file.
qCDebug(EPSPLUGIN) << "cannot read offset of MS-DOS EPS file";
return false;
}
ps_offset // Offset is in little endian
= qint64( ((unsigned char)buf[0])
+ ((unsigned char)buf[1] << 8)
+ ((unsigned char)buf[2] << 16)
+ ((unsigned char)buf[3] << 24));
if (io->read(buf, 4) != 4) { // Get size of PostScript code in the MS-DOS EPS file.
qCDebug(EPSPLUGIN) << "cannot read size of MS-DOS EPS file";
return false;
}
ps_size // Size is in little endian
= qint64( ((unsigned char)buf[0])
+ ((unsigned char)buf[1] << 8)
+ ((unsigned char)buf[2] << 16)
+ ((unsigned char)buf[3] << 24));
qCDebug(EPSPLUGIN) << "Offset: " << ps_offset <<" Size: " << ps_size;
if (!io->seek(ps_offset)) { // Get offset of PostScript code in the MS-DOS EPS file.
qCDebug(EPSPLUGIN) << "cannot seek in MS-DOS EPS file";
return false;
}
if (io->read(buf, 2) != 2) { // Read first two bytes of what should be the Postscript code
qCDebug(EPSPLUGIN) << "PostScript code has less than 2 bytes.";
return false;
}
if (buf[0] == '%' && buf[1] == '!') { // Check %! magic
qCDebug(EPSPLUGIN) << "MS-DOS EPS file";
} else {
qCDebug(EPSPLUGIN) << "supposed Postscript code of a MS-DOS EPS file doe not start with %!.";
return false;
}
} else {
qCDebug(EPSPLUGIN) << "wrong magic for potential MS-DOS EPS file!";
return false;
}
} else {
qCDebug(EPSPLUGIN) << "not an EPS file!";
return false;
}
return true;
}
static bool bbox(QIODevice *io, int *x1, int *y1, int *x2, int *y2)
{
char buf[BBOX_BUFLEN + 1];
bool ret = false;
while (io->readLine(buf, BBOX_BUFLEN) > 0) {
if (strncmp(buf, BBOX, BBOX_LEN) == 0) {
// Some EPS files have non-integer values for the bbox
// We don't support that currently, but at least we parse it
float _x1, _y1, _x2, _y2;
if (sscanf(buf, "%*s %f %f %f %f",
&_x1, &_y1, &_x2, &_y2) == 4) {
qCDebug(EPSPLUGIN) << "BBOX: " << _x1 << " " << _y1 << " " << _x2 << " " << _y2;
*x1 = int(_x1);
*y1 = int(_y1);
*x2 = int(_x2);
*y2 = int(_y2);
ret = true;
break;
}
}
}
return ret;
}
EPSHandler::EPSHandler()
{
}
bool EPSHandler::canRead() const
{
if (canRead(device())) {
setFormat("eps");
return true;
}
return false;
}
bool EPSHandler::read(QImage *image)
{
qCDebug(EPSPLUGIN) << "starting...";
int x1, y1, x2, y2;
#ifdef EPS_PERFORMANCE_DEBUG
QTime dt;
dt.start();
#endif
QIODevice* io = device();
qint64 ps_offset, ps_size;
// find start of PostScript code
if (!seekToCodeStart(io, ps_offset, ps_size))
return false;
qCDebug(EPSPLUGIN) << "Offset:" << ps_offset << "; size:" << ps_size;
// find bounding box
if (!bbox(io, &x1, &y1, &x2, &y2)) {
qCDebug(EPSPLUGIN) << "no bounding box found!";
return false;
}
QTemporaryFile tmpFile;
if (!tmpFile.open()) {
qWarning() << "Could not create the temporary file" << tmpFile.fileName();
return false;
}
qCDebug(EPSPLUGIN) << "temporary file:" << tmpFile.fileName();
// x1, y1 -> translation
// x2, y2 -> new size
x2 -= x1;
y2 -= y1;
qCDebug(EPSPLUGIN) << "origin point: " << x1 << "," << y1 << " size:" << x2 << "," << y2;
double xScale = 1.0;
double yScale = 1.0;
int wantedWidth = x2;
int wantedHeight = y2;
// create GS command line
QStringList gsArgs;
gsArgs << QLatin1String("-sOutputFile=") + tmpFile.fileName()
<< QStringLiteral("-q")
<< QString(QStringLiteral("-g%1x%2")).arg(wantedWidth).arg(wantedHeight)
<< QStringLiteral("-dSAFER")
<< QStringLiteral("-dPARANOIDSAFER")
<< QStringLiteral("-dNOPAUSE")
<< QStringLiteral("-sDEVICE=ppm")
<< QStringLiteral("-c")
<< QStringLiteral("0 0 moveto "
"1000 0 lineto "
"1000 1000 lineto "
"0 1000 lineto "
"1 1 254 255 div setrgbcolor fill "
"0 0 0 setrgbcolor")
<< QStringLiteral("-")
<< QStringLiteral("-c")
<< QStringLiteral("showpage quit");
qCDebug(EPSPLUGIN) << "Running gs with args" << gsArgs;
QProcess converter;
converter.setProcessChannelMode(QProcess::ForwardedErrorChannel);
converter.start(QStringLiteral("gs"), gsArgs);
if (!converter.waitForStarted(3000)) {
qWarning() << "Reading EPS files requires gs (from GhostScript)";
return false;
}
QByteArray intro = "\n";
intro += QByteArray::number(-qRound(x1*xScale));
intro += " ";
intro += QByteArray::number(-qRound(y1*yScale));
intro += " translate\n";
converter.write(intro);
io->reset();
if (ps_offset > 0)
io->seek(ps_offset);
QByteArray buffer;
buffer.resize(4096);
bool limited = ps_size > 0;
qint64 remaining = ps_size;
qint64 count = io->read(buffer.data(), buffer.size());
while (count > 0) {
if (limited) {
if (count > remaining) {
count = remaining;
}
remaining -= count;
}
converter.write(buffer.constData(), count);
if (!limited || remaining > 0) {
count = io->read(buffer.data(), buffer.size());
}
}
converter.closeWriteChannel();
converter.waitForFinished(-1);
QImageReader ppmReader(tmpFile.fileName(), "ppm");
if (ppmReader.read(image)) {
qCDebug(EPSPLUGIN) << "success!";
#ifdef EPS_PERFORMANCE_DEBUG
qCDebug(EPSPLUGIN) << "Loading EPS took " << (float)(dt.elapsed()) / 1000 << " seconds";
#endif
return true;
} else {
qCDebug(EPSPLUGIN) << "Reading failed:" << ppmReader.errorString();
return false;
}
}
bool EPSHandler::write(const QImage &image)
{
QPrinter psOut(QPrinter::PrinterResolution);
QPainter p;
QTemporaryFile tmpFile(QStringLiteral("XXXXXXXX.pdf"));
if (!tmpFile.open())
return false;
psOut.setCreator(QStringLiteral("KDE EPS image plugin"));
psOut.setOutputFileName(tmpFile.fileName());
psOut.setOutputFormat(QPrinter::PdfFormat);
psOut.setFullPage(true);
psOut.setPaperSize(image.size(), QPrinter::DevicePixel);
// painting the pixmap to the "printer" which is a file
p.begin(&psOut);
p.drawImage(QPoint(0, 0), image);
p.end();
QProcess converter;
converter.setProcessChannelMode(QProcess::ForwardedErrorChannel);
converter.setReadChannel(QProcess::StandardOutput);
// pdftops comes with Poppler and produces much smaller EPS files than GhostScript
QStringList pdftopsArgs;
pdftopsArgs << QStringLiteral("-eps")
<< tmpFile.fileName()
<< QStringLiteral("-");
qCDebug(EPSPLUGIN) << "Running pdftops with args" << pdftopsArgs;
converter.start(QStringLiteral("pdftops"), pdftopsArgs);
if (!converter.waitForStarted()) {
// GhostScript produces huge files, and takes a long time doing so
QStringList gsArgs;
gsArgs << QStringLiteral("-q") << QStringLiteral("-P-")
<< QStringLiteral("-dNOPAUSE") << QStringLiteral("-dBATCH")
<< QStringLiteral("-dSAFER")
<< QStringLiteral("-sDEVICE=epswrite")
<< QStringLiteral("-sOutputFile=-")
<< QStringLiteral("-c")
<< QStringLiteral("save") << QStringLiteral("pop")
<< QStringLiteral("-f")
<< tmpFile.fileName();
qCDebug(EPSPLUGIN) << "Failed to start pdftops; trying gs with args" << gsArgs;
converter.start(QStringLiteral("gs"), gsArgs);
if (!converter.waitForStarted(3000)) {
qWarning() << "Creating EPS files requires pdftops (from Poppler) or gs (from GhostScript)";
return false;
}
}
while (converter.bytesAvailable() || (converter.state() == QProcess::Running && converter.waitForReadyRead(2000))) {
device()->write(converter.readAll());
}
return true;
}
bool EPSHandler::canRead(QIODevice *device)
{
if (!device) {
qWarning("EPSHandler::canRead() called with no device");
return false;
}
qint64 oldPos = device->pos();
QByteArray head = device->readLine(64);
int readBytes = head.size();
if (device->isSequential()) {
while (readBytes > 0)
device->ungetChar(head[readBytes-- - 1]);
} else {
device->seek(oldPos);
}
return head.contains("%!PS-Adobe");
}
QImageIOPlugin::Capabilities EPSPlugin::capabilities(QIODevice *device, const QByteArray &format) const
{
if (format == "eps" || format == "epsi" || format == "epsf")
return Capabilities(CanRead | CanWrite);
if (!format.isEmpty())
return 0;
if (!device->isOpen())
return 0;
Capabilities cap;
if (device->isReadable() && EPSHandler::canRead(device))
cap |= CanRead;
if (device->isWritable())
cap |= CanWrite;
return cap;
}
QImageIOHandler *EPSPlugin::create(QIODevice *device, const QByteArray &format) const
{
QImageIOHandler *handler = new EPSHandler;
handler->setDevice(device);
handler->setFormat(format);
return handler;
}

View File

@ -0,0 +1,7 @@
[Desktop Entry]
Type=Service
X-KDE-ServiceTypes=QImageIOPlugins
X-KDE-ImageFormat=eps,epsi,epsf
X-KDE-MimeType=image/x-eps
X-KDE-Read=true
X-KDE-Write=true

35
src/imageformats/eps.h Normal file
View File

@ -0,0 +1,35 @@
/**
* QImageIO Routines to read/write EPS images.
* copyright (c) 1998 Dirk Schoenberger <dirk.schoenberger@freenet.de>
*
* This library is distributed under the conditions of the GNU LGPL.
*/
#ifndef KIMG_EPS_H
#define KIMG_EPS_H
#include <QImageIOPlugin>
class EPSHandler : public QImageIOHandler
{
public:
EPSHandler();
virtual bool canRead() const;
virtual bool read(QImage *image);
virtual bool write(const QImage &image);
static bool canRead(QIODevice *device);
};
class EPSPlugin : public QImageIOPlugin
{
Q_OBJECT
Q_PLUGIN_METADATA(IID "org.qt-project.Qt.QImageIOHandlerFactoryInterface" FILE "eps.json")
public:
virtual Capabilities capabilities(QIODevice *device, const QByteArray &format) const;
virtual QImageIOHandler *create(QIODevice *device, const QByteArray &format = QByteArray()) const;
};
#endif // KIMG_EPS_H

View File

@ -0,0 +1,4 @@
{
"Keys": [ "eps", "epsi", "epsf" ],
"MimeTypes": [ "image/x-eps" ]
}

239
src/imageformats/exr.cpp Normal file
View File

@ -0,0 +1,239 @@
// -*- C++;indent-tabs-mode: t; tab-width: 4; c-basic-offset: 4; -*-
/**
* KImageIO Routines to read (and perhaps in the future, write) images
* in the high dynamic range EXR format.
* Copyright (c) 2003, Brad Hards <bradh@frogmouth.net>
*
* This library is distributed under the conditions of the GNU LGPL.
*/
#include "exr.h"
#include <ImfRgbaFile.h>
#include <ImfStandardAttributes.h>
#include <ImathBox.h>
#include <ImfInputFile.h>
#include <ImfBoxAttribute.h>
#include <ImfChannelListAttribute.h>
#include <ImfCompressionAttribute.h>
#include <ImfFloatAttribute.h>
#include <ImfIntAttribute.h>
#include <ImfLineOrderAttribute.h>
#include <ImfStringAttribute.h>
#include <ImfVecAttribute.h>
#include <ImfArray.h>
#include <ImfConvert.h>
#include <ImfVersion.h>
#include <IexThrowErrnoExc.h>
#include <iostream>
#include <QImage>
#include <QDataStream>
// #include <QDebug>
#include <QImageIOPlugin>
class K_IStream: public Imf::IStream
{
public:
K_IStream(QIODevice *dev, const QByteArray& fileName):
IStream(fileName.data()), m_dev(dev) {
}
virtual bool read(char c[], int n);
virtual Imf::Int64 tellg();
virtual void seekg(Imf::Int64 pos);
virtual void clear();
private:
QIODevice *m_dev;
};
bool K_IStream::read(char c[], int n)
{
qint64 result = m_dev->read(c, n);
if (result > 0) {
return true;
} else if (result == 0) {
throw Iex::InputExc("Unexpected end of file");
} else // negative value {
Iex::throwErrnoExc("Error in read", result);
return false;
}
Imf::Int64 K_IStream::tellg()
{
return m_dev->pos();
}
void K_IStream::seekg(Imf::Int64 pos)
{
m_dev->seek(pos);
}
void K_IStream::clear()
{
// TODO
}
/* this does a conversion from the ILM Half (equal to Nvidia Half)
* format into the normal 32 bit pixel format. Process is from the
* ILM code.
*/
QRgb RgbaToQrgba(struct Imf::Rgba imagePixel)
{
float r, g, b, a;
// 1) Compensate for fogging by subtracting defog
// from the raw pixel values.
// Response: We work with defog of 0.0, so this is a no-op
// 2) Multiply the defogged pixel values by
// 2^(exposure + 2.47393).
// Response: We work with exposure of 0.0.
// (2^2.47393) is 5.55555
r = imagePixel.r * 5.55555;
g = imagePixel.g * 5.55555;
b = imagePixel.b * 5.55555;
a = imagePixel.a * 5.55555;
// 3) Values, which are now 1.0, are called "middle gray".
// If defog and exposure are both set to 0.0, then
// middle gray corresponds to a raw pixel value of 0.18.
// In step 6, middle gray values will be mapped to an
// intensity 3.5 f-stops below the display's maximum
// intensity.
// Response: no apparent content.
// 4) Apply a knee function. The knee function has two
// parameters, kneeLow and kneeHigh. Pixel values
// below 2^kneeLow are not changed by the knee
// function. Pixel values above kneeLow are lowered
// according to a logarithmic curve, such that the
// value 2^kneeHigh is mapped to 2^3.5 (in step 6,
// this value will be mapped to the display's
// maximum intensity).
// Response: kneeLow = 0.0 (2^0.0 => 1); kneeHigh = 5.0 (2^5 =>32)
if (r > 1.0)
r = 1.0 + Imath::Math<float>::log((r - 1.0) * 0.184874 + 1) / 0.184874;
if (g > 1.0)
g = 1.0 + Imath::Math<float>::log((g - 1.0) * 0.184874 + 1) / 0.184874;
if (b > 1.0)
b = 1.0 + Imath::Math<float>::log((b - 1.0) * 0.184874 + 1) / 0.184874;
if (a > 1.0)
a = 1.0 + Imath::Math<float>::log((a - 1.0) * 0.184874 + 1) / 0.184874;
//
// 5) Gamma-correct the pixel values, assuming that the
// screen's gamma is 0.4545 (or 1/2.2).
r = Imath::Math<float>::pow(r, 0.4545);
g = Imath::Math<float>::pow(g, 0.4545);
b = Imath::Math<float>::pow(b, 0.4545);
a = Imath::Math<float>::pow(a, 0.4545);
// 6) Scale the values such that pixels middle gray
// pixels are mapped to 84.66 (or 3.5 f-stops below
// the display's maximum intensity).
//
// 7) Clamp the values to [0, 255].
return qRgba((unsigned char)(Imath::clamp(r * 84.66f, 0.f, 255.f)),
(unsigned char)(Imath::clamp(g * 84.66f, 0.f, 255.f)),
(unsigned char)(Imath::clamp(b * 84.66f, 0.f, 255.f)),
(unsigned char)(Imath::clamp(a * 84.66f, 0.f, 255.f)));
}
EXRHandler::EXRHandler()
{
}
bool EXRHandler::canRead() const
{
if (canRead(device())) {
setFormat("exr");
return true;
}
return false;
}
bool EXRHandler::read(QImage *outImage)
{
try {
int width, height;
K_IStream istr(device(), QByteArray());
Imf::RgbaInputFile file(istr);
Imath::Box2i dw = file.dataWindow();
width = dw.max.x - dw.min.x + 1;
height = dw.max.y - dw.min.y + 1;
Imf::Array2D<Imf::Rgba> pixels;
pixels.resizeErase(height, width);
file.setFrameBuffer(&pixels[0][0] - dw.min.x - dw.min.y * width, 1, width);
file.readPixels(dw.min.y, dw.max.y);
QImage image(width, height, QImage::Format_RGB32);
if (image.isNull())
return false;
// somehow copy pixels into image
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
// copy pixels(x,y) into image(x,y)
image.setPixel(x, y, RgbaToQrgba(pixels[y][x]));
}
}
*outImage = image;
return true;
} catch (const std::exception &exc) {
// qDebug() << exc.what();
return false;
}
}
bool EXRHandler::write(const QImage &image)
{
// TODO: stub
Q_UNUSED(image);
return false;
}
bool EXRHandler::canRead(QIODevice *device)
{
if (!device) {
qWarning("EXRHandler::canRead() called with no device");
return false;
}
const QByteArray head = device->peek(4);
return Imf::isImfMagic(head.data());
}
QImageIOPlugin::Capabilities EXRPlugin::capabilities(QIODevice *device, const QByteArray &format) const
{
if (format == "exr")
return Capabilities(CanRead);
if (!format.isEmpty())
return 0;
if (!device->isOpen())
return 0;
Capabilities cap;
if (device->isReadable() && EXRHandler::canRead(device))
cap |= CanRead;
return cap;
}
QImageIOHandler *EXRPlugin::create(QIODevice *device, const QByteArray &format) const
{
QImageIOHandler *handler = new EXRHandler;
handler->setDevice(device);
handler->setFormat(format);
return handler;
}

View File

@ -0,0 +1,7 @@
[Desktop Entry]
Type=Service
X-KDE-ServiceTypes=QImageIOPlugins
X-KDE-ImageFormat=exr
X-KDE-MimeType=image/x-exr
X-KDE-Read=true
X-KDE-Write=false

38
src/imageformats/exr.h Normal file
View File

@ -0,0 +1,38 @@
/**
* QImageIO Routines to read (and perhaps in the future, write) images
* in the high definition EXR format.
*
* Copyright (c) 2003, Brad Hards <bradh@frogmouth.net>
*
* This library is distributed under the conditions of the GNU LGPL.
*
*/
#ifndef KIMG_EXR_H
#define KIMG_EXR_H
#include <QImageIOPlugin>
class EXRHandler : public QImageIOHandler
{
public:
EXRHandler();
virtual bool canRead() const;
virtual bool read(QImage *outImage);
virtual bool write(const QImage &image);
static bool canRead(QIODevice *device);
};
class EXRPlugin : public QImageIOPlugin
{
Q_OBJECT
Q_PLUGIN_METADATA(IID "org.qt-project.Qt.QImageIOHandlerFactoryInterface" FILE "exr.json")
public:
virtual Capabilities capabilities(QIODevice *device, const QByteArray &format) const;
virtual QImageIOHandler *create(QIODevice *device, const QByteArray &format = QByteArray()) const;
};
#endif // KIMG_EXR_H

View File

@ -0,0 +1,4 @@
{
"Keys": [ "exr" ],
"MimeTypes": [ "image/x-exr" ]
}

406
src/imageformats/gimp.h Normal file
View File

@ -0,0 +1,406 @@
#ifndef GIMP_H
#define GIMP_H
/* -*- c++ -*-
* gimp.h: Header for a Qt 3 plug-in for reading GIMP XCF image files
* Copyright (C) 2001 lignum Computing, Inc. <allen@lignumcomputing.com>
* Copyright (C) 2004 Melchior FRANZ <mfranz@kde.org>
*
* This plug-in is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
*/
typedef unsigned char uchar;
/*
* These are the constants and functions I extracted from The GIMP source
* code. If the reader fails to work, this is probably the place to start
* looking for discontinuities.
*/
// From GIMP "tile.h" v1.2
const uint TILE_WIDTH = 64; //!< Width of a tile in the XCF file.
const uint TILE_HEIGHT = 64; //!< Height of a tile in the XCF file.
// From GIMP "paint_funcs.c" v1.2
const int RANDOM_TABLE_SIZE = 4096; //!< Size of dissolve random number table.
const int RANDOM_SEED = 314159265; //!< Seed for dissolve random number table.
const double EPSILON = 0.0001; //!< Roundup in alpha blending.
// From GIMP "paint_funcs.h" v1.2
const uchar OPAQUE_OPACITY = 255; //!< Opaque value for 8-bit alpha component.
// From GIMP "apptypes.h" v1.2
//! Basic GIMP image type. QImage converter may produce a deeper image
//! than is specified here. For example, a grayscale image with an
//! alpha channel must (currently) use a 32-bit Qt image.
typedef enum {
RGB,
GRAY,
INDEXED
} GimpImageBaseType;
//! Type of individual layers in an XCF file.
typedef enum {
RGB_GIMAGE,
RGBA_GIMAGE,
GRAY_GIMAGE,
GRAYA_GIMAGE,
INDEXED_GIMAGE,
INDEXEDA_GIMAGE
} GimpImageType;
// From GIMP "libgimp/gimpenums.h" v2.4
//! Effect to apply when layers are merged together.
typedef enum {
NORMAL_MODE,
DISSOLVE_MODE,
BEHIND_MODE,
MULTIPLY_MODE,
SCREEN_MODE,
OVERLAY_MODE,
DIFFERENCE_MODE,
ADDITION_MODE,
SUBTRACT_MODE,
DARKEN_ONLY_MODE,
LIGHTEN_ONLY_MODE,
HUE_MODE,
SATURATION_MODE,
COLOR_MODE,
VALUE_MODE,
DIVIDE_MODE,
DODGE_MODE,
BURN_MODE,
HARDLIGHT_MODE,
SOFTLIGHT_MODE,
GRAIN_EXTRACT_MODE,
GRAIN_MERGE_MODE
} LayerModeEffects;
// From GIMP "xcf.c" v1.2
//! Properties which can be stored in an XCF file.
typedef enum {
PROP_END = 0,
PROP_COLORMAP = 1,
PROP_ACTIVE_LAYER = 2,
PROP_ACTIVE_CHANNEL = 3,
PROP_SELECTION = 4,
PROP_FLOATING_SELECTION = 5,
PROP_OPACITY = 6,
PROP_MODE = 7,
PROP_VISIBLE = 8,
PROP_LINKED = 9,
PROP_PRESERVE_TRANSPARENCY = 10,
PROP_APPLY_MASK = 11,
PROP_EDIT_MASK = 12,
PROP_SHOW_MASK = 13,
PROP_SHOW_MASKED = 14,
PROP_OFFSETS = 15,
PROP_COLOR = 16,
PROP_COMPRESSION = 17,
PROP_GUIDES = 18,
PROP_RESOLUTION = 19,
PROP_TATTOO = 20,
PROP_PARASITES = 21,
PROP_UNIT = 22,
PROP_PATHS = 23,
PROP_USER_UNIT = 24
} PropType;
// From GIMP "xcf.c" v1.2
//! Compression type used in layer tiles.
typedef enum {
COMPRESS_NONE = 0,
COMPRESS_RLE = 1,
COMPRESS_ZLIB = 2,
COMPRESS_FRACTAL = 3 /* Unused. */
} CompressionType;
// From GIMP "paint_funcs.c" v1.2
/*!
* Multiply two color components. Really expects the arguments to be
* 8-bit quantities.
* \param a first minuend.
* \param b second minuend.
* \return product of arguments.
*/
inline int INT_MULT(int a, int b)
{
int c = a * b + 0x80;
return ((c >> 8) + c) >> 8;
}
/*!
* Blend the two color components in the proportion alpha:
*
* result = alpha a + ( 1 - alpha ) b
*
* \param a first component.
* \param b second component.
* \param alpha blend proportion.
* \return blended color components.
*/
inline int INT_BLEND(int a, int b, int alpha)
{
return INT_MULT(a - b, alpha) + b;
}
// From GIMP "gimpcolorspace.c" v1.2
/*!
* Convert a color in RGB space to HSV space (Hue, Saturation, Value).
* \param red the red component (modified in place).
* \param green the green component (modified in place).
* \param blue the blue component (modified in place).
*/
static void RGBTOHSV(uchar& red, uchar& green, uchar& blue)
{
int r, g, b;
double h, s, v;
int min, max;
h = 0.;
r = red;
g = green;
b = blue;
if (r > g) {
max = qMax(r, b);
min = qMin(g, b);
} else {
max = qMax(g, b);
min = qMin(r, b);
}
v = max;
if (max != 0)
s = ((max - min) * 255) / (double)max;
else
s = 0;
if (s == 0)
h = 0;
else {
int delta = max - min;
if (r == max)
h = (g - b) / (double)delta;
else if (g == max)
h = 2 + (b - r) / (double)delta;
else if (b == max)
h = 4 + (r - g) / (double)delta;
h *= 42.5;
if (h < 0)
h += 255;
if (h > 255)
h -= 255;
}
red = (uchar)h;
green = (uchar)s;
blue = (uchar)v;
}
/*!
* Convert a color in HSV space to RGB space.
* \param hue the hue component (modified in place).
* \param saturation the saturation component (modified in place).
* \param value the value component (modified in place).
*/
static void HSVTORGB(uchar& hue, uchar& saturation, uchar& value)
{
if (saturation == 0) {
hue = value;
saturation = value;
//value = value;
} else {
double h = hue * 6. / 255.;
double s = saturation / 255.;
double v = value / 255.;
double f = h - (int)h;
double p = v * (1. - s);
double q = v * (1. - (s * f));
double t = v * (1. - (s * (1. - f)));
// Worth a note here that gcc 2.96 will generate different results
// depending on optimization mode on i386.
switch ((int)h) {
case 0:
hue = (uchar)(v * 255);
saturation = (uchar)(t * 255);
value = (uchar)(p * 255);
break;
case 1:
hue = (uchar)(q * 255);
saturation = (uchar)(v * 255);
value = (uchar)(p * 255);
break;
case 2:
hue = (uchar)(p * 255);
saturation = (uchar)(v * 255);
value = (uchar)(t * 255);
break;
case 3:
hue = (uchar)(p * 255);
saturation = (uchar)(q * 255);
value = (uchar)(v * 255);
break;
case 4:
hue = (uchar)(t * 255);
saturation = (uchar)(p * 255);
value = (uchar)(v * 255);
break;
case 5:
hue = (uchar)(v * 255);
saturation = (uchar)(p * 255);
value = (uchar)(q * 255);
}
}
}
/*!
* Convert a color in RGB space to HLS space (Hue, Lightness, Saturation).
* \param red the red component (modified in place).
* \param green the green component (modified in place).
* \param blue the blue component (modified in place).
*/
static void RGBTOHLS(uchar& red, uchar& green, uchar& blue)
{
int r = red;
int g = green;
int b = blue;
int min, max;
if (r > g) {
max = qMax(r, b);
min = qMin(g, b);
} else {
max = qMax(g, b);
min = qMin(r, b);
}
double h;
double l = (max + min) / 2.;
double s;
if (max == min) {
s = 0.;
h = 0.;
} else {
int delta = max - min;
if (l < 128)
s = 255 * (double)delta / (double)(max + min);
else
s = 255 * (double)delta / (double)(511 - max - min);
if (r == max)
h = (g - b) / (double)delta;
else if (g == max)
h = 2 + (b - r) / (double)delta;
else
h = 4 + (r - g) / (double)delta;
h *= 42.5;
if (h < 0)
h += 255;
else if (h > 255)
h -= 255;
}
red = (uchar)h;
green = (uchar)l;
blue = (uchar)s;
}
/*!
* Implement the HLS "double hex-cone".
* \param n1 lightness fraction (?)
* \param n2 saturation fraction (?)
* \param hue hue "angle".
* \return HLS value.
*/
static int HLSVALUE(double n1, double n2, double hue)
{
double value;
if (hue > 255)
hue -= 255;
else if (hue < 0)
hue += 255;
if (hue < 42.5)
value = n1 + (n2 - n1) * (hue / 42.5);
else if (hue < 127.5)
value = n2;
else if (hue < 170)
value = n1 + (n2 - n1) * ((170 - hue) / 42.5);
else
value = n1;
return (int)(value * 255);
}
/*!
* Convert a color in HLS space to RGB space.
* \param hue the hue component (modified in place).
* \param lightness the lightness component (modified in place).
* \param saturation the saturation component (modified in place).
*/
static void HLSTORGB(uchar& hue, uchar& lightness, uchar& saturation)
{
double h = hue;
double l = lightness;
double s = saturation;
if (s == 0) {
hue = (uchar)l;
lightness = (uchar)l;
saturation = (uchar)l;
} else {
double m1, m2;
if (l < 128)
m2 = (l * (255 + s)) / 65025.;
else
m2 = (l + s - (l * s) / 255.) / 255.;
m1 = (l / 127.5) - m2;
hue = HLSVALUE(m1, m2, h + 85);
lightness = HLSVALUE(m1, m2, h);
saturation = HLSVALUE(m1, m2, h - 85);
}
}
#endif

237
src/imageformats/hdr.cpp Normal file
View File

@ -0,0 +1,237 @@
/* This file is part of the KDE project
Copyright (C) 2005 Christoph Hormann <chris_hormann@gmx.de>
Copyright (C) 2005 Ignacio Castaño <castanyo@yahoo.es>
This program is free software; you can redistribute it and/or
modify it under the terms of the Lesser GNU General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
*/
#include "hdr.h"
#include <QImage>
#include <QtCore/QDataStream>
#include <QDebug>
typedef Q_UINT8 uchar;
namespace // Private.
{
#define MAXLINE 1024
#define MINELEN 8 // minimum scanline length for encoding
#define MAXELEN 0x7fff // maximum scanline length for encoding
static inline uchar ClipToByte(float value)
{
if (value > 255.0f) return 255;
//else if (value < 0.0f) return 0; // we know value is positive.
return uchar(value);
}
// read an old style line from the hdr image file
// if 'first' is true the first byte is already read
static bool Read_Old_Line(uchar * image, int width, QDataStream & s)
{
int rshift = 0;
int i;
while (width > 0) {
s >> image[0];
s >> image[1];
s >> image[2];
s >> image[3];
if (s.atEnd()) return false;
if ((image[0] == 1) && (image[1] == 1) && (image[2] == 1)) {
for (i = image[3] << rshift; i > 0; i--) {
//memcpy(image, image-4, 4);
(uint &)image[0] = (uint &)image[0 - 4];
image += 4;
width--;
}
rshift += 8;
} else {
image += 4;
width--;
rshift = 0;
}
}
return true;
}
static void RGBE_To_QRgbLine(uchar * image, QRgb * scanline, int width)
{
for (int j = 0; j < width; j++) {
// v = ldexp(1.0, int(image[3]) - 128);
float v;
int e = int(image[3]) - 128;
if (e > 0) {
v = float(1 << e);
} else {
v = 1.0f / float(1 << -e);
}
scanline[j] = qRgb(ClipToByte(float(image[0]) * v),
ClipToByte(float(image[1]) * v),
ClipToByte(float(image[2]) * v));
image += 4;
}
}
// Load the HDR image.
static bool LoadHDR(QDataStream & s, const int width, const int height, QImage & img)
{
uchar val, code;
// Create dst image.
if (!img.create(width, height, 32)) {
return false;
}
QMemArray<uchar> image(width * 4);
for (int cline = 0; cline < height; cline++) {
QRgb * scanline = (QRgb *) img.scanLine(cline);
// determine scanline type
if ((width < MINELEN) || (MAXELEN < width)) {
Read_Old_Line(image.data(), width, s);
RGBE_To_QRgbLine(image.data(), scanline, width);
continue;
}
s >> val;
if (s.atEnd()) {
return true;
}
if (val != 2) {
s.device()->at(s.device()->at() - 1);
Read_Old_Line(image.data(), width, s);
RGBE_To_QRgbLine(image.data(), scanline, width);
continue;
}
s >> image[1];
s >> image[2];
s >> image[3];
if (s.atEnd()) {
return true;
}
if ((image[1] != 2) || (image[2] & 128)) {
image[0] = 2;
Read_Old_Line(image.data() + 4, width - 1, s);
RGBE_To_QRgbLine(image.data(), scanline, width);
continue;
}
if ((image[2] << 8 | image[3]) != width) {
return false;
}
// read each component
for (int i = 0; i < 4; i++) {
for (int j = 0; j < width;) {
s >> code;
if (s.atEnd()) {
return false;
}
if (code > 128) {
// run
code &= 127;
s >> val;
while (code != 0) {
image[i + j * 4] = val;
j++;
code--;
}
} else {
// non-run
while (code != 0) {
s >> image[i + j * 4];
j++;
code--;
}
}
}
}
RGBE_To_QRgbLine(image.data(), scanline, width);
}
return true;
}
} // namespace
Q_DECL_EXPORT void kimgio_hdr_read(QImageIO * io)
{
int len;
char line[MAXLINE];
//bool validHeader = false;
bool validFormat = false;
// Parse header
do {
len = io->ioDevice()->readLine(line, MAXLINE);
/*if (strcmp(line, "#?RADIANCE\n") == 0 || strcmp(line, "#?RGBE\n") == 0)
{
validHeader = true;
}*/
if (strcmp(line, "FORMAT=32-bit_rle_rgbe\n") == 0) {
validFormat = true;
}
} while ((len > 0) && (line[0] != '\n'));
if (/*!validHeader ||*/ !validFormat) {
// qDebug() << "Unknown HDR format.";
io->setImage(0);
io->setStatus(-1);
return;
}
io->ioDevice()->readLine(line, MAXLINE);
char s1[3], s2[3];
int width, height;
if (sscanf(line, "%2[+-XY] %d %2[+-XY] %d\n", s1, &height, s2, &width) != 4)
//if( sscanf(line, "-Y %d +X %d", &height, &width) < 2 )
{
// qDebug() << "Invalid HDR file.";
io->setImage(0);
io->setStatus(-1);
return;
}
QDataStream s(io->ioDevice());
QImage img;
if (!LoadHDR(s, width, height, img)) {
// qDebug() << "Error loading HDR file.";
io->setImage(0);
io->setStatus(-1);
return;
}
io->setImage(img);
io->setStatus(0);
}
Q_DECL_EXPORT void kimgio_hdr_write(QImageIO *)
{
// intentionally not implemented (since writing low dynamic range data to a HDR file is nonsense.)
}

View File

@ -0,0 +1,7 @@
[Desktop Entry]
Type=Service
X-KDE-ServiceTypes=QImageIOPlugins
X-KDE-ImageFormat=hdr
X-KDE-MimeType=image/x-hdr
X-KDE-Read=true
X-KDE-Write=false

21
src/imageformats/hdr.h Normal file
View File

@ -0,0 +1,21 @@
/* This file is part of the KDE project
Copyright (C) 2005 Christoph Hormann <chris_hormann@gmx.de>
This program is free software; you can redistribute it and/or
modify it under the terms of the Lesser GNU General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
*/
#ifndef KIMG_HDR_H
#define KIMG_HDR_H
class QImageIO;
extern "C" {
void kimgio_hdr_read(QImageIO *);
void kimgio_hdr_write(QImageIO *);
}
#endif

489
src/imageformats/jp2.cpp Normal file
View File

@ -0,0 +1,489 @@
/**
* QImageIO Routines to read/write JPEG2000 images.
* copyright (c) 2002 Michael Ritzert <michael@ritzert.de>
*
* This library is distributed under the conditions of the GNU LGPL.
*/
#include "jp2.h"
#include <config-kimgio.h>
#if HAVE_SYS_TYPES_H
#include <sys/types.h>
#endif
#if HAVE_STDINT_H
#include <stdint.h>
#endif
#include <QImage>
#include <QVariant>
#include <QTextStream>
// dirty, but avoids a warning because jasper.h includes jas_config.h.
#undef PACKAGE
#undef VERSION
#include <jasper/jasper.h>
// code taken in parts from JasPer's jiv.c
#define DEFAULT_RATE 0.10
#define MAXCMPTS 256
/************************* JasPer QIODevice stream ***********************/
//unfortunately this is declared as static in JasPer libraries
static jas_stream_t *jas_stream_create()
{
jas_stream_t *stream;
if (!(stream = (jas_stream_t*)jas_malloc(sizeof(jas_stream_t)))) {
return 0;
}
stream->openmode_ = 0;
stream->bufmode_ = 0;
stream->flags_ = 0;
stream->bufbase_ = 0;
stream->bufstart_ = 0;
stream->bufsize_ = 0;
stream->ptr_ = 0;
stream->cnt_ = 0;
stream->ops_ = 0;
stream->obj_ = 0;
stream->rwcnt_ = 0;
stream->rwlimit_ = -1;
return stream;
}
//unfortunately this is declared as static in JasPer libraries
static void jas_stream_initbuf(jas_stream_t *stream, int bufmode, char *buf,
int bufsize)
{
/* If this function is being called, the buffer should not have been
initialized yet. */
assert(!stream->bufbase_);
if (bufmode != JAS_STREAM_UNBUF) {
/* The full- or line-buffered mode is being employed. */
if (!buf) {
/* The caller has not specified a buffer to employ, so allocate
one. */
if ((stream->bufbase_ = (unsigned char*)jas_malloc(JAS_STREAM_BUFSIZE +
JAS_STREAM_MAXPUTBACK))) {
stream->bufmode_ |= JAS_STREAM_FREEBUF;
stream->bufsize_ = JAS_STREAM_BUFSIZE;
} else {
/* The buffer allocation has failed. Resort to unbuffered
operation. */
stream->bufbase_ = stream->tinybuf_;
stream->bufsize_ = 1;
}
} else {
/* The caller has specified a buffer to employ. */
/* The buffer must be large enough to accommodate maximum
putback. */
assert(bufsize > JAS_STREAM_MAXPUTBACK);
stream->bufbase_ = JAS_CAST(uchar *, buf);
stream->bufsize_ = bufsize - JAS_STREAM_MAXPUTBACK;
}
} else {
/* The unbuffered mode is being employed. */
/* A buffer should not have been supplied by the caller. */
assert(!buf);
/* Use a trivial one-character buffer. */
stream->bufbase_ = stream->tinybuf_;
stream->bufsize_ = 1;
}
stream->bufstart_ = &stream->bufbase_[JAS_STREAM_MAXPUTBACK];
stream->ptr_ = stream->bufstart_;
stream->cnt_ = 0;
stream->bufmode_ |= bufmode & JAS_STREAM_BUFMODEMASK;
}
static int qiodevice_read(jas_stream_obj_t *obj, char *buf, int cnt)
{
QIODevice *io = (QIODevice*) obj;
return io->read(buf, cnt);
}
static int qiodevice_write(jas_stream_obj_t *obj, char *buf, int cnt)
{
QIODevice *io = (QIODevice*) obj;
return io->write(buf, cnt);
}
static long qiodevice_seek(jas_stream_obj_t *obj, long offset, int origin)
{
QIODevice *io = (QIODevice*) obj;
long newpos;
switch (origin) {
case SEEK_SET:
newpos = offset;
break;
case SEEK_END:
newpos = io->size() - offset;
break;
case SEEK_CUR:
newpos = io->pos() + offset;
break;
default:
return -1;
}
if (newpos < 0) {
return -1;
}
if (io->seek(newpos))
return newpos;
else
return -1;
}
static int qiodevice_close(jas_stream_obj_t *)
{
return 0;
}
static jas_stream_ops_t jas_stream_qiodeviceops = {
qiodevice_read,
qiodevice_write,
qiodevice_seek,
qiodevice_close
};
static jas_stream_t *jas_stream_qiodevice(QIODevice *iodevice)
{
jas_stream_t *stream;
if (!iodevice) return 0;
if (!(stream = jas_stream_create())) {
return 0;
}
/* A stream associated with a memory buffer is always opened
for both reading and writing in binary mode. */
stream->openmode_ = JAS_STREAM_READ | JAS_STREAM_WRITE | JAS_STREAM_BINARY;
jas_stream_initbuf(stream, JAS_STREAM_FULLBUF, 0, 0);
/* Select the operations for a memory stream. */
stream->obj_ = (void *)iodevice;
stream->ops_ = &jas_stream_qiodeviceops;
return stream;
}
/************************ End of JasPer QIODevice stream ****************/
typedef struct {
jas_image_t* image;
int cmptlut[MAXCMPTS];
jas_image_t* altimage;
} gs_t;
static jas_image_t*
read_image(QIODevice* io)
{
jas_stream_t* in = 0;
in = jas_stream_qiodevice(io);
if (!in) return 0;
jas_image_t* image = jas_image_decode(in, -1, 0);
jas_stream_close(in);
// image may be 0, but that's Ok
return image;
} // read_image
static bool
convert_colorspace(gs_t& gs)
{
jas_cmprof_t *outprof = jas_cmprof_createfromclrspc(JAS_CLRSPC_SRGB);
if (!outprof) return false;
gs.altimage = jas_image_chclrspc(gs.image, outprof,
JAS_CMXFORM_INTENT_PER);
if (!gs.altimage) return false;
return true;
} // convert_colorspace
static bool
render_view(gs_t& gs, QImage* outImage)
{
if (!gs.altimage) return false;
QImage qti;
if ((gs.cmptlut[0] = jas_image_getcmptbytype(gs.altimage,
JAS_IMAGE_CT_COLOR(JAS_CLRSPC_CHANIND_RGB_R))) < 0 ||
(gs.cmptlut[1] = jas_image_getcmptbytype(gs.altimage,
JAS_IMAGE_CT_COLOR(JAS_CLRSPC_CHANIND_RGB_G))) < 0 ||
(gs.cmptlut[2] = jas_image_getcmptbytype(gs.altimage,
JAS_IMAGE_CT_COLOR(JAS_CLRSPC_CHANIND_RGB_B))) < 0) {
return false;
} // if
const int* cmptlut = gs.cmptlut;
int v[3];
// check that all components have the same size.
const int width = jas_image_cmptwidth(gs.altimage, cmptlut[0]);
const int height = jas_image_cmptheight(gs.altimage, cmptlut[0]);
for (int i = 1; i < 3; ++i) {
if (jas_image_cmptwidth(gs.altimage, cmptlut[i]) != width ||
jas_image_cmptheight(gs.altimage, cmptlut[i]) != height)
return false;
} // for
jas_matrix_t *cmptmatrix[3];
jas_seqent_t *buf[3];
int prec[3];
for (int k = 0; k < 3; ++k) {
prec[k] = jas_image_cmptprec(gs.altimage, cmptlut[k]);
if (!(cmptmatrix[k] = jas_matrix_create(1, width))) {
return false;
}
}
qti = QImage(jas_image_width(gs.altimage), jas_image_height(gs.altimage),
QImage::Format_RGB32);
if (qti.isNull()) {
return false;
}
uint32_t* data = (uint32_t*)qti.bits();
for (int y = 0; y < height; ++y) {
for (int k = 0; k < 3; ++k) {
if (jas_image_readcmpt(gs.altimage, cmptlut[k], 0, y, width, 1, cmptmatrix[k])) {
return false;
}
buf[k] = jas_matrix_getref(cmptmatrix[k], 0, 0);
}
for (int x = 0; x < width; ++x) {
for (int k = 0; k < 3; ++k) {
v[k] = *buf[k];
// if the precision of the component is too small, increase
// it to use the complete value range.
v[k] <<= 8 - prec[k];
if (v[k] < 0) v[k] = 0;
else if (v[k] > 255) v[k] = 255;
++buf[k];
} // for k
*data++ = qRgb(v[0], v[1], v[2]);
} // for x
} // for y
for (int k = 0; k < 3; ++k) {
if (cmptmatrix[k]) {
jas_matrix_destroy(cmptmatrix[k]);
}
}
*outImage = qti;
return true;
} // render_view
static jas_image_t*
create_image(const QImage& qi)
{
// prepare the component parameters
jas_image_cmptparm_t* cmptparms = new jas_image_cmptparm_t[ 3 ];
for (int i = 0; i < 3; ++i) {
// x and y offset
cmptparms[i].tlx = 0;
cmptparms[i].tly = 0;
// the resulting image will be hstep*width x vstep*height !
cmptparms[i].hstep = 1;
cmptparms[i].vstep = 1;
cmptparms[i].width = qi.width();
cmptparms[i].height = qi.height();
// we write everything as 24bit truecolor ATM
cmptparms[i].prec = 8;
cmptparms[i].sgnd = false;
}
jas_image_t* ji = jas_image_create(3 /* number components */, cmptparms, JAS_CLRSPC_UNKNOWN);
delete[] cmptparms;
// returning 0 is ok
return ji;
} // create_image
static bool
write_components(jas_image_t* ji, const QImage& qi)
{
const unsigned height = qi.height();
const unsigned width = qi.width();
jas_matrix_t* m = jas_matrix_create(height, width);
if (!m) return false;
jas_image_setclrspc(ji, JAS_CLRSPC_SRGB);
jas_image_setcmpttype(ji, 0, JAS_IMAGE_CT_RGB_R);
for (uint y = 0; y < height; ++y)
for (uint x = 0; x < width; ++x)
jas_matrix_set(m, y, x, qRed(qi.pixel(x, y)));
jas_image_writecmpt(ji, 0, 0, 0, width, height, m);
jas_image_setcmpttype(ji, 1, JAS_IMAGE_CT_RGB_G);
for (uint y = 0; y < height; ++y)
for (uint x = 0; x < width; ++x)
jas_matrix_set(m, y, x, qGreen(qi.pixel(x, y)));
jas_image_writecmpt(ji, 1, 0, 0, width, height, m);
jas_image_setcmpttype(ji, 2, JAS_IMAGE_CT_RGB_B);
for (uint y = 0; y < height; ++y)
for (uint x = 0; x < width; ++x)
jas_matrix_set(m, y, x, qBlue(qi.pixel(x, y)));
jas_image_writecmpt(ji, 2, 0, 0, width, height, m);
jas_matrix_destroy(m);
return true;
} // write_components
static bool
write_image(const QImage &image, QIODevice* io, int quality)
{
jas_stream_t* stream = 0;
stream = jas_stream_qiodevice(io);
// by here, a jas_stream_t is open
if (!stream) return false;
jas_image_t* ji = create_image(image);
if (!ji) {
jas_stream_close(stream);
return false;
} // if
if (!write_components(ji, image)) {
jas_stream_close(stream);
jas_image_destroy(ji);
return false;
} // if
// optstr:
// - rate=#B => the resulting file size is about # bytes
// - rate=0.0 .. 1.0 => the resulting file size is about the factor times
// the uncompressed size
// use sprintf for locale-aware string
char rateBuffer[16];
sprintf(rateBuffer, "rate=%.2g\n", (quality < 0) ? DEFAULT_RATE : quality / 100.0);
int i = jp2_encode(ji, stream, rateBuffer);
jas_image_destroy(ji);
jas_stream_close(stream);
if (i != 0) return false;
return true;
}
JP2Handler::JP2Handler()
{
quality = 75;
jas_init();
}
JP2Handler::~JP2Handler()
{
jas_cleanup();
}
bool JP2Handler::canRead() const
{
if (canRead(device())) {
setFormat("jp2");
return true;
}
return false;
}
bool JP2Handler::canRead(QIODevice *device)
{
if (!device) {
return false;
}
return device->peek(6) == QByteArray("\x00\x00\x00\x0C\x6A\x50", 6);
}
bool JP2Handler::read(QImage *image)
{
if (!canRead()) return false;
gs_t gs;
if (!(gs.image = read_image(device()))) return false;
if (!convert_colorspace(gs)) return false;
render_view(gs, image);
if (gs.image) jas_image_destroy(gs.image);
if (gs.altimage) jas_image_destroy(gs.altimage);
return true;
}
bool JP2Handler::write(const QImage &image)
{
return write_image(image, device(), quality);
}
bool JP2Handler::supportsOption(ImageOption option) const
{
return option == Quality;
}
QVariant JP2Handler::option(ImageOption option) const
{
if (option == Quality)
return quality;
return QVariant();
}
void JP2Handler::setOption(ImageOption option, const QVariant &value)
{
if (option == Quality)
quality = qBound(-1, value.toInt(), 100);
}
QImageIOPlugin::Capabilities JP2Plugin::capabilities(QIODevice *device, const QByteArray &format) const
{
if (format == "jp2")
return Capabilities(CanRead | CanWrite);
if (!format.isEmpty())
return 0;
if (!device->isOpen())
return 0;
Capabilities cap;
if (device->isReadable() && JP2Handler::canRead(device))
cap |= CanRead;
if (device->isWritable())
cap |= CanWrite;
return cap;
}
QImageIOHandler *JP2Plugin::create(QIODevice *device, const QByteArray &format) const
{
QImageIOHandler *handler = new JP2Handler;
handler->setDevice(device);
handler->setFormat(format);
return handler;
}

View File

@ -0,0 +1,7 @@
[Desktop Entry]
Type=Service
X-KDE-ServiceTypes=QImageIOPlugins
X-KDE-ImageFormat=jp2
X-KDE-MimeType=image/jp2
X-KDE-Read=true
X-KDE-Write=true

42
src/imageformats/jp2.h Normal file
View File

@ -0,0 +1,42 @@
/**
* QImageIO Routines to read/write JPEG2000 images.
* copyright (c) 2002 Michael Ritzert <michael@ritzert.de>
*
* This library is distributed under the conditions of the GNU LGPL.
*/
#ifndef KIMG_JP2_H
#define KIMG_JP2_H
#include <QImageIOPlugin>
class JP2Handler : public QImageIOHandler
{
public:
JP2Handler();
virtual ~JP2Handler();
virtual bool canRead() const;
virtual bool read(QImage *image);
virtual bool write(const QImage &image);
virtual bool supportsOption(ImageOption option) const;
virtual QVariant option(ImageOption option) const;
virtual void setOption(ImageOption option, const QVariant &value);
static bool canRead(QIODevice *device);
private:
int quality;
};
class JP2Plugin : public QImageIOPlugin
{
Q_OBJECT
Q_PLUGIN_METADATA(IID "org.qt-project.Qt.QImageIOHandlerFactoryInterface" FILE "jp2.json")
public:
virtual Capabilities capabilities(QIODevice *device, const QByteArray &format) const;
virtual QImageIOHandler *create(QIODevice *device, const QByteArray &format = QByteArray()) const;
};
#endif // KIMG_JP2_H

View File

@ -0,0 +1,4 @@
{
"Keys": [ "jp2" ],
"MimeTypes": [ "image/jp2" ]
}

657
src/imageformats/pcx.cpp Normal file
View File

@ -0,0 +1,657 @@
/* This file is part of the KDE project
Copyright (C) 2002-2005 Nadeem Hasan <nhasan@kde.org>
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License (LGPL) as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
*/
#include "pcx.h"
#include <QColor>
#include <QDataStream>
// #include <QDebug>
#include <QImage>
class RGB
{
public:
quint8 r;
quint8 g;
quint8 b;
static RGB from(const QRgb &color) {
RGB c;
c.r = qRed(color);
c.g = qGreen(color);
c.b = qBlue(color);
return c;
}
} Q_PACKED;
class Palette
{
public:
void setColor(int i, const QRgb color) {
RGB &c = rgb[ i ];
c.r = qRed(color);
c.g = qGreen(color);
c.b = qBlue(color);
}
QRgb color(int i) const {
return qRgb(rgb[ i ].r, rgb[ i ].g, rgb[ i ].b);
}
class RGB rgb[ 16 ];
} Q_PACKED;
class PCXHEADER
{
public:
PCXHEADER();
inline int width() const {
return (XMax - XMin) + 1;
}
inline int height() const {
return (YMax - YMin) + 1;
}
inline bool isCompressed() const {
return (Encoding == 1);
}
quint8 Manufacturer; // Constant Flag, 10 = ZSoft .pcx
quint8 Version; // Version information·
// 0 = Version 2.5 of PC Paintbrush·
// 2 = Version 2.8 w/palette information·
// 3 = Version 2.8 w/o palette information·
// 4 = PC Paintbrush for Windows(Plus for
// Windows uses Ver 5)·
// 5 = Version 3.0 and > of PC Paintbrush
// and PC Paintbrush +, includes
// Publisher's Paintbrush . Includes
// 24-bit .PCX files·
quint8 Encoding; // 1 = .PCX run length encoding
quint8 Bpp; // Number of bits to represent a pixel
// (per Plane) - 1, 2, 4, or 8·
quint16 XMin;
quint16 YMin;
quint16 XMax;
quint16 YMax;
quint16 HDpi;
quint16 YDpi;
Palette ColorMap;
quint8 Reserved; // Should be set to 0.
quint8 NPlanes; // Number of color planes
quint16 BytesPerLine; // Number of bytes to allocate for a scanline
// plane. MUST be an EVEN number. Do NOT
// calculate from Xmax-Xmin.·
quint16 PaletteInfo; // How to interpret palette- 1 = Color/BW,
// 2 = Grayscale ( ignored in PB IV/ IV + )·
quint16 HScreenSize; // Horizontal screen size in pixels. New field
// found only in PB IV/IV Plus
quint16 VScreenSize; // Vertical screen size in pixels. New field
// found only in PB IV/IV Plus
} Q_PACKED;
static QDataStream &operator>>(QDataStream &s, RGB &rgb)
{
quint8 r, g, b;
s >> r >> g >> b;
rgb.r = r;
rgb.g = g;
rgb.b = b;
return s;
}
static QDataStream &operator>>(QDataStream &s, Palette &pal)
{
for (int i = 0; i < 16; ++i)
s >> pal.rgb[ i ];
return s;
}
static QDataStream &operator>>(QDataStream &s, PCXHEADER &ph)
{
quint8 m, ver, enc, bpp;
s >> m >> ver >> enc >> bpp;
ph.Manufacturer = m;
ph.Version = ver;
ph.Encoding = enc;
ph.Bpp = bpp;
quint16 xmin, ymin, xmax, ymax;
s >> xmin >> ymin >> xmax >> ymax;
ph.XMin = xmin;
ph.YMin = ymin;
ph.XMax = xmax;
ph.YMax = ymax;
quint16 hdpi, ydpi;
s >> hdpi >> ydpi;
ph.HDpi = hdpi;
ph.YDpi = ydpi;
Palette colorMap;
quint8 res, np;
s >> colorMap >> res >> np;
ph.ColorMap = colorMap;
ph.Reserved = res;
ph.NPlanes = np;
quint16 bytesperline;
s >> bytesperline; ph.BytesPerLine = bytesperline;
quint16 paletteinfo;
s >> paletteinfo; ph.PaletteInfo = paletteinfo;
quint16 hscreensize, vscreensize;
s >> hscreensize; ph.HScreenSize = hscreensize;
s >> vscreensize; ph.VScreenSize = vscreensize;
// Skip the rest of the header
quint8 byte;
while (s.device()->pos() < 128)
s >> byte;
return s;
}
static QDataStream &operator<<(QDataStream &s, const RGB &rgb)
{
s << rgb.r << rgb.g << rgb.b;
return s;
}
static QDataStream &operator<<(QDataStream &s, const Palette &pal)
{
for (int i = 0; i < 16; ++i)
s << pal.rgb[ i ];
return s;
}
static QDataStream &operator<<(QDataStream &s, const PCXHEADER &ph)
{
s << ph.Manufacturer;
s << ph.Version;
s << ph.Encoding;
s << ph.Bpp;
s << ph.XMin << ph.YMin << ph.XMax << ph.YMax;
s << ph.HDpi << ph.YDpi;
s << ph.ColorMap;
s << ph.Reserved;
s << ph.NPlanes;
s << ph.BytesPerLine;
s << ph.PaletteInfo;
s << ph.HScreenSize;
s << ph.VScreenSize;
quint8 byte = 0;
for (int i = 0; i < 54; ++i)
s << byte;
return s;
}
PCXHEADER::PCXHEADER()
{
// Initialize all data to zero
QByteArray dummy(128, 0);
dummy.fill(0);
QDataStream s(&dummy, QIODevice::ReadOnly);
s >> *this;
}
static void readLine(QDataStream &s, QByteArray &buf, const PCXHEADER &header)
{
quint32 i = 0;
quint32 size = buf.size();
quint8 byte, count;
if (header.isCompressed()) {
// Uncompress the image data
while (i < size) {
count = 1;
s >> byte;
if (byte > 0xc0) {
count = byte - 0xc0;
s >> byte;
}
while (count-- && i < size)
buf[ i++ ] = byte;
}
} else {
// Image is not compressed (possible?)
while (i < size) {
s >> byte;
buf[ i++ ] = byte;
}
}
}
static void readImage1(QImage &img, QDataStream &s, const PCXHEADER &header)
{
QByteArray buf(header.BytesPerLine, 0);
img = QImage(header.width(), header.height(), QImage::Format_Mono);
img.setColorCount(2);
for (int y = 0; y < header.height(); ++y) {
if (s.atEnd()) {
img = QImage();
return;
}
readLine(s, buf, header);
uchar *p = img.scanLine(y);
unsigned int bpl = qMin((quint16)((header.width() + 7) / 8), header.BytesPerLine);
for (unsigned int x = 0; x < bpl; ++x)
p[ x ] = buf[x];
}
// Set the color palette
img.setColor(0, qRgb(0, 0, 0));
img.setColor(1, qRgb(255, 255, 255));
}
static void readImage4(QImage &img, QDataStream &s, const PCXHEADER &header)
{
QByteArray buf(header.BytesPerLine * 4, 0);
QByteArray pixbuf(header.width(), 0);
img = QImage(header.width(), header.height(), QImage::Format_Indexed8);
img.setColorCount(16);
for (int y = 0; y < header.height(); ++y) {
if (s.atEnd()) {
img = QImage();
return;
}
pixbuf.fill(0);
readLine(s, buf, header);
for (int i = 0; i < 4; i++) {
quint32 offset = i * header.BytesPerLine;
for (int x = 0; x < header.width(); ++x)
if (buf[ offset + (x / 8) ] & (128 >> (x % 8)))
pixbuf[ x ] = (int)(pixbuf[ x ]) + (1 << i);
}
uchar *p = img.scanLine(y);
for (int x = 0; x < header.width(); ++x)
p[ x ] = pixbuf[ x ];
}
// Read the palette
for (int i = 0; i < 16; ++i)
img.setColor(i, header.ColorMap.color(i));
}
static void readImage8(QImage &img, QDataStream &s, const PCXHEADER &header)
{
QByteArray buf(header.BytesPerLine, 0);
img = QImage(header.width(), header.height(), QImage::Format_Indexed8);
img.setColorCount(256);
for (int y = 0; y < header.height(); ++y) {
if (s.atEnd()) {
img = QImage();
return;
}
readLine(s, buf, header);
uchar *p = img.scanLine(y);
unsigned int bpl = qMin(header.BytesPerLine, (quint16)header.width());
for (unsigned int x = 0; x < bpl; ++x)
p[ x ] = buf[ x ];
}
quint8 flag;
s >> flag;
// qDebug() << "Palette Flag: " << flag;
if (flag == 12 && (header.Version == 5 || header.Version == 2)) {
// Read the palette
quint8 r, g, b;
for (int i = 0; i < 256; ++i) {
s >> r >> g >> b;
img.setColor(i, qRgb(r, g, b));
}
}
}
static void readImage24(QImage &img, QDataStream &s, const PCXHEADER &header)
{
QByteArray r_buf(header.BytesPerLine, 0);
QByteArray g_buf(header.BytesPerLine, 0);
QByteArray b_buf(header.BytesPerLine, 0);
img = QImage(header.width(), header.height(), QImage::Format_RGB32);
for (int y = 0; y < header.height(); ++y) {
if (s.atEnd()) {
img = QImage();
return;
}
readLine(s, r_buf, header);
readLine(s, g_buf, header);
readLine(s, b_buf, header);
uint *p = (uint *)img.scanLine(y);
for (int x = 0; x < header.width(); ++x)
p[ x ] = qRgb(r_buf[ x ], g_buf[ x ], b_buf[ x ]);
}
}
static void writeLine(QDataStream &s, QByteArray &buf)
{
quint32 i = 0;
quint32 size = buf.size();
quint8 count, data;
char byte;
while (i < size) {
count = 1;
byte = buf[ i++ ];
while ((i < size) && (byte == buf[ i ]) && (count < 63)) {
++i;
++count;
}
data = byte;
if (count > 1 || data >= 0xc0) {
count |= 0xc0;
s << count;
}
s << data;
}
}
static void writeImage1(QImage &img, QDataStream &s, PCXHEADER &header)
{
img = img.convertToFormat(QImage::Format_Mono);
header.Bpp = 1;
header.NPlanes = 1;
header.BytesPerLine = img.bytesPerLine();
s << header;
QByteArray buf(header.BytesPerLine, 0);
for (int y = 0; y < header.height(); ++y) {
quint8 *p = img.scanLine(y);
// Invert as QImage uses reverse palette for monochrome images?
for (int i = 0; i < header.BytesPerLine; ++i)
buf[ i ] = ~p[ i ];
writeLine(s, buf);
}
}
static void writeImage4(QImage &img, QDataStream &s, PCXHEADER &header)
{
header.Bpp = 1;
header.NPlanes = 4;
header.BytesPerLine = header.width() / 8;
for (int i = 0; i < 16; ++i)
header.ColorMap.setColor(i, img.color(i));
s << header;
QByteArray buf[ 4 ];
for (int i = 0; i < 4; ++i)
buf[ i ].resize(header.BytesPerLine);
for (int y = 0; y < header.height(); ++y) {
quint8 *p = img.scanLine(y);
for (int i = 0; i < 4; ++i)
buf[ i ].fill(0);
for (int x = 0; x < header.width(); ++x) {
for (int i = 0; i < 4; ++i)
if (*(p + x) & (1 << i))
buf[ i ][ x / 8 ] = (int)(buf[ i ][ x / 8 ]) | 1 << (7 - x % 8);
}
for (int i = 0; i < 4; ++i)
writeLine(s, buf[ i ]);
}
}
static void writeImage8(QImage &img, QDataStream &s, PCXHEADER &header)
{
header.Bpp = 8;
header.NPlanes = 1;
header.BytesPerLine = img.bytesPerLine();
s << header;
QByteArray buf(header.BytesPerLine, 0);
for (int y = 0; y < header.height(); ++y) {
quint8 *p = img.scanLine(y);
for (int i = 0; i < header.BytesPerLine; ++i)
buf[ i ] = p[ i ];
writeLine(s, buf);
}
// Write palette flag
quint8 byte = 12;
s << byte;
// Write palette
for (int i = 0; i < 256; ++i)
s << RGB::from(img.color(i));
}
static void writeImage24(QImage &img, QDataStream &s, PCXHEADER &header)
{
header.Bpp = 8;
header.NPlanes = 3;
header.BytesPerLine = header.width();
s << header;
QByteArray r_buf(header.width(), 0);
QByteArray g_buf(header.width(), 0);
QByteArray b_buf(header.width(), 0);
for (int y = 0; y < header.height(); ++y) {
uint *p = (uint *)img.scanLine(y);
for (int x = 0; x < header.width(); ++x) {
QRgb rgb = *p++;
r_buf[ x ] = qRed(rgb);
g_buf[ x ] = qGreen(rgb);
b_buf[ x ] = qBlue(rgb);
}
writeLine(s, r_buf);
writeLine(s, g_buf);
writeLine(s, b_buf);
}
}
PCXHandler::PCXHandler()
{
}
bool PCXHandler::canRead() const
{
if (canRead(device())) {
setFormat("pcx");
return true;
}
return false;
}
bool PCXHandler::read(QImage *outImage)
{
QDataStream s(device());
s.setByteOrder(QDataStream::LittleEndian);
if (s.device()->size() < 128) {
return false;
}
PCXHEADER header;
s >> header;
if (header.Manufacturer != 10 || s.atEnd()) {
return false;
}
// int w = header.width();
// int h = header.height();
// qDebug() << "Manufacturer: " << header.Manufacturer;
// qDebug() << "Version: " << header.Version;
// qDebug() << "Encoding: " << header.Encoding;
// qDebug() << "Bpp: " << header.Bpp;
// qDebug() << "Width: " << w;
// qDebug() << "Height: " << h;
// qDebug() << "Window: " << header.XMin << "," << header.XMax << ","
// << header.YMin << "," << header.YMax << endl;
// qDebug() << "BytesPerLine: " << header.BytesPerLine;
// qDebug() << "NPlanes: " << header.NPlanes;
QImage img;
if (header.Bpp == 1 && header.NPlanes == 1) {
readImage1(img, s, header);
} else if (header.Bpp == 1 && header.NPlanes == 4) {
readImage4(img, s, header);
} else if (header.Bpp == 8 && header.NPlanes == 1) {
readImage8(img, s, header);
} else if (header.Bpp == 8 && header.NPlanes == 3) {
readImage24(img, s, header);
}
// qDebug() << "Image Bytes: " << img.numBytes();
// qDebug() << "Image Bytes Per Line: " << img.bytesPerLine();
// qDebug() << "Image Depth: " << img.depth();
if (!img.isNull()) {
*outImage = img;
return true;
} else {
return false;
}
}
bool PCXHandler::write(const QImage &image)
{
QDataStream s(device());
s.setByteOrder(QDataStream::LittleEndian);
QImage img = image;
int w = img.width();
int h = img.height();
// qDebug() << "Width: " << w;
// qDebug() << "Height: " << h;
// qDebug() << "Depth: " << img.depth();
// qDebug() << "BytesPerLine: " << img.bytesPerLine();
// qDebug() << "Color Count: " << img.colorCount();
PCXHEADER header;
header.Manufacturer = 10;
header.Version = 5;
header.Encoding = 1;
header.XMin = 0;
header.YMin = 0;
header.XMax = w - 1;
header.YMax = h - 1;
header.HDpi = 300;
header.YDpi = 300;
header.Reserved = 0;
header.PaletteInfo = 1;
if (img.depth() == 1) {
writeImage1(img, s, header);
} else if (img.depth() == 8 && img.colorCount() <= 16) {
writeImage4(img, s, header);
} else if (img.depth() == 8) {
writeImage8(img, s, header);
} else if (img.depth() == 32) {
writeImage24(img, s, header);
}
return true;
}
bool PCXHandler::canRead(QIODevice *device)
{
if (!device) {
qWarning("PCXHandler::canRead() called with no device");
return false;
}
qint64 oldPos = device->pos();
char head[1];
qint64 readBytes = device->read(head, sizeof(head));
if (readBytes != sizeof(head)) {
if (device->isSequential()) {
while (readBytes > 0)
device->ungetChar(head[readBytes-- - 1]);
} else {
device->seek(oldPos);
}
return false;
}
if (device->isSequential()) {
while (readBytes > 0)
device->ungetChar(head[readBytes-- - 1]);
} else {
device->seek(oldPos);
}
return qstrncmp(head, "\012", 1) == 0;
}
QImageIOPlugin::Capabilities PCXPlugin::capabilities(QIODevice *device, const QByteArray &format) const
{
if (format == "pcx")
return Capabilities(CanRead | CanWrite);
if (!format.isEmpty())
return 0;
if (!device->isOpen())
return 0;
Capabilities cap;
if (device->isReadable() && PCXHandler::canRead(device))
cap |= CanRead;
if (device->isWritable())
cap |= CanWrite;
return cap;
}
QImageIOHandler *PCXPlugin::create(QIODevice *device, const QByteArray &format) const
{
QImageIOHandler *handler = new PCXHandler;
handler->setDevice(device);
handler->setFormat(format);
return handler;
}

View File

@ -0,0 +1,7 @@
[Desktop Entry]
Type=Service
X-KDE-ServiceTypes=QImageIOPlugins
X-KDE-ImageFormat=pcx
X-KDE-MimeType=image/x-pcx
X-KDE-Read=true
X-KDE-Write=true

37
src/imageformats/pcx.h Normal file
View File

@ -0,0 +1,37 @@
/* This file is part of the KDE project
Copyright (C) 2002-2003 Nadeem Hasan <nhasan@kde.org>
This program is free software; you can redistribute it and/or
modify it under the terms of the Lesser GNU General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
*/
#ifndef KIMG_PCX_H
#define KIMG_PCX_H
#include <QImageIOPlugin>
class PCXHandler : public QImageIOHandler
{
public:
PCXHandler();
virtual bool canRead() const;
virtual bool read(QImage *image);
virtual bool write(const QImage &image);
static bool canRead(QIODevice *device);
};
class PCXPlugin : public QImageIOPlugin
{
Q_OBJECT
Q_PLUGIN_METADATA(IID "org.qt-project.Qt.QImageIOHandlerFactoryInterface" FILE "pcx.json")
public:
virtual Capabilities capabilities(QIODevice *device, const QByteArray &format) const;
virtual QImageIOHandler *create(QIODevice *device, const QByteArray &format = QByteArray()) const;
};
#endif // KIMG_PCX_H

View File

@ -0,0 +1,4 @@
{
"Keys": [ "pcx" ],
"MimeTypes": [ "image/x-pcx" ]
}

103
src/imageformats/pic.cpp Normal file
View File

@ -0,0 +1,103 @@
/**
* PIC_RW - Qt PIC Support
* Copyright (C) 2007 Ruben Lopez <r.lopez@bren.es>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
* ----------------------------------------------------------------------------
*/
#include "pic.h"
#include "pic_rw.h"
#include <QVariant>
#include <QImage>
#include <iostream>
bool SoftimagePICHandler::canRead() const
{
if (!SoftimagePICHandler::canRead(device())) {
return false;
}
setFormat("pic");
return true;
}
bool SoftimagePICHandler::read(QImage *image)
{
pic_read(device(), image);
return true;
}
bool SoftimagePICHandler::write(const QImage &image)
{
pic_write(device(), &image);
return true;
}
bool SoftimagePICHandler::canRead(QIODevice *device)
{
PICHeader hdr;
if (picReadHeader(device, &hdr, true)) {
if (strncmp(hdr.id, "PICT", 4) == 0) {
return true;
}
}
return false;
}
QVariant SoftimagePICHandler::option(ImageOption option) const
{
if (option == Size) {
PICHeader hdr;
if (picReadHeader(device(), &hdr, true)) {
return QSize(hdr.width, hdr.height);
} else {
return QSize(-1, -1);
}
}
return QVariant();
}
bool SoftimagePICHandler::supportsOption(ImageOption option) const
{
return (option == Size);
}
QImageIOPlugin::Capabilities SoftimagePICPlugin::capabilities(QIODevice *device, const QByteArray &format) const
{
if (format == "pic")
return Capabilities(CanRead | CanWrite);
if (!format.isEmpty())
return 0;
if (!device->isOpen())
return 0;
Capabilities cap;
if (device->isReadable() && SoftimagePICHandler::canRead(device)) {
cap |= CanRead;
}
if (device->isWritable()) {
cap |= CanWrite;
}
return cap;
}
QImageIOHandler * SoftimagePICPlugin::create(QIODevice *device, const QByteArray &format) const
{
QImageIOHandler * handler = new SoftimagePICHandler();
handler->setDevice(device);
handler->setFormat(format);
return handler;
}

View File

@ -0,0 +1,7 @@
[Desktop Entry]
Type=Service
X-KDE-ServiceTypes=QImageIOPlugins
X-KDE-ImageFormat=pic
X-KDE-MimeType=image/x-pic
X-KDE-Read=true
X-KDE-Write=true

49
src/imageformats/pic.h Normal file
View File

@ -0,0 +1,49 @@
/**
* PIC_RW - Qt PIC Support
* Copyright (C) 2007 Ruben Lopez <r.lopez@bren.es>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
* ----------------------------------------------------------------------------
*/
#ifndef KIMG_PIC_H
#define KIMG_PIC_H
#include <QImageIOPlugin>
class SoftimagePICHandler : public QImageIOHandler
{
public:
virtual bool canRead() const;
virtual bool read(QImage * image);
virtual bool write(const QImage &);
virtual QVariant option(ImageOption option) const;
virtual bool supportsOption(ImageOption option) const;
static bool canRead(QIODevice *device);
};
class SoftimagePICPlugin : public QImageIOPlugin
{
Q_OBJECT
Q_PLUGIN_METADATA(IID "org.qt-project.Qt.QImageIOHandlerFactoryInterface" FILE "pic.json")
public:
virtual Capabilities capabilities(QIODevice *device, const QByteArray &format) const;
virtual QImageIOHandler *create(QIODevice *device, const QByteArray &format = QByteArray()) const;
};
#endif // KIMG_PIC_H

View File

@ -0,0 +1,4 @@
{
"Keys": [ "pic" ],
"MimeTypes": [ "image/x-pic" ]
}

View File

@ -0,0 +1,292 @@
/**
* PIC_RW - Qt PIC Support
* Copyright (C) 2007 Ruben Lopez <r.lopez@bren.es>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
* ----------------------------------------------------------------------------
*/
/* This code is based on the GIMP-PIC plugin by Halfdan Ingvarsson,
* and relicensed from GPL to LGPL to accommodate the KDE licensing policy
* with his permission.
* These is the original copyright:
* Copyright (C) 1998 Halfdan Ingvarsson
*/
#include "pic_rw.h"
#include <netinet/in.h>
#include <iostream>
#include <qimage.h>
#include <algorithm>
/**
* Reads the PIC header and checks that it is OK
* @param dev The QT device to read from
* @param hdr A pointer to the PIC header
* @param peek Keep bytes in the device
* @return true on success
*/
bool picReadHeader(QIODevice *dev, PICHeader *hdr, bool peek)
{
int result = 0;
if (peek) {
result = dev->peek((char*) hdr, HEADER_SIZE);
} else {
result = dev->read((char*) hdr, HEADER_SIZE);
}
hdr->magic = ntohl(hdr->magic);
hdr->width = ntohs(hdr->width);
hdr->height = ntohs(hdr->height);
hdr->fields = ntohs(hdr->fields);
if (hdr->magic != PIC_MAGIC_NUMBER || strncmp(hdr->id, "PICT", 4)) {
return false;
}
return result == HEADER_SIZE;
}
#define CHANNEL_BYTE(ch, mask) (( ch & mask) ? 1 : 0)
/**
* Gets the channels definition and returns the number of bytes per pixel
* @param channels The channels bitfield
* @return The number of bytes per pixel
*/
static int channels2bpp(char channels)
{
return CHANNEL_BYTE(channels, RED)
+ CHANNEL_BYTE(channels, GREEN)
+ CHANNEL_BYTE(channels, BLUE)
+ CHANNEL_BYTE(channels, ALPHA);
}
/**
* Reads the channels info
* @param dev The QT device to read from
* @param channels A pointer to 8 channels
* @return true on success
*/
static bool readChannels(QIODevice *dev, PICChannel *channels, int &bpp)
{
int c = 0;
memset(channels, 0, sizeof(PICChannel) * 8);
do {
int result = dev->read((char*) & channels[c], CHANNEL_SIZE);
if (result != CHANNEL_SIZE) {
return false;
} else {
bpp += channels2bpp(channels[c].channel);
c++;
}
} while (channels[c - 1].chained);
return true;
}
/**
* Makes a component map based on the channels info
* @param channels The channel information
* @param cmap The component map to be built
*/
inline static void makeComponentMap(unsigned channel, unsigned char *cmap)
{
std::fill(cmap, cmap + 8, 0);
unsigned compos[] = {ALPHA, BLUE, GREEN, RED};
unsigned rgba[] = {3, 2, 1, 0};
unsigned pos = 0;
for (unsigned compo = 0; compo < 4; compo++) {
if (CHANNEL_BYTE(channel, compos[compo])) {
cmap[pos++] = rgba[compo];
}
}
}
/**
* Converts a PIC pixel to 32bits RGBA
* @param src_pixel The source PIC pixel as readed from file
* @param target_pixel The target buffer where to write the pixel info
* @param cmap The component map that maps each component in PIC format to RGBA format
* @param components The number of components in the source pixel
*/
inline static void pic2RGBA(unsigned char *src_pixel, unsigned char *target_pixel, unsigned char *cmap, unsigned components)
{
for (unsigned i = 0; i < components; i++) {
target_pixel[cmap[i]] = src_pixel[i];
}
}
/**
* Counts the number of channels in the PICChannel header
* @param channels The header
* @return The number of used channels
*/
inline static unsigned getNumChannels(PICChannel *channels)
{
unsigned result = 0;
for (unsigned i = 0; i < 8; i++) {
if (channels[i].channel != 0) {
result++;
} else {
return result;
}
}
return result;
}
/**
* Decodes a Run-length encoded chunk
* @param dev The device to read from
* @param row The row pointer to write to
* @param max The maximum length to write
* @param channels The channels header
* @return The number of generated pixels
*/
static int decodeRLE(QIODevice *dev, void *row, unsigned max, unsigned bpp, unsigned channels)
{
unsigned char buf[512];
unsigned *ptr = (unsigned *) row;
unsigned char component_map[8];
unsigned len = 0;
makeComponentMap(channels, component_map);
if (dev->read((char*) buf, 1) != 1) {
return -1;
}
/* If last bit is 1, then it is 2 to 127 repetitions */
if (buf[0] > 128) {
len = buf[0] - 127;
if (len > max) {
return -1;
}
unsigned count = dev->read((char*) buf, bpp);
if (count != bpp) {
return -1;
}
for (unsigned i = 0; i < len; i++) {
pic2RGBA(buf, (unsigned char*)(ptr + i), component_map, bpp);
}
} /* If the value is exactly 10000000, it means that it is more than 127 repetitions */
else if (buf[0] == 128) {
unsigned count = dev->read((char*) buf, bpp + 2);
if (count != bpp + 2) {
return -1;
}
len = (buf[0] << 8) | buf[1];
if (len > max) {
return -1;
}
for (unsigned i = 0; i < len; i++) {
pic2RGBA(buf + 2, (unsigned char*)(ptr + i), component_map, bpp);
}
} /** No repetitions */
else {
len = buf[0] + 1;
if (len > max) {
return -1;
}
unsigned count = dev->read((char*) buf, len * bpp);
if (count != len * bpp) {
return -1;
}
for (unsigned i = 0; i < len; i++) {
pic2RGBA(buf + (i * bpp), (unsigned char*)(ptr + i), component_map, bpp);
}
}
return len;
}
/**
* Reads a row from the file
* @param dev The device to read from
* @param row The row pointer to write to
* @param width The image width
* @param bpp The bytes per pixel
* @param channels The channels header info
*/
static bool readRow(QIODevice *dev, unsigned *row, unsigned width, PICChannel *channels)
{
for (int c = 0; channels[c].channel != 0; c++) {
unsigned remain = width;
unsigned bpp = channels2bpp(channels[c].channel);
if (channels[c].type == (int) RLE) {
unsigned *rowpos = row;
while (remain > 0) {
int readed = decodeRLE(dev, rowpos, remain, bpp, channels[c].channel);
if (readed < 0) {
return false;
}
remain -= readed;
rowpos += readed;
}
} else {
unsigned char component_map[8];
unsigned count = dev->read((char*) row, width * bpp);
if (count != width * bpp) {
return false;
}
makeComponentMap(channels[c].channel, component_map);
for (unsigned i = 0; i < width; i++) {
pic2RGBA(((unsigned char*) row) + (i * bpp), (unsigned char*)(row + i), component_map, bpp);
}
}
}
return true;
}
#define FAIL() { \
std::cout << "ERROR Reading PIC!" << std::endl; \
return; \
}
bool hasAlpha(PICChannel *channels)
{
int channel = 0;
do {
if (CHANNEL_BYTE(channels[channel].channel, ALPHA)) {
return true;
}
channel++;
} while (channels[channel - 1].chained);
return false;
}
/**
* KDE image reading function. Must have this exact name in order to work
*/
void pic_read(QIODevice *dev, QImage *result)
{
PICHeader header;
PICChannel channels[8];
int bpp = 0;
if (!picReadHeader(dev, &header) || !readChannels(dev, channels, bpp)) {
FAIL();
}
QImage img(header.width, header.height, QImage::Format_ARGB32);
for (int r = 0; r < header.height; r++) {
unsigned *row = (unsigned*) img.scanLine(r);
std::fill(row, row + header.width, 0);
if (!readRow(dev, row, header.width, channels)) {
FAIL();
}
}
// img->setAlphaBuffer(hasAlpha(channels));
*result = img;
}

110
src/imageformats/pic_rw.h Normal file
View File

@ -0,0 +1,110 @@
/**
* PIC_RW - Qt PIC Support
* Copyright (C) 2007 Ruben Lopez <r.lopez@bren.es>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
* ----------------------------------------------------------------------------
*/
/* This code is based on the GIMP-PIC plugin by Halfdan Ingvarsson,
* and relicensed from GPL to LGPL to accommodate the KDE licensing policy
* with his permission.
* These is the original copyright:
* Copyright (C) 1998 Halfdan Ingvarsson
*/
#ifndef __PIC_RW_H__
#define __PIC_RW_H__
#define PIC_MAGIC_NUMBER 0x5380f634
#include <QtCore/QFile>
#include <QImageIOPlugin>
#include <QColor>
/**
* How fields are distributed over the image
*/
typedef enum {
NONE = 0, /* No picture */
ODD = 1, /* Odd scanlines */
EVEN = 2, /* Even scanlines */
BOTH = 3 /* Every scanline */
} PICFields;
/**
* Type of a channel
*/
typedef enum {
UNCOMPRESSED = 0, /* Image is uncompressed */
RLE = 2 /* Run length compression */
} PICChannelType;
/**
* Channel codes
*/
typedef enum {
RED = 0x80, /* Red channel */
GREEN = 0x40, /* Green channel */
BLUE = 0x20, /* Blue channel */
ALPHA = 0x10 /* Alpha channel */
} PICChannelCode;
/**
* PIC format header
*/
typedef struct {
qint32 magic; /* PIC_MAGIC_NUMBER */
float version; /* Version of format */
char comment[80]; /* Prototype description */
char id[4]; /* "PICT" */
qint16 width; /* Image width, in pixels */
qint16 height; /* Image height, in pixels */
float ratio; /* Pixel aspect ratio */
qint16 fields; /* Picture field type */
qint16 pad; /* Unused */
} PICHeader;
/**
* PIC channel header
*/
typedef struct {
char chained; /* 1 if another packet follows, else 0 */
char size; /* Bits per pixel by channel */
char type; /* RLE or uncompressed */
char channel; /* Channel code (which planes are affected by this channel) */
} PICChannel;
#define HEADER_SIZE sizeof(PICHeader)
#define CHANNEL_SIZE sizeof(PICChannel)
/**
* Reads the PIC header and checks that it is OK
* @param dev The QT device to read from
* @param hdr A pointer to the PIC header
* @param peek Keep bytes in the device
* @return true on success
*/
bool picReadHeader(QIODevice *dev, PICHeader *hdr, bool peek = false);
/// Pic read handler for Qt / KDE
void pic_read(QIODevice *dev, QImage *img);
/// Pic write handler for Qt / KDE
void pic_write(QIODevice *dev, const QImage *img);
#endif//__PIC_RW_H__

View File

@ -0,0 +1,228 @@
/**
* PIC_RW - Qt PIC Support
* Copyright (C) 2007 Ruben Lopez <r.lopez@bren.es>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
* ----------------------------------------------------------------------------
*/
/* This code is based on the GIMP-PIC plugin by Halfdan Ingvarsson,
* and relicensed from GPL to LGPL to accommodate the KDE licensing policy
* with his permission.
* These is the original copyright:
* Copyright (C) 1998 Halfdan Ingvarsson
*/
#include "pic_rw.h"
#include <netinet/in.h>
#include <iostream>
#include <qimage.h>
/**
* Writes the PIC header info.
* @param dev IO Device
* @param msg Header message
* @param width Image width
* @param height Image height
* @param alpha Image has alpha?
* @return True on success
*/
static bool writeHeader(QIODevice *dev, std::string msg, unsigned width, unsigned height, bool alpha)
{
PICHeader h;
PICChannel c;
unsigned count = 0;
memset(&h, 0, sizeof(PICHeader));
h.magic = htonl(PIC_MAGIC_NUMBER);
h.version = 3.71f;
strcpy(h.comment, msg.c_str());
strncpy(h.id, "PICT", 4);
h.width = htons(width);
h.height = htons(height);
h.ratio = 1.0f;
h.fields = htons(BOTH);
count = dev->write((const char*) & h, sizeof(PICHeader));
if (count != sizeof(PICHeader)) {
return false;
}
memset(&c, 0, sizeof(PICChannel));
c.size = 8;
c.type = RLE;
c.channel = RED | GREEN | BLUE;
if (alpha) {
c.chained = 1;
}
count = dev->write((const char*) & c, sizeof(PICChannel));
if (count != sizeof(PICChannel)) {
return false;
}
if (alpha) {
c.channel = ALPHA;
c.chained = 0;
count = dev->write((const char*) & c, sizeof(PICChannel));
if (count != sizeof(PICChannel)) {
return false;
}
}
return true;
}
inline unsigned convertABGRtoRGBA(unsigned pixel)
{
unsigned r = pixel & 0xFF;
unsigned g = (pixel >> 8) & 0xFF;
unsigned b = (pixel >> 16) & 0xFF;
unsigned a = (pixel >> 24) & 0xFF;
return a | (b << 8) | (g << 16) | (r << 24);
}
/**
* Encodes a portion of the image in RLE coding
* @param image The image that we want to encode
* @param output The output buffer
* @param channels The number of channels to write
* @param offset Offset in bytes to copy
* @param max The maximum number of pixels to write
* @param oConsumed The number of pixels consumed from image
* @param oProduced The number of bytes produced in out
* @return True on success
*/
static bool encodeRLE(const unsigned *image, unsigned char *output, bool rgb, unsigned max, unsigned &oConsumed, unsigned &oProduced)
{
const unsigned *in = image;
unsigned char *out = output;
unsigned count = 0;
unsigned channels = 3;
unsigned offset = 1;
unsigned mask = 0x00FFFFFF;
if (!rgb) {
channels = 1;
offset = 0;
mask = 0xFF000000;
}
for (; (*in & mask) == (*image & mask) && count < 65536 && count < max; in++, count++) {
}
if (count > 127) {
/* Sequence of > 127 identical pixels */
*out++ = 128;
*out++ = count >> 8;
*out++ = count & 0xFF;
unsigned pixel = convertABGRtoRGBA(*image);
memcpy(out, ((char*) & pixel) + offset, channels);
out += channels;
oConsumed = count;
oProduced = out - output;
} else if (count > 1) {
/* Sequece of < 128 identical pixels */
*out++ = (count + 127);
unsigned pixel = convertABGRtoRGBA(*image);
memcpy(out, ((char*) & pixel) + offset, channels);
out += channels;
oConsumed = count;
oProduced = out - output;
} else {
in = image + 1;
unsigned previous = *image;
count = 0;
while ((*in & mask) != (previous & mask) && count < 128 && count < max) {
previous = *in;
in++;
count++;
}
// This only happens when it is the end of the row, and it is ok
if (count == 0) {
count = 1;
}
*out++ = (count - 1);
in = image;
for (unsigned c = 0; c < count; ++c) {
unsigned pixel = convertABGRtoRGBA(*in);
memcpy(out, ((char*) & pixel) + offset, channels);
out += channels;
in++;
}
oConsumed = count;
oProduced = out - output;
}
return true;
}
/**
* Writes a row to the file
* @return True on success
*/
static bool writeRow(QIODevice *dev, unsigned *row, unsigned width, bool alpha)
{
unsigned char *buf = new unsigned char[width * 4];
unsigned posIn = 0;
unsigned posOut = 0;
memset(buf, 0, width * 4);
unsigned consumed = 0;
unsigned produced = 0;
/* Write the RGB part of the scanline */
while (posIn < width) {
if (!encodeRLE(row + posIn, buf + posOut, true, width - posIn, consumed, produced)) {
delete[] buf;
return false;
}
posIn += consumed;
posOut += produced;
}
/* Write the alpha channel */
if (alpha) {
posIn = 0;
while (posIn < width) {
if (!encodeRLE(row + posIn, buf + posOut, false, width - posIn, consumed, produced)) {
delete[] buf;
return false;
}
posIn += consumed;
posOut += produced;
}
}
dev->write((const char*) buf, posOut);
delete[] buf;
return true;
}
#define FAIL() { \
std::cout << "ERROR Writing PIC!" << std::endl; \
return; \
}
/// Pic write handler for Qt / KDE
void pic_write(QIODevice *dev, const QImage *img)
{
bool alpha = img->hasAlphaChannel();
if (!writeHeader(dev, "Created with KDE", img->width(), img->height(), alpha)) {
FAIL();
}
for (int r = 0; r < img->height(); r++) {
unsigned *row = (unsigned*) img->scanLine(r);
if (!writeRow(dev, row, img->width(), alpha)) {
FAIL();
}
}
}

View File

@ -0,0 +1,7 @@
[Desktop Entry]
Type=Service
X-KDE-ServiceTypes=QImageIOPlugins
X-KDE-ImageFormat=pnm
X-KDE-MimeType=image/x-portable-anymap
X-KDE-Read=true
X-KDE-Write=false

330
src/imageformats/psd.cpp Normal file
View File

@ -0,0 +1,330 @@
/* This file is part of the KDE project
Copyright (C) 2003 Ignacio Castaño <castano@ludicon.com>
This program is free software; you can redistribute it and/or
modify it under the terms of the Lesser GNU General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
This code is based on Thacher Ulrich PSD loading code released
on public domain. See: http://tulrich.com/geekstuff/
*/
/* this code supports:
* reading:
* rle and raw psd files
* writing:
* not supported
*/
#include "psd.h"
#include <QImage>
#include <QtCore/QDataStream>
// #include <QDebug>
typedef quint32 uint;
typedef quint16 ushort;
typedef quint8 uchar;
namespace // Private.
{
enum ColorMode {
CM_BITMAP = 0,
CM_GRAYSCALE = 1,
CM_INDEXED = 2,
CM_RGB = 3,
CM_CMYK = 4,
CM_MULTICHANNEL = 7,
CM_DUOTONE = 8,
CM_LABCOLOR = 9
};
struct PSDHeader {
uint signature;
ushort version;
uchar reserved[6];
ushort channel_count;
uint height;
uint width;
ushort depth;
ushort color_mode;
};
static QDataStream & operator>> (QDataStream & s, PSDHeader & header)
{
s >> header.signature;
s >> header.version;
for (int i = 0; i < 6; i++) {
s >> header.reserved[i];
}
s >> header.channel_count;
s >> header.height;
s >> header.width;
s >> header.depth;
s >> header.color_mode;
return s;
}
static bool seekBy(QDataStream& s, unsigned int bytes)
{
char buf[4096];
while (bytes) {
unsigned int num = qMin(bytes, (unsigned int)sizeof(buf));
unsigned int l = num;
s.readRawData(buf, l);
if (l != num)
return false;
bytes -= num;
}
return true;
}
// Check that the header is a valid PSD.
static bool IsValid(const PSDHeader & header)
{
if (header.signature != 0x38425053) { // '8BPS'
return false;
}
return true;
}
// Check that the header is supported.
static bool IsSupported(const PSDHeader & header)
{
if (header.version != 1) {
return false;
}
if (header.channel_count > 16) {
return false;
}
if (header.depth != 8) {
return false;
}
if (header.color_mode != CM_RGB) {
return false;
}
return true;
}
// Load the PSD image.
static bool LoadPSD(QDataStream & s, const PSDHeader & header, QImage & img)
{
// Create dst image.
img = QImage(header.width, header.height, QImage::Format_RGB32);
uint tmp;
// Skip mode data.
s >> tmp;
s.device()->seek(s.device()->pos() + tmp);
// Skip image resources.
s >> tmp;
s.device()->seek(s.device()->pos() + tmp);
// Skip the reserved data.
s >> tmp;
s.device()->seek(s.device()->pos() + tmp);
// Find out if the data is compressed.
// Known values:
// 0: no compression
// 1: RLE compressed
ushort compression;
s >> compression;
if (compression > 1) {
// Unknown compression type.
return false;
}
uint channel_num = header.channel_count;
// Clear the image.
if (channel_num < 4) {
img.fill(qRgba(0, 0, 0, 0xFF));
} else {
// Enable alpha.
img = img.convertToFormat(QImage::Format_ARGB32);
// Ignore the other channels.
channel_num = 4;
}
const uint pixel_count = header.height * header.width;
static const uint components[4] = {2, 1, 0, 3}; // @@ Is this endian dependant?
if (compression) {
// Skip row lengths.
if (!seekBy(s, header.height * header.channel_count * sizeof(ushort)))
return false;
// Read RLE data.
for (uint channel = 0; channel < channel_num; channel++) {
uchar * ptr = img.bits() + components[channel];
uint count = 0;
while (count < pixel_count) {
uchar c;
if (s.atEnd())
return false;
s >> c;
uint len = c;
if (len < 128) {
// Copy next len+1 bytes literally.
len++;
count += len;
if (count > pixel_count)
return false;
while (len != 0) {
s >> *ptr;
ptr += 4;
len--;
}
} else if (len > 128) {
// Next -len+1 bytes in the dest are replicated from next source byte.
// (Interpret len as a negative 8-bit int.)
len ^= 0xFF;
len += 2;
count += len;
if (s.atEnd() || count > pixel_count)
return false;
uchar val;
s >> val;
while (len != 0) {
*ptr = val;
ptr += 4;
len--;
}
} else if (len == 128) {
// No-op.
}
}
}
} else {
// We're at the raw image data. It's each channel in order (Red, Green, Blue, Alpha, ...)
// where each channel consists of an 8-bit value for each pixel in the image.
// Read the data by channel.
for (uint channel = 0; channel < channel_num; channel++) {
uchar * ptr = img.bits() + components[channel];
// Read the data.
uint count = pixel_count;
while (count != 0) {
s >> *ptr;
ptr += 4;
count--;
}
}
}
return true;
}
} // Private
PSDHandler::PSDHandler()
{
}
bool PSDHandler::canRead() const
{
if (canRead(device())) {
setFormat("psd");
return true;
}
return false;
}
bool PSDHandler::read(QImage *image)
{
QDataStream s(device());
s.setByteOrder(QDataStream::BigEndian);
PSDHeader header;
s >> header;
// Check image file format.
if (s.atEnd() || !IsValid(header)) {
// qDebug() << "This PSD file is not valid.";
return false;
}
// Check if it's a supported format.
if (!IsSupported(header)) {
// qDebug() << "This PSD file is not supported.";
return false;
}
QImage img;
if (!LoadPSD(s, header, img)) {
// qDebug() << "Error loading PSD file.";
return false;
}
*image = img;
return true;
}
bool PSDHandler::canRead(QIODevice *device)
{
if (!device) {
qWarning("PSDHandler::canRead() called with no device");
return false;
}
qint64 oldPos = device->pos();
char head[4];
qint64 readBytes = device->read(head, sizeof(head));
if (readBytes != sizeof(head)) {
if (device->isSequential()) {
while (readBytes > 0)
device->ungetChar(head[readBytes-- - 1]);
} else {
device->seek(oldPos);
}
return false;
}
if (device->isSequential()) {
while (readBytes > 0)
device->ungetChar(head[readBytes-- - 1]);
} else {
device->seek(oldPos);
}
return qstrncmp(head, "8BPS", 4) == 0;
}
QImageIOPlugin::Capabilities PSDPlugin::capabilities(QIODevice *device, const QByteArray &format) const
{
if (format == "psd")
return Capabilities(CanRead);
if (!format.isEmpty())
return 0;
if (!device->isOpen())
return 0;
Capabilities cap;
if (device->isReadable() && PSDHandler::canRead(device))
cap |= CanRead;
return cap;
}
QImageIOHandler *PSDPlugin::create(QIODevice *device, const QByteArray &format) const
{
QImageIOHandler *handler = new PSDHandler;
handler->setDevice(device);
handler->setFormat(format);
return handler;
}

View File

@ -0,0 +1,7 @@
[Desktop Entry]
Type=Service
X-KDE-ServiceTypes=QImageIOPlugins
X-KDE-ImageFormat=psd
X-KDE-MimeType=image/x-psd
X-KDE-Read=true
X-KDE-Write=false

37
src/imageformats/psd.h Normal file
View File

@ -0,0 +1,37 @@
/* This file is part of the KDE project
Copyright (C) 2003 Ignacio Castaño <castano@ludicon.com>
This program is free software; you can redistribute it and/or
modify it under the terms of the Lesser GNU General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
*/
#ifndef KIMG_PSD_H
#define KIMG_PSD_H
#include <QImageIOPlugin>
class PSDHandler : public QImageIOHandler
{
public:
PSDHandler();
virtual bool canRead() const;
virtual bool read(QImage *image);
static bool canRead(QIODevice *device);
};
class PSDPlugin : public QImageIOPlugin
{
Q_OBJECT
Q_PLUGIN_METADATA(IID "org.qt-project.Qt.QImageIOHandlerFactoryInterface" FILE "psd.json")
public:
virtual Capabilities capabilities(QIODevice *device, const QByteArray &format) const;
virtual QImageIOHandler *create(QIODevice *device, const QByteArray &format = QByteArray()) const;
};
#endif // KIMG_PSD_H

View File

@ -0,0 +1,4 @@
{
"Keys": [ "psd" ],
"MimeTypes": [ "image/x-psd" ]
}

300
src/imageformats/ras.cpp Normal file
View File

@ -0,0 +1,300 @@
/* This file is part of the KDE project
Copyright (C) 2003 Dominik Seichter <domseichter@web.de>
Copyright (C) 2004 Ignacio Castaño <castano@ludicon.com>
Copyright (C) 2010 Troy Unrau <troy@kde.org>
This program is free software; you can redistribute it and/or
modify it under the terms of the Lesser GNU General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
*/
#include "ras.h"
#include <QImage>
#include <QtCore/QDataStream>
// #include <QDebug>
namespace // Private.
{
// format info from http://www.fileformat.info/format/sunraster/egff.htm
// Header format of saved files.
quint32 rasMagicBigEndian = 0x59a66a95;
// quint32 rasMagicLittleEndian = 0x956aa659; # used to support wrong encoded files
enum RASType {
RAS_TYPE_OLD = 0x0,
RAS_TYPE_STANDARD = 0x1,
RAS_TYPE_BYTE_ENCODED = 0x2,
RAS_TYPE_RGB_FORMAT = 0x3,
RAS_TYPE_TIFF_FORMAT = 0x4,
RAS_TYPE_IFF_FORMAT = 0x5,
RAS_TYPE_EXPERIMENTAL = 0xFFFF
};
enum RASColorMapType {
RAS_COLOR_MAP_TYPE_NONE = 0x0,
RAS_COLOR_MAP_TYPE_RGB = 0x1,
RAS_COLOR_MAP_TYPE_RAW = 0x2
};
struct RasHeader {
quint32 MagicNumber;
quint32 Width;
quint32 Height;
quint32 Depth;
quint32 Length;
quint32 Type;
quint32 ColorMapType;
quint32 ColorMapLength;
enum { SIZE = 32 }; // 8 fields of four bytes each
};
static QDataStream & operator>> (QDataStream & s, RasHeader & head)
{
s >> head.MagicNumber;
s >> head.Width;
s >> head.Height;
s >> head.Depth;
s >> head.Length;
s >> head.Type;
s >> head.ColorMapType;
s >> head.ColorMapLength;
/*qDebug() << "MagicNumber: " << head.MagicNumber
<< "Width: " << head.Width
<< "Height: " << head.Height
<< "Depth: " << head.Depth
<< "Length: " << head.Length
<< "Type: " << head.Type
<< "ColorMapType: " << head.ColorMapType
<< "ColorMapLength: " << head.ColorMapLength;*/
return s;
}
static bool IsSupported(const RasHeader & head)
{
// check magic number
if (head.MagicNumber != rasMagicBigEndian) {
return false;
}
// check for an appropriate depth
// we support 8bit+palette, 24bit and 32bit ONLY!
// TODO: add support for 1bit
if (!((head.Depth == 8 && head.ColorMapType == 1)
|| head.Depth == 24 || head.Depth == 32)) {
return false;
}
// the Type field adds support for RLE(BGR), RGB and other encodings
// we support Type 1: Normal(BGR) and Type 3: Normal(RGB) ONLY!
// TODO: add support for Type 2: RLE(BGR) & Type 4,5: TIFF/IFF
if (!(head.Type == 1 || head.Type == 3)) {
return false;
}
// Old files didn't have Length set - reject them for now
// TODO: add length recalculation to support old files
if (!head.Length) {
return false;
}
return true;
}
static bool LoadRAS(QDataStream & s, const RasHeader & ras, QImage &img)
{
s.device()->seek(RasHeader::SIZE);
// Read palette if needed.
QVector<quint8> palette(ras.ColorMapLength);
if (ras.ColorMapType == 1) {
for (quint32 i = 0; i < ras.ColorMapLength; ++i) {
s >> palette[i];
}
}
// each line must be a factor of 16 bits, so they may contain padding
// this will be 1 if padding required, 0 otherwise
int paddingrequired = (ras.Width * (ras.Depth / 8) % 2);
// qDebug() << "paddingrequired: " << paddingrequired;
// don't trust ras.Length
QVector<quint8> input(ras.Length);
int i = 0;
while (! s.atEnd()) {
s >> input[i];
// I guess we need to find out if we're at the end of a line
if (paddingrequired && i != 0 && !(i % (ras.Width * (ras.Depth / 8)))) {
s >> input[i];
}
i++;
}
// Allocate image
img = QImage(ras.Width, ras.Height, QImage::Format_ARGB32);
// Reconstruct image from RGB palette if we have a palette
// TODO: make generic so it works with 24bit or 32bit palettes
if (ras.ColorMapType == 1 && ras.Depth == 8) {
quint8 red, green, blue;
for (quint32 y = 0; y < ras.Height; y++) {
for (quint32 x = 0; x < ras.Width; x++) {
red = palette[(int)input[y * ras.Width + x]];
green = palette[(int)input[y * ras.Width + x] + (ras.ColorMapLength / 3)];
blue = palette[(int)input[y * ras.Width + x] + 2 * (ras.ColorMapLength / 3)];
img.setPixel(x, y, qRgb(red, green, blue));
}
}
}
if (ras.ColorMapType == 0 && ras.Depth == 24 && (ras.Type == 1 || ras.Type == 2)) {
quint8 red, green, blue;
for (quint32 y = 0; y < ras.Height; y++) {
for (quint32 x = 0; x < ras.Width; x++) {
red = input[y * 3 * ras.Width + x * 3 + 2];
green = input[y * 3 * ras.Width + x * 3 + 1];
blue = input[y * 3 * ras.Width + x * 3];
img.setPixel(x, y, qRgb(red, green, blue));
}
}
}
if (ras.ColorMapType == 0 && ras.Depth == 24 && ras.Type == 3) {
quint8 red, green, blue;
for (quint32 y = 0; y < ras.Height; y++) {
for (quint32 x = 0; x < ras.Width; x++) {
red = input[y * 3 * ras.Width + x * 3];
green = input[y * 3 * ras.Width + x * 3 + 1];
blue = input[y * 3 * ras.Width + x * 3 + 2];
img.setPixel(x, y, qRgb(red, green, blue));
}
}
}
if (ras.ColorMapType == 0 && ras.Depth == 32 && (ras.Type == 1 || ras.Type == 2)) {
quint8 red, green, blue;
for (quint32 y = 0; y < ras.Height; y++) {
for (quint32 x = 0; x < ras.Width; x++) {
red = input[y * 4 * ras.Width + x * 4 + 3];
green = input[y * 4 * ras.Width + x * 4 + 2];
blue = input[y * 4 * ras.Width + x * 4 + 1];
img.setPixel(x, y, qRgb(red, green, blue));
}
}
}
if (ras.ColorMapType == 0 && ras.Depth == 32 && ras.Type == 3) {
quint8 red, green, blue;
for (quint32 y = 0; y < ras.Height; y++) {
for (quint32 x = 0; x < ras.Width; x++) {
red = input[y * 4 * ras.Width + x * 4 + 1];
green = input[y * 4 * ras.Width + x * 4 + 2];
blue = input[y * 4 * ras.Width + x * 4 + 3];
img.setPixel(x, y, qRgb(red, green, blue));
}
}
}
return true;
}
} // namespace
RASHandler::RASHandler()
{
}
bool RASHandler::canRead() const
{
if (canRead(device())) {
setFormat("ras");
return true;
}
return false;
}
bool RASHandler::canRead(QIODevice *device)
{
if (!device) {
qWarning("RASHandler::canRead() called with no device");
return false;
}
if (device->isSequential()) {
qWarning("Reading ras files from sequential devices not supported");
return false;
}
qint64 oldPos = device->pos();
QByteArray head = device->read(RasHeader::SIZE); // header is exactly 32 bytes, always FIXME
int readBytes = head.size(); // this should always be 32 bytes
device->seek(oldPos);
if (readBytes < RasHeader::SIZE) {
return false;
}
QDataStream stream(head);
stream.setByteOrder(QDataStream::BigEndian);
RasHeader ras;
stream >> ras;
return IsSupported(ras);
}
bool RASHandler::read(QImage *outImage)
{
QDataStream s(device());
s.setByteOrder(QDataStream::BigEndian);
// Read image header.
RasHeader ras;
s >> ras;
// TODO: add support for old versions of RAS where Length may be zero in header
s.device()->seek(RasHeader::SIZE + ras.Length + ras.ColorMapLength);
// Check image file format. Type 2 is RLE, which causing seeking to be silly.
if (!s.atEnd() && ras.Type != 2) {
// qDebug() << "This RAS file is not valid, or an older version of the format.";
return false;
}
// Check supported file types.
if (!IsSupported(ras)) {
// qDebug() << "This RAS file is not supported.";
return false;
}
QImage img;
bool result = LoadRAS(s, ras, img);
if (result == false) {
// qDebug() << "Error loading RAS file.";
return false;
}
*outImage = img;
return true;
}
QImageIOPlugin::Capabilities RASPlugin::capabilities(QIODevice *device, const QByteArray &format) const
{
if (format == "ras")
return Capabilities(CanRead);
if (!format.isEmpty())
return 0;
if (!device->isOpen())
return 0;
Capabilities cap;
if (device->isReadable() && RASHandler::canRead(device))
cap |= CanRead;
return cap;
}
QImageIOHandler *RASPlugin::create(QIODevice *device, const QByteArray &format) const
{
QImageIOHandler *handler = new RASHandler;
handler->setDevice(device);
handler->setFormat(format);
return handler;
}

View File

@ -0,0 +1,7 @@
[Desktop Entry]
Type=Service
X-KDE-ServiceTypes=QImageIOPlugins
X-KDE-ImageFormat=ras
X-KDE-MimeType=image/x-sun-raster
X-KDE-Read=true
X-KDE-Write=false

38
src/imageformats/ras.h Normal file
View File

@ -0,0 +1,38 @@
/* This file is part of the KDE project
Copyright (C) 2003 Dominik Seichter <domseichter@web.de>
Copyright (C) 2010 Troy Unrau <troy@kde.org>
This program is free software; you can redistribute it and/or
modify it under the terms of the Lesser GNU General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
*/
#ifndef KIMG_RAS_H
#define KIMG_RAS_H
#include <QImageIOPlugin>
class RASHandler : public QImageIOHandler
{
public:
RASHandler();
virtual bool canRead() const;
virtual bool read(QImage *image);
static bool canRead(QIODevice *device);
};
class RASPlugin : public QImageIOPlugin
{
Q_OBJECT
Q_PLUGIN_METADATA(IID "org.qt-project.Qt.QImageIOHandlerFactoryInterface" FILE "ras.json")
public:
virtual Capabilities capabilities(QIODevice *device, const QByteArray &format) const;
virtual QImageIOHandler *create(QIODevice *device, const QByteArray &format = QByteArray()) const;
};
#endif // KIMG_RAS_H

View File

@ -0,0 +1,4 @@
{
"Keys": [ "ras" ],
"MimeTypes": [ "image/x-sun-raster" ]
}

700
src/imageformats/rgb.cpp Normal file
View File

@ -0,0 +1,700 @@
// kimgio module for SGI images
//
// Copyright (C) 2004 Melchior FRANZ <mfranz@kde.org>
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the Lesser GNU General Public License as
// published by the Free Software Foundation; either version 2 of the
// License, or (at your option) any later version.
/* this code supports:
* reading:
* everything, except images with 1 dimension or images with
* mapmode != NORMAL (e.g. dithered); Images with 16 bit
* precision or more than 4 layers are stripped down.
* writing:
* Run Length Encoded (RLE) or Verbatim (uncompressed)
* (whichever is smaller)
*
* Please report if you come across rgb/rgba/sgi/bw files that aren't
* recognized. Also report applications that can't deal with images
* saved by this filter.
*/
#include "rgb.h"
#include <QtCore/QMap>
#include <QtCore/QVector>
#include <QImage>
// #include <QDebug>
class RLEData : public QVector<uchar>
{
public:
RLEData() {}
RLEData(const uchar *d, uint l, uint o) : _offset(o) {
for (uint i = 0; i < l; i++)
append(d[i]);
}
bool operator<(const RLEData&) const;
void write(QDataStream& s);
uint offset() const {
return _offset;
}
private:
uint _offset;
};
class RLEMap : public QMap<RLEData, uint>
{
public:
RLEMap() : _counter(0), _offset(0) {}
uint insert(const uchar *d, uint l);
QVector<const RLEData*> vector();
void setBaseOffset(uint o) {
_offset = o;
}
private:
uint _counter;
uint _offset;
};
class SGIImage
{
public:
SGIImage(QIODevice *device);
~SGIImage();
bool readImage(QImage&);
bool writeImage(const QImage&);
private:
enum { NORMAL, DITHERED, SCREEN, COLORMAP }; // colormap
QIODevice *_dev;
QDataStream _stream;
quint8 _rle;
quint8 _bpc;
quint16 _dim;
quint16 _xsize;
quint16 _ysize;
quint16 _zsize;
quint32 _pixmin;
quint32 _pixmax;
char _imagename[80];
quint32 _colormap;
quint32 *_starttab;
quint32 *_lengthtab;
QByteArray _data;
QByteArray::Iterator _pos;
RLEMap _rlemap;
QVector<const RLEData*> _rlevector;
uint _numrows;
bool readData(QImage&);
bool getRow(uchar *dest);
void writeHeader();
void writeRle();
void writeVerbatim(const QImage&);
bool scanData(const QImage&);
uint compact(uchar *, uchar *);
uchar intensity(uchar);
};
SGIImage::SGIImage(QIODevice *io) :
_starttab(0),
_lengthtab(0)
{
_dev = io;
_stream.setDevice(_dev);
}
SGIImage::~SGIImage()
{
delete[] _starttab;
delete[] _lengthtab;
}
///////////////////////////////////////////////////////////////////////////////
bool SGIImage::getRow(uchar *dest)
{
int n, i;
if (!_rle) {
for (i = 0; i < _xsize; i++) {
if (_pos >= _data.end())
return false;
dest[i] = uchar(*_pos);
_pos += _bpc;
}
return true;
}
for (i = 0; i < _xsize;) {
if (_bpc == 2)
_pos++;
n = *_pos & 0x7f;
if (!n)
break;
if (*_pos++ & 0x80) {
for (; i < _xsize && n--; i++) {
*dest++ = *_pos;
_pos += _bpc;
}
} else {
for (; i < _xsize && n--; i++)
*dest++ = *_pos;
_pos += _bpc;
}
}
return i == _xsize;
}
bool SGIImage::readData(QImage& img)
{
QRgb *c;
quint32 *start = _starttab;
QByteArray lguard(_xsize, 0);
uchar *line = (uchar *)lguard.data();
unsigned x, y;
if (!_rle)
_pos = _data.begin();
for (y = 0; y < _ysize; y++) {
if (_rle)
_pos = _data.begin() + *start++;
if (!getRow(line))
return false;
c = (QRgb *)img.scanLine(_ysize - y - 1);
for (x = 0; x < _xsize; x++, c++)
*c = qRgb(line[x], line[x], line[x]);
}
if (_zsize == 1)
return true;
if (_zsize != 2) {
for (y = 0; y < _ysize; y++) {
if (_rle)
_pos = _data.begin() + *start++;
if (!getRow(line))
return false;
c = (QRgb *)img.scanLine(_ysize - y - 1);
for (x = 0; x < _xsize; x++, c++)
*c = qRgb(qRed(*c), line[x], line[x]);
}
for (y = 0; y < _ysize; y++) {
if (_rle)
_pos = _data.begin() + *start++;
if (!getRow(line))
return false;
c = (QRgb *)img.scanLine(_ysize - y - 1);
for (x = 0; x < _xsize; x++, c++)
*c = qRgb(qRed(*c), qGreen(*c), line[x]);
}
if (_zsize == 3)
return true;
}
for (y = 0; y < _ysize; y++) {
if (_rle)
_pos = _data.begin() + *start++;
if (!getRow(line))
return false;
c = (QRgb *)img.scanLine(_ysize - y - 1);
for (x = 0; x < _xsize; x++, c++)
*c = qRgba(qRed(*c), qGreen(*c), qBlue(*c), line[x]);
}
return true;
}
bool SGIImage::readImage(QImage& img)
{
qint8 u8;
qint16 u16;
qint32 u32;
// qDebug() << "reading rgb ";
// magic
_stream >> u16;
if (u16 != 0x01da)
return false;
// verbatim/rle
_stream >> _rle;
// qDebug() << (_rle ? "RLE" : "verbatim");
if (_rle > 1)
return false;
// bytes per channel
_stream >> _bpc;
// qDebug() << "bytes per channel: " << int(_bpc);
if (_bpc == 1)
;
else if (_bpc == 2) {
// qDebug() << "dropping least significant byte";
} else
return false;
// number of dimensions
_stream >> _dim;
// qDebug() << "dimensions: " << _dim;
if (_dim < 1 || _dim > 3)
return false;
_stream >> _xsize >> _ysize >> _zsize >> _pixmin >> _pixmax >> u32;
// qDebug() << "x: " << _xsize;
// qDebug() << "y: " << _ysize;
// qDebug() << "z: " << _zsize;
// name
_stream.readRawData(_imagename, 80);
_imagename[79] = '\0';
_stream >> _colormap;
// qDebug() << "colormap: " << _colormap;
if (_colormap != NORMAL)
return false; // only NORMAL supported
for (int i = 0; i < 404; i++)
_stream >> u8;
if (_dim == 1) {
// qDebug() << "1-dimensional images aren't supported yet";
return false;
}
if (_stream.atEnd())
return false;
_numrows = _ysize * _zsize;
img = QImage(_xsize, _ysize, QImage::Format_RGB32);
if (_zsize == 2 || _zsize == 4)
img = img.convertToFormat(QImage::Format_ARGB32);
else if (_zsize > 4) {
// qDebug() << "using first 4 of " << _zsize << " channels";
}
if (_rle) {
uint l;
_starttab = new quint32[_numrows];
for (l = 0; !_stream.atEnd() && l < _numrows; l++) {
_stream >> _starttab[l];
_starttab[l] -= 512 + _numrows * 2 * sizeof(quint32);
}
_lengthtab = new quint32[_numrows];
for (l = 0; l < _numrows; l++)
_stream >> _lengthtab[l];
}
_data = _dev->readAll();
// sanity check
if (_rle)
for (uint o = 0; o < _numrows; o++)
// don't change to greater-or-equal!
if (_starttab[o] + _lengthtab[o] > (uint)_data.size()) {
// qDebug() << "image corrupt (sanity check failed)";
return false;
}
if (!readData(img)) {
// qDebug() << "image corrupt (incomplete scanline)";
return false;
}
return true;
}
///////////////////////////////////////////////////////////////////////////////
void RLEData::write(QDataStream& s)
{
for (int i = 0; i < size(); i++)
s << at(i);
}
bool RLEData::operator<(const RLEData& b) const
{
uchar ac, bc;
for (int i = 0; i < qMin(size(), b.size()); i++) {
ac = at(i);
bc = b[i];
if (ac != bc)
return ac < bc;
}
return size() < b.size();
}
uint RLEMap::insert(const uchar *d, uint l)
{
RLEData data = RLEData(d, l, _offset);
Iterator it = find(data);
if (it != end())
return it.value();
_offset += l;
return QMap<RLEData, uint>::insert(data, _counter++).value();
}
QVector<const RLEData*> RLEMap::vector()
{
QVector<const RLEData*> v(size());
for (Iterator it = begin(); it != end(); ++it)
v.replace(it.value(), &it.key());
return v;
}
uchar SGIImage::intensity(uchar c)
{
if (c < _pixmin)
_pixmin = c;
if (c > _pixmax)
_pixmax = c;
return c;
}
uint SGIImage::compact(uchar *d, uchar *s)
{
uchar *dest = d, *src = s, patt, *t, *end = s + _xsize;
int i, n;
while (src < end) {
for (n = 0, t = src; t + 2 < end && !(*t == t[1] && *t == t[2]); t++)
n++;
while (n) {
i = n > 126 ? 126 : n;
n -= i;
*dest++ = 0x80 | i;
while (i--)
*dest++ = *src++;
}
if (src == end)
break;
patt = *src++;
for (n = 1; src < end && *src == patt; src++)
n++;
while (n) {
i = n > 126 ? 126 : n;
n -= i;
*dest++ = i;
*dest++ = patt;
}
}
*dest++ = 0;
return dest - d;
}
bool SGIImage::scanData(const QImage& img)
{
quint32 *start = _starttab;
QByteArray lineguard(_xsize * 2, 0);
QByteArray bufguard(_xsize, 0);
uchar *line = (uchar *)lineguard.data();
uchar *buf = (uchar *)bufguard.data();
const QRgb *c;
unsigned x, y;
uint len;
for (y = 0; y < _ysize; y++) {
c = reinterpret_cast<const QRgb *>(img.scanLine(_ysize - y - 1));
for (x = 0; x < _xsize; x++)
buf[x] = intensity(qRed(*c++));
len = compact(line, buf);
*start++ = _rlemap.insert(line, len);
}
if (_zsize == 1)
return true;
if (_zsize != 2) {
for (y = 0; y < _ysize; y++) {
c = reinterpret_cast<const QRgb *>(img.scanLine(_ysize - y - 1));
for (x = 0; x < _xsize; x++)
buf[x] = intensity(qGreen(*c++));
len = compact(line, buf);
*start++ = _rlemap.insert(line, len);
}
for (y = 0; y < _ysize; y++) {
c = reinterpret_cast<const QRgb *>(img.scanLine(_ysize - y - 1));
for (x = 0; x < _xsize; x++)
buf[x] = intensity(qBlue(*c++));
len = compact(line, buf);
*start++ = _rlemap.insert(line, len);
}
if (_zsize == 3)
return true;
}
for (y = 0; y < _ysize; y++) {
c = reinterpret_cast<const QRgb *>(img.scanLine(_ysize - y - 1));
for (x = 0; x < _xsize; x++)
buf[x] = intensity(qAlpha(*c++));
len = compact(line, buf);
*start++ = _rlemap.insert(line, len);
}
return true;
}
void SGIImage::writeHeader()
{
_stream << quint16(0x01da);
_stream << _rle << _bpc << _dim;
_stream << _xsize << _ysize << _zsize;
_stream << _pixmin << _pixmax;
_stream << quint32(0);
for (int i = 0; i < 80; i++)
_imagename[i] = '\0';
_stream.writeRawData(_imagename, 80);
_stream << _colormap;
for (int i = 0; i < 404; i++)
_stream << quint8(0);
}
void SGIImage::writeRle()
{
_rle = 1;
// qDebug() << "writing RLE data";
writeHeader();
uint i;
// write start table
for (i = 0; i < _numrows; i++)
_stream << quint32(_rlevector[_starttab[i]]->offset());
// write length table
for (i = 0; i < _numrows; i++)
_stream << quint32(_rlevector[_starttab[i]]->size());
// write data
for (i = 0; (int)i < _rlevector.size(); i++)
const_cast<RLEData*>(_rlevector[i])->write(_stream);
}
void SGIImage::writeVerbatim(const QImage& img)
{
_rle = 0;
// qDebug() << "writing verbatim data";
writeHeader();
const QRgb *c;
unsigned x, y;
for (y = 0; y < _ysize; y++) {
c = reinterpret_cast<const QRgb *>(img.scanLine(_ysize - y - 1));
for (x = 0; x < _xsize; x++)
_stream << quint8(qRed(*c++));
}
if (_zsize == 1)
return;
if (_zsize != 2) {
for (y = 0; y < _ysize; y++) {
c = reinterpret_cast<const QRgb *>(img.scanLine(_ysize - y - 1));
for (x = 0; x < _xsize; x++)
_stream << quint8(qGreen(*c++));
}
for (y = 0; y < _ysize; y++) {
c = reinterpret_cast<const QRgb *>(img.scanLine(_ysize - y - 1));
for (x = 0; x < _xsize; x++)
_stream << quint8(qBlue(*c++));
}
if (_zsize == 3)
return;
}
for (y = 0; y < _ysize; y++) {
c = reinterpret_cast<const QRgb *>(img.scanLine(_ysize - y - 1));
for (x = 0; x < _xsize; x++)
_stream << quint8(qAlpha(*c++));
}
}
bool SGIImage::writeImage(const QImage& image)
{
// qDebug() << "writing "; // TODO add filename
QImage img = image;
if (img.allGray())
_dim = 2, _zsize = 1;
else
_dim = 3, _zsize = 3;
if (img.format() == QImage::Format_ARGB32)
_dim = 3, _zsize++;
img = img.convertToFormat(QImage::Format_RGB32);
if (img.isNull()) {
// qDebug() << "can't convert image to depth 32";
return false;
}
_bpc = 1;
_xsize = img.width();
_ysize = img.height();
_pixmin = ~0u;
_pixmax = 0;
_colormap = NORMAL;
_numrows = _ysize * _zsize;
_starttab = new quint32[_numrows];
_rlemap.setBaseOffset(512 + _numrows * 2 * sizeof(quint32));
if (!scanData(img)) {
// qDebug() << "this can't happen";
return false;
}
_rlevector = _rlemap.vector();
long verbatim_size = _numrows * _xsize;
long rle_size = _numrows * 2 * sizeof(quint32);
for (int i = 0; i < _rlevector.size(); i++)
rle_size += _rlevector[i]->size();
// qDebug() << "minimum intensity: " << _pixmin;
// qDebug() << "maximum intensity: " << _pixmax;
// qDebug() << "saved scanlines: " << _numrows - _rlemap.size();
// qDebug() << "total savings: " << (verbatim_size - rle_size) << " bytes";
// qDebug() << "compression: " << (rle_size * 100.0 / verbatim_size) << '%';
if (verbatim_size <= rle_size)
writeVerbatim(img);
else
writeRle();
return true;
}
///////////////////////////////////////////////////////////////////////////////
RGBHandler::RGBHandler()
{
}
bool RGBHandler::canRead() const
{
if (canRead(device())) {
setFormat("rgb");
return true;
}
return false;
}
bool RGBHandler::read(QImage *outImage)
{
SGIImage sgi(device());
return sgi.readImage(*outImage);
}
bool RGBHandler::write(const QImage &image)
{
SGIImage sgi(device());
return sgi.writeImage(image);
}
bool RGBHandler::canRead(QIODevice *device)
{
if (!device) {
qWarning("RGBHandler::canRead() called with no device");
return false;
}
qint64 oldPos = device->pos();
QByteArray head = device->readLine(64);
int readBytes = head.size();
if (device->isSequential()) {
while (readBytes > 0)
device->ungetChar(head[readBytes-- - 1]);
} else {
device->seek(oldPos);
}
const QRegExp regexp(QLatin1String("^\x01\xda\x01[\x01\x02]"));
QString data(QString::fromLocal8Bit(head));
return data.contains(regexp);
}
///////////////////////////////////////////////////////////////////////////////
QImageIOPlugin::Capabilities RGBPlugin::capabilities(QIODevice *device, const QByteArray &format) const
{
if (format == "rgb" || format == "rgba" ||
format == "bw" || format == "sgi")
return Capabilities(CanRead | CanWrite);
if (!format.isEmpty())
return 0;
if (!device->isOpen())
return 0;
Capabilities cap;
if (device->isReadable() && RGBHandler::canRead(device))
cap |= CanRead;
if (device->isWritable())
cap |= CanWrite;
return cap;
}
QImageIOHandler *RGBPlugin::create(QIODevice *device, const QByteArray &format) const
{
QImageIOHandler *handler = new RGBHandler;
handler->setDevice(device);
handler->setFormat(format);
return handler;
}

View File

@ -0,0 +1,7 @@
[Desktop Entry]
Type=Service
X-KDE-ServiceTypes=QImageIOPlugins
X-KDE-ImageFormat=rgb,rgba,bw,sgi
X-KDE-MimeType=image/x-rgb
X-KDE-Read=true
X-KDE-Write=true

38
src/imageformats/rgb.h Normal file
View File

@ -0,0 +1,38 @@
// kimgio module for SGI images
//
// Copyright (C) 2004 Melchior FRANZ <mfranz@kde.org>
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the Lesser GNU General Public License as
// published by the Free Software Foundation; either version 2 of the
// License, or (at your option) any later version.
#ifndef KIMG_RGB_H
#define KIMG_RGB_H
#include <QImageIOPlugin>
class RGBHandler : public QImageIOHandler
{
public:
RGBHandler();
virtual bool canRead() const;
virtual bool read(QImage *image);
virtual bool write(const QImage &image);
static bool canRead(QIODevice *device);
};
class RGBPlugin : public QImageIOPlugin
{
Q_OBJECT
Q_PLUGIN_METADATA(IID "org.qt-project.Qt.QImageIOHandlerFactoryInterface" FILE "rgb.json")
public:
virtual Capabilities capabilities(QIODevice *device, const QByteArray &format) const;
virtual QImageIOHandler *create(QIODevice *device, const QByteArray &format = QByteArray()) const;
};
#endif // KIMG_RGB_H

View File

@ -0,0 +1,4 @@
{
"Keys": [ "rgb", "rgba", "bw", "sgi" ],
"MimeTypes": [ "image/x-rgb" ]
}

437
src/imageformats/tga.cpp Normal file
View File

@ -0,0 +1,437 @@
/* This file is part of the KDE project
Copyright (C) 2003 Dominik Seichter <domseichter@web.de>
Copyright (C) 2004 Ignacio Castaño <castano@ludicon.com>
This program is free software; you can redistribute it and/or
modify it under the terms of the Lesser GNU General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
*/
/* this code supports:
* reading:
* uncompressed and run length encoded indexed, grey and color tga files.
* image types 1, 2, 3, 9, 10 and 11.
* only RGB color maps with no more than 256 colors.
* pixel formats 8, 16, 24 and 32.
* writing:
* uncompressed true color tga files
*/
#include "tga.h"
#include <assert.h>
#include <QImage>
#include <QtCore/QDataStream>
// #include <QDebug>
typedef quint32 uint;
typedef quint16 ushort;
typedef quint8 uchar;
namespace // Private.
{
// Header format of saved files.
uchar targaMagic[12] = { 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
enum TGAType {
TGA_TYPE_INDEXED = 1,
TGA_TYPE_RGB = 2,
TGA_TYPE_GREY = 3,
TGA_TYPE_RLE_INDEXED = 9,
TGA_TYPE_RLE_RGB = 10,
TGA_TYPE_RLE_GREY = 11
};
#define TGA_INTERLEAVE_MASK 0xc0
#define TGA_INTERLEAVE_NONE 0x00
#define TGA_INTERLEAVE_2WAY 0x40
#define TGA_INTERLEAVE_4WAY 0x80
#define TGA_ORIGIN_MASK 0x30
#define TGA_ORIGIN_LEFT 0x00
#define TGA_ORIGIN_RIGHT 0x10
#define TGA_ORIGIN_LOWER 0x00
#define TGA_ORIGIN_UPPER 0x20
/** Tga Header. */
struct TgaHeader {
uchar id_length;
uchar colormap_type;
uchar image_type;
ushort colormap_index;
ushort colormap_length;
uchar colormap_size;
ushort x_origin;
ushort y_origin;
ushort width;
ushort height;
uchar pixel_size;
uchar flags;
enum { SIZE = 18 }; // const static int SIZE = 18;
};
static QDataStream & operator>> (QDataStream & s, TgaHeader & head)
{
s >> head.id_length;
s >> head.colormap_type;
s >> head.image_type;
s >> head.colormap_index;
s >> head.colormap_length;
s >> head.colormap_size;
s >> head.x_origin;
s >> head.y_origin;
s >> head.width;
s >> head.height;
s >> head.pixel_size;
s >> head.flags;
/*qDebug() << "id_length: " << head.id_length << " - colormap_type: " << head.colormap_type << " - image_type: " << head.image_type;
qDebug() << "colormap_index: " << head.colormap_index << " - colormap_length: " << head.colormap_length << " - colormap_size: " << head.colormap_size;
qDebug() << "x_origin: " << head.x_origin << " - y_origin: " << head.y_origin << " - width:" << head.width << " - height:" << head.height << " - pixelsize: " << head.pixel_size << " - flags: " << head.flags;*/
return s;
}
static bool IsSupported(const TgaHeader & head)
{
if (head.image_type != TGA_TYPE_INDEXED &&
head.image_type != TGA_TYPE_RGB &&
head.image_type != TGA_TYPE_GREY &&
head.image_type != TGA_TYPE_RLE_INDEXED &&
head.image_type != TGA_TYPE_RLE_RGB &&
head.image_type != TGA_TYPE_RLE_GREY) {
return false;
}
if (head.image_type == TGA_TYPE_INDEXED ||
head.image_type == TGA_TYPE_RLE_INDEXED) {
if (head.colormap_length > 256 || head.colormap_size != 24 || head.colormap_type != 1) {
return false;
}
}
if (head.image_type == TGA_TYPE_RGB ||
head.image_type == TGA_TYPE_GREY ||
head.image_type == TGA_TYPE_RLE_RGB ||
head.image_type == TGA_TYPE_RLE_GREY) {
if (head.colormap_type != 0) {
return false;
}
}
if (head.width == 0 || head.height == 0) {
return false;
}
if (head.pixel_size != 8 && head.pixel_size != 16 &&
head.pixel_size != 24 && head.pixel_size != 32) {
return false;
}
return true;
}
struct Color555 {
ushort b : 5;
ushort g : 5;
ushort r : 5;
};
struct TgaHeaderInfo {
bool rle;
bool pal;
bool rgb;
bool grey;
TgaHeaderInfo(const TgaHeader & tga) : rle(false), pal(false), rgb(false), grey(false) {
switch (tga.image_type) {
case TGA_TYPE_RLE_INDEXED:
rle = true;
// no break is intended!
case TGA_TYPE_INDEXED:
pal = true;
break;
case TGA_TYPE_RLE_RGB:
rle = true;
// no break is intended!
case TGA_TYPE_RGB:
rgb = true;
break;
case TGA_TYPE_RLE_GREY:
rle = true;
// no break is intended!
case TGA_TYPE_GREY:
grey = true;
break;
default:
// Error, unknown image type.
break;
}
}
};
static bool LoadTGA(QDataStream & s, const TgaHeader & tga, QImage &img)
{
// Create image.
img = QImage(tga.width, tga.height, QImage::Format_RGB32);
TgaHeaderInfo info(tga);
// Bits 0-3 are the numbers of alpha bits (can be zero!)
const int numAlphaBits = tga.flags & 0xf;
// However alpha exists only in the 32 bit format.
if ((tga.pixel_size == 32) && (tga.flags & 0xf)) {
img = QImage(tga.width, tga.height, QImage::Format_ARGB32);
}
uint pixel_size = (tga.pixel_size / 8);
uint size = tga.width * tga.height * pixel_size;
if (size < 1) {
// qDebug() << "This TGA file is broken with size " << size;
return false;
}
// Read palette.
char palette[768];
if (info.pal) {
// @todo Support palettes in other formats!
s.readRawData(palette, 3 * tga.colormap_length);
}
// Allocate image.
uchar * const image = new uchar[size];
if (info.rle) {
// Decode image.
char * dst = (char *)image;
int num = size;
while (num > 0) {
// Get packet header.
uchar c;
s >> c;
uint count = (c & 0x7f) + 1;
num -= count * pixel_size;
if (c & 0x80) {
// RLE pixels.
assert(pixel_size <= 8);
char pixel[8];
s.readRawData(pixel, pixel_size);
do {
memcpy(dst, pixel, pixel_size);
dst += pixel_size;
} while (--count);
} else {
// Raw pixels.
count *= pixel_size;
s.readRawData(dst, count);
dst += count;
}
}
} else {
// Read raw image.
s.readRawData((char *)image, size);
}
// Convert image to internal format.
int y_start, y_step, y_end;
if (tga.flags & TGA_ORIGIN_UPPER) {
y_start = 0;
y_step = 1;
y_end = tga.height;
} else {
y_start = tga.height - 1;
y_step = -1;
y_end = -1;
}
uchar * src = image;
for (int y = y_start; y != y_end; y += y_step) {
QRgb * scanline = (QRgb *) img.scanLine(y);
if (info.pal) {
// Paletted.
for (int x = 0; x < tga.width; x++) {
uchar idx = *src++;
scanline[x] = qRgb(palette[3 * idx + 2], palette[3 * idx + 1], palette[3 * idx + 0]);
}
} else if (info.grey) {
// Greyscale.
for (int x = 0; x < tga.width; x++) {
scanline[x] = qRgb(*src, *src, *src);
src++;
}
} else {
// True Color.
if (tga.pixel_size == 16) {
for (int x = 0; x < tga.width; x++) {
Color555 c = *reinterpret_cast<Color555 *>(src);
scanline[x] = qRgb((c.r << 3) | (c.r >> 2), (c.g << 3) | (c.g >> 2), (c.b << 3) | (c.b >> 2));
src += 2;
}
} else if (tga.pixel_size == 24) {
for (int x = 0; x < tga.width; x++) {
scanline[x] = qRgb(src[2], src[1], src[0]);
src += 3;
}
} else if (tga.pixel_size == 32) {
for (int x = 0; x < tga.width; x++) {
// ### TODO: verify with images having really some alpha data
const uchar alpha = (src[3] << (8 - numAlphaBits));
scanline[x] = qRgba(src[2], src[1], src[0], alpha);
src += 4;
}
}
}
}
// Free image.
delete [] image;
return true;
}
} // namespace
TGAHandler::TGAHandler()
{
}
bool TGAHandler::canRead() const
{
if (canRead(device())) {
setFormat("tga");
return true;
}
return false;
}
bool TGAHandler::read(QImage *outImage)
{
//qDebug() << "Loading TGA file!";
QDataStream s(device());
s.setByteOrder(QDataStream::LittleEndian);
// Read image header.
TgaHeader tga;
s >> tga;
s.device()->seek(TgaHeader::SIZE + tga.id_length);
// Check image file format.
if (s.atEnd()) {
// qDebug() << "This TGA file is not valid.";
return false;
}
// Check supported file types.
if (!IsSupported(tga)) {
// qDebug() << "This TGA file is not supported.";
return false;
}
QImage img;
bool result = LoadTGA(s, tga, img);
if (result == false) {
// qDebug() << "Error loading TGA file.";
return false;
}
*outImage = img;
return true;
}
bool TGAHandler::write(const QImage &image)
{
QDataStream s(device());
s.setByteOrder(QDataStream::LittleEndian);
const QImage& img = image;
const bool hasAlpha = (img.format() == QImage::Format_ARGB32);
for (int i = 0; i < 12; i++)
s << targaMagic[i];
// write header
s << quint16(img.width()); // width
s << quint16(img.height()); // height
s << quint8(hasAlpha ? 32 : 24); // depth (24 bit RGB + 8 bit alpha)
s << quint8(hasAlpha ? 0x24 : 0x20); // top left image (0x20) + 8 bit alpha (0x4)
for (int y = 0; y < img.height(); y++)
for (int x = 0; x < img.width(); x++) {
const QRgb color = img.pixel(x, y);
s << quint8(qBlue(color));
s << quint8(qGreen(color));
s << quint8(qRed(color));
if (hasAlpha)
s << quint8(qAlpha(color));
}
return true;
}
bool TGAHandler::canRead(QIODevice *device)
{
if (!device) {
qWarning("TGAHandler::canRead() called with no device");
return false;
}
qint64 oldPos = device->pos();
QByteArray head = device->read(TgaHeader::SIZE);
int readBytes = head.size();
if (device->isSequential()) {
for (int pos = readBytes - 1; pos >= 0; --pos) {
device->ungetChar(head[pos]);
}
} else {
device->seek(oldPos);
}
if (readBytes < TgaHeader::SIZE) {
return false;
}
QDataStream stream(head);
stream.setByteOrder(QDataStream::LittleEndian);
TgaHeader tga;
stream >> tga;
return IsSupported(tga);
}
QImageIOPlugin::Capabilities TGAPlugin::capabilities(QIODevice *device, const QByteArray &format) const
{
if (format == "tga")
return Capabilities(CanRead | CanWrite);
if (!format.isEmpty())
return 0;
if (!device->isOpen())
return 0;
Capabilities cap;
if (device->isReadable() && TGAHandler::canRead(device))
cap |= CanRead;
if (device->isWritable())
cap |= CanWrite;
return cap;
}
QImageIOHandler *TGAPlugin::create(QIODevice *device, const QByteArray &format) const
{
QImageIOHandler *handler = new TGAHandler;
handler->setDevice(device);
handler->setFormat(format);
return handler;
}

View File

@ -0,0 +1,7 @@
[Desktop Entry]
Type=Service
X-KDE-ServiceTypes=QImageIOPlugins
X-KDE-ImageFormat=tga
X-KDE-MimeType=image/x-tga
X-KDE-Read=true
X-KDE-Write=true

37
src/imageformats/tga.h Normal file
View File

@ -0,0 +1,37 @@
/* This file is part of the KDE project
Copyright (C) 2003 Dominik Seichter <domseichter@web.de>
This program is free software; you can redistribute it and/or
modify it under the terms of the Lesser GNU General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
*/
#ifndef KIMG_TGA_H
#define KIMG_TGA_H
#include <QImageIOPlugin>
class TGAHandler : public QImageIOHandler
{
public:
TGAHandler();
virtual bool canRead() const;
virtual bool read(QImage *image);
virtual bool write(const QImage &image);
static bool canRead(QIODevice *device);
};
class TGAPlugin : public QImageIOPlugin
{
Q_OBJECT
Q_PLUGIN_METADATA(IID "org.qt-project.Qt.QImageIOHandlerFactoryInterface" FILE "tga.json")
public:
virtual Capabilities capabilities(QIODevice *device, const QByteArray &format) const;
virtual QImageIOHandler *create(QIODevice *device, const QByteArray &format = QByteArray()) const;
};
#endif // KIMG_TGA_H

View File

@ -0,0 +1,4 @@
{
"Keys": [ "tga" ],
"MimeTypes": [ "image/x-tga" ]
}

2655
src/imageformats/xcf.cpp Normal file

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,7 @@
[Desktop Entry]
Type=Service
X-KDE-ServiceTypes=QImageIOPlugins
X-KDE-ImageFormat=xcf
X-KDE-MimeType=image/x-xcf
X-KDE-Read=true
X-KDE-Write=false

49
src/imageformats/xcf.h Normal file
View File

@ -0,0 +1,49 @@
/*
* xcf.cpp: A Qt 5 plug-in for reading GIMP XCF image files
* Copyright (C) 2001 lignum Computing, Inc. <allen@lignumcomputing.com>
* Copyright (C) 2004 Melchior FRANZ <mfranz@kde.org>
*
* This plug-in is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
*/
#ifndef KIMG_XCF_H
#define KIMG_XCF_H
#include <QImageIOPlugin>
class XCFHandler : public QImageIOHandler
{
public:
XCFHandler();
virtual bool canRead() const;
virtual bool read(QImage *image);
virtual bool write(const QImage &image);
static bool canRead(QIODevice *device);
};
class XCFPlugin : public QImageIOPlugin
{
Q_OBJECT
Q_PLUGIN_METADATA(IID "org.qt-project.Qt.QImageIOHandlerFactoryInterface" FILE "xcf.json")
public:
virtual Capabilities capabilities(QIODevice *device, const QByteArray &format) const;
virtual QImageIOHandler *create(QIODevice *device, const QByteArray &format = QByteArray()) const;
};
#endif // KIMG_XCF_H

View File

@ -0,0 +1,4 @@
{
"Keys": [ "xcf" ],
"MimeTypes": [ "image/x-xcf" ]
}

View File

@ -0,0 +1,7 @@
[Desktop Entry]
Type=Service
X-KDE-ServiceTypes=QImageIOPlugins
X-KDE-ImageFormat=xv
X-KDE-MimeType=
X-KDE-Read=true
X-KDE-Write=true

229
src/imageformats/xview.cpp Normal file
View File

@ -0,0 +1,229 @@
/**
* QImageIO Routines to read/write XV images.
* copyright (c) 1998 Torben Weis <weis@kde.org>
* copyright (c) 1999 Oliver Eiden <o.eiden@pop.ruhr.de>
*
* This library is distributed under the conditions of the GNU LGPL.
*/
#include "xview.h"
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <QImage>
#define BUFSIZE 1024
static const int b_255_3[] = {0, 85, 170, 255}, // index*255/3
rg_255_7[] = {0, 36, 72, 109, 145, 182, 218, 255}; // index *255/7
XVHandler::XVHandler()
{
}
bool XVHandler::canRead() const
{
if (canRead(device())) {
setFormat("xv");
return true;
}
return false;
}
bool XVHandler::read(QImage *retImage)
{
int x = -1;
int y = -1;
int maxval = -1;
QIODevice *iodev = device();
char str[ BUFSIZE ];
// magic number must be "P7 332"
iodev->readLine(str, BUFSIZE);
if (strncmp(str, "P7 332", 6))
return false;
// next line #XVVERSION
iodev->readLine(str, BUFSIZE);
if (strncmp(str, "#XVVERSION", 10))
return false;
// now it gets interesting, #BUILTIN means we are out.
// if IMGINFO comes, we are happy!
iodev->readLine(str, BUFSIZE);
if (strncmp(str, "#IMGINFO:", 9))
return false;
// after this an #END_OF_COMMENTS signals everything to be ok!
iodev->readLine(str, BUFSIZE);
if (strncmp(str, "#END_OF", 7))
return false;
// now a last line with width, height, maxval which is
// supposed to be 255
iodev->readLine(str, BUFSIZE);
sscanf(str, "%d %d %d", &x, &y, &maxval);
if (maxval != 255)
return false;
int blocksize = x * y;
if (x < 0 || y < 0 || blocksize < x || blocksize < y)
return false;
// now follows a binary block of x*y bytes.
char *block = (char*) malloc(blocksize);
if (!block)
return false;
if (iodev->read(block, blocksize) != blocksize) {
free(block);
return false;
}
// Create the image
QImage image(x, y, QImage::Format_Indexed8);
int numColors;
numColors = qMin(maxval + 1, 0);
numColors = qMax(0, maxval + 1);
image.setColorCount(numColors);
// how do the color handling? they are absolute 24bpp
// or at least can be calculated as such.
int r, g, b;
for (int j = 0; j < 256; j++) {
r = rg_255_7[((j >> 5) & 0x07)];
g = rg_255_7[((j >> 2) & 0x07)];
b = b_255_3[((j >> 0) & 0x03)];
image.setColor(j, qRgb(r, g, b));
}
for (int py = 0; py < y; py++) {
uchar *data = image.scanLine(py);
memcpy(data, block + py * x, x);
}
*retImage = image;
free(block);
return true;
}
bool XVHandler::write(const QImage &image)
{
QIODevice& f = *(device());
// Removed "f.open(...)" and "f.close()" (tanghus)
int w = image.width(), h = image.height();
char str[ 1024 ];
// magic number must be "P7 332"
f.write("P7 332\n", 7);
// next line #XVVERSION
f.write("#XVVERSION:\n", 12);
// now it gets interesting, #BUILTIN means we are out.
// if IMGINFO comes, we are happy!
f.write("#IMGINFO:\n", 10);
// after this an #END_OF_COMMENTS signals everything to be ok!
f.write("#END_OF_COMMENTS:\n", 18);
// now a last line with width, height, maxval which is supposed to be 255
sprintf(str, "%i %i 255\n", w, h);
f.write(str, strlen(str));
QImage tmpImage(image);
if (image.depth() == 1)
tmpImage = image.convertToFormat(QImage::Format_Indexed8, Qt::AutoColor);
uchar* buffer = new uchar[ w ];
for (int py = 0; py < h; py++) {
const uchar *data = tmpImage.scanLine(py);
for (int px = 0; px < w; px++) {
int r, g, b;
if (tmpImage.depth() == 32) {
const QRgb *data32 = (QRgb*) data;
r = qRed(*data32) >> 5;
g = qGreen(*data32) >> 5;
b = qBlue(*data32) >> 6;
data += sizeof(QRgb);
} else {
QRgb color = tmpImage.color(*data);
r = qRed(color) >> 5;
g = qGreen(color) >> 5;
b = qBlue(color) >> 6;
data++;
}
buffer[ px ] = (r << 5) | (g << 2) | b;
}
f.write((const char*)buffer, w);
}
delete[] buffer;
return true;
}
bool XVHandler::canRead(QIODevice *device)
{
if (!device) {
qWarning("XVHandler::canRead() called with no device");
return false;
}
qint64 oldPos = device->pos();
char head[6];
qint64 readBytes = device->read(head, sizeof(head));
if (readBytes != sizeof(head)) {
if (device->isSequential()) {
while (readBytes > 0)
device->ungetChar(head[readBytes-- - 1]);
} else {
device->seek(oldPos);
}
return false;
}
if (device->isSequential()) {
while (readBytes > 0)
device->ungetChar(head[readBytes-- - 1]);
} else {
device->seek(oldPos);
}
return qstrncmp(head, "P7 332", 6) == 0;
}
QImageIOPlugin::Capabilities XVPlugin::capabilities(QIODevice *device, const QByteArray &format) const
{
if (format == "xv")
return Capabilities(CanRead | CanWrite);
if (!format.isEmpty())
return 0;
if (!device->isOpen())
return 0;
Capabilities cap;
if (device->isReadable() && XVHandler::canRead(device))
cap |= CanRead;
if (device->isWritable())
cap |= CanWrite;
return cap;
}
QImageIOHandler *XVPlugin::create(QIODevice *device, const QByteArray &format) const
{
QImageIOHandler *handler = new XVHandler;
handler->setDevice(device);
handler->setFormat(format);
return handler;
}

42
src/imageformats/xview.h Normal file
View File

@ -0,0 +1,42 @@
/**
* QImageIO Routines to read/write XV images.
* copyright (c) 1998 Torben Weis <weis@kde.org>
* copyright (c) 1999 Oliver Eiden <o.eiden@pop.ruhr.de>
*
* This library is distributed under the conditions of the GNU LGPL.
*
*
* Changelog:
* 23.3.99 Oliver Eiden <o.eiden@pop.ruhr.de>
* changed the mapping from 3-3-2 decoded pixels to 8-8-8 decoded true-color pixels
* now it uses the same mapping as xv, this leads to better visual results
* Patch merged in HEAD by Chris Spiegel <matrix@xirtam.org>
*/
#ifndef KIMG_XVIEW_H
#define KIMG_XVIEW_H
#include <QImageIOPlugin>
class XVHandler : public QImageIOHandler
{
public:
XVHandler();
virtual bool canRead() const;
virtual bool read(QImage *image);
virtual bool write(const QImage &image);
static bool canRead(QIODevice *device);
};
class XVPlugin : public QImageIOPlugin
{
Q_OBJECT
Q_PLUGIN_METADATA(IID "org.qt-project.Qt.QImageIOHandlerFactoryInterface" FILE "xview.json")
public:
virtual Capabilities capabilities(QIODevice *device, const QByteArray &format) const;
virtual QImageIOHandler *create(QIODevice *device, const QByteArray &format = QByteArray()) const;
};
#endif // KIMG_XVIEW_H

View File

@ -0,0 +1,4 @@
{
"Keys": [ "xv" ],
"MimeTypes": [ ]
}