You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
138 lines
3.6 KiB
138 lines
3.6 KiB
15 years ago
|
/* This file is part of the KDE project
|
||
|
* Copyright (C) 2003 Volker Krause <volker.krause@rwth-aachen.de>
|
||
|
*
|
||
|
* This program is free software; you can redistribute it and/or
|
||
|
* modify it under the terms of the GNU General Public
|
||
|
* License as published by the Free Software Foundation version 2.
|
||
|
*
|
||
|
* This program 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
|
||
|
* General Public License for more details.
|
||
|
*
|
||
|
* You should have received a copy of the GNU General Public License
|
||
|
* along with this program; see the file COPYING. If not, write to
|
||
|
* the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
|
||
|
* Boston, MA 02110-1301, USA.
|
||
|
*
|
||
|
*/
|
||
|
|
||
12 years ago
|
#include "tdefile_pnm.h"
|
||
15 years ago
|
|
||
|
#include <math.h>
|
||
|
#include <kgenericfactory.h>
|
||
15 years ago
|
#include <tqfile.h>
|
||
13 years ago
|
#include <tqtextstream.h>
|
||
15 years ago
|
|
||
|
static const char* formats[] = {
|
||
|
I18N_NOOP("plain"),
|
||
|
I18N_NOOP("raw")
|
||
|
};
|
||
|
|
||
|
typedef KGenericFactory<KPnmPlugin> PnmFactory;
|
||
|
|
||
12 years ago
|
K_EXPORT_COMPONENT_FACTORY(tdefile_pnm, PnmFactory("tdefile_pnm"))
|
||
15 years ago
|
|
||
14 years ago
|
KPnmPlugin::KPnmPlugin(TQObject *parent, const char *name, const TQStringList &args) : KFilePlugin(parent, name, args)
|
||
15 years ago
|
{
|
||
|
makeMimeTypeInfo( "image/x-portable-bitmap" );
|
||
|
makeMimeTypeInfo( "image/x-portable-greymap" );
|
||
|
makeMimeTypeInfo( "image/x-portable-pixmap" );
|
||
|
}
|
||
|
|
||
15 years ago
|
void KPnmPlugin::makeMimeTypeInfo(const TQString& mimetype)
|
||
15 years ago
|
{
|
||
|
KFileMimeTypeInfo* info = addMimeTypeInfo( mimetype );
|
||
|
|
||
|
KFileMimeTypeInfo::GroupInfo* group = 0;
|
||
|
KFileMimeTypeInfo::ItemInfo* item = 0;
|
||
|
|
||
|
group = addGroupInfo(info, "General", i18n("General"));
|
||
|
|
||
15 years ago
|
addItemInfo(group, "Format", i18n("Format"), TQVariant::String);
|
||
15 years ago
|
|
||
15 years ago
|
item = addItemInfo(group, "Dimensions", i18n("Dimensions"), TQVariant::Size);
|
||
15 years ago
|
setUnit(item, KFileMimeTypeInfo::Pixels);
|
||
|
|
||
15 years ago
|
item = addItemInfo(group, "BitDepth", i18n("Bit Depth"), TQVariant::Int);
|
||
15 years ago
|
setUnit(item, KFileMimeTypeInfo::BitsPerPixel);
|
||
|
|
||
15 years ago
|
addItemInfo(group, "Comment", i18n("Comment"), TQVariant::String);
|
||
15 years ago
|
}
|
||
|
|
||
|
|
||
|
bool KPnmPlugin::readInfo( KFileMetaInfo& info, uint /*what*/)
|
||
|
{
|
||
15 years ago
|
TQFile f(info.path());
|
||
15 years ago
|
if(!f.open(IO_ReadOnly))
|
||
|
return false;
|
||
|
if(f.size() <= 2)
|
||
|
return false;
|
||
15 years ago
|
TQTextStream stream(&f);
|
||
15 years ago
|
|
||
|
char c;
|
||
|
stream >> c;
|
||
|
if(c != 'P')
|
||
|
return false;
|
||
|
|
||
|
// image format and type
|
||
|
int n;
|
||
|
stream >> n;
|
||
|
int format = (n - 1) / 3;
|
||
|
if(format != 0 && format != 1)
|
||
|
return false;
|
||
|
int type = (n - 1) % 3;
|
||
|
|
||
15 years ago
|
TQString comments, buffer;
|
||
15 years ago
|
while(!stream.atEnd()) {
|
||
|
stream >> c;
|
||
|
// comment
|
||
|
if( c == '#') {
|
||
|
buffer = stream.readLine();
|
||
|
comments += buffer.stripWhiteSpace();
|
||
|
comments += '\n';
|
||
|
}
|
||
|
// image size
|
||
14 years ago
|
// unfortunately TQt doesn't have some kind of push-back method for
|
||
15 years ago
|
// TQTextStream, so we need to manually decode the first part of the
|
||
15 years ago
|
// image size.
|
||
|
if( c >= '0' && c <= '9' ) {
|
||
|
buffer = "";
|
||
15 years ago
|
TQChar tmp(c);
|
||
15 years ago
|
while(!stream.atEnd() && tmp.isDigit()) {
|
||
|
buffer += tmp;
|
||
|
stream >> tmp;
|
||
|
}
|
||
|
break;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
// image size
|
||
|
int x, y, max;
|
||
|
x = buffer.toInt();
|
||
|
stream >> y;
|
||
|
stream >> max;
|
||
|
|
||
|
// bit depth
|
||
|
// PNM doesn't provide a conventional bit depth value, only the highest value
|
||
|
// per pixel. To have comparable values with other formats, we convert it to
|
||
|
// a normal bit depth value.
|
||
|
int bpp = 1;
|
||
|
if(type != 0)
|
||
|
bpp = (int)ceil(log((double)max) / log(2.0));
|
||
|
if(type == 2)
|
||
|
bpp *= 3;
|
||
|
|
||
|
KFileMetaInfoGroup group = appendGroup(info, "General");
|
||
|
appendItem(group, "Format", formats[format]);
|
||
15 years ago
|
appendItem(group, "Dimensions", TQSize(x, y));
|
||
15 years ago
|
if(!comments.isEmpty())
|
||
|
appendItem(group, "Comment", comments.stripWhiteSpace());
|
||
|
appendItem(group, "BitDepth", bpp);
|
||
|
|
||
|
f.close();
|
||
|
return true;
|
||
|
}
|
||
|
|
||
12 years ago
|
#include "tdefile_pnm.moc"
|