update
[kdegraphics.git] / strigi-analyzer / ico / icothroughanalyzer.cpp
blobc13f99dd3a971aef78d26eaf0c96fc4b778c152b
1 /* This file is part of the KDE project
2 * Copyright (C) 2002 Shane Wright <me@shanewright.co.uk>
3 * Copyright (C) 2007 Matthias Lechner <matthias@lmme.de>
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public
7 * License as published by the Free Software Foundation version 2.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; see the file COPYING. If not, write to
16 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
21 #define STRIGI_IMPORT_API
22 #include <strigi/streamthroughanalyzer.h>
23 #include <strigi/analyzerplugin.h>
24 #include <strigi/fieldtypes.h>
25 #include <strigi/analysisresult.h>
27 #include <QByteArray>
28 #include <QDataStream>
30 using namespace Strigi;
31 using namespace std;
33 class IcoThroughAnalyzerFactory;
34 class IcoThroughAnalyzer : public StreamThroughAnalyzer {
35 private:
36 const IcoThroughAnalyzerFactory* factory;
37 AnalysisResult* idx;
39 void setIndexable( AnalysisResult *i ) {
40 idx = i;
42 InputStream* connectInputStream( InputStream *in );
43 bool isReadyWithStream() { return true; }
44 const char* name() const {
45 return "IcoThroughAnalyzer";
48 public:
49 IcoThroughAnalyzer( const IcoThroughAnalyzerFactory* f ) : factory( f ) {}
52 class IcoThroughAnalyzerFactory : public StreamThroughAnalyzerFactory {
53 private:
54 const char* name() const {
55 return "IcoThroughAnalyzer";
57 StreamThroughAnalyzer* newInstance() const {
58 return new IcoThroughAnalyzer(this);
60 void registerFields( FieldRegister& );
62 static const string numberFieldName;
63 static const string widthFieldName;
64 static const string heightFieldName;
65 static const string bitsPerPixelFieldName;
66 static const string colorCountFieldName;
67 public:
68 const RegisteredField* numberField;
69 const RegisteredField* widthField;
70 const RegisteredField* heightField;
71 const RegisteredField* bitsPerPixelField;
72 const RegisteredField* colorCountField;
75 const string IcoThroughAnalyzerFactory::numberFieldName( "document.stats.image_count" );
76 const string IcoThroughAnalyzerFactory::widthFieldName( "image.width" );
77 const string IcoThroughAnalyzerFactory::heightFieldName( "image.height" );
78 const string IcoThroughAnalyzerFactory::bitsPerPixelFieldName( "image.color_depth" );
79 const string IcoThroughAnalyzerFactory::colorCountFieldName( "image.color_count" );
81 void IcoThroughAnalyzerFactory::registerFields( FieldRegister& reg ) {
82 numberField = reg.registerField( numberFieldName, FieldRegister::integerType, 1, 0 );
83 widthField = reg.registerField( widthFieldName, FieldRegister::integerType, 1, 0 );
84 heightField = reg.registerField( heightFieldName, FieldRegister::integerType, 1, 0 );
85 bitsPerPixelField = reg.registerField( bitsPerPixelFieldName, FieldRegister::integerType, 1, 0 );
86 colorCountField = reg.registerField( colorCountFieldName, FieldRegister::integerType, 1, 0 );
90 InputStream* IcoThroughAnalyzer::connectInputStream( InputStream* in ) {
91 if( !in )
92 return in;
94 const char *c;
95 int32_t nread = in->read( c, in->size(), in->size() );
96 in->reset( 0 );
97 if( nread == -2 )
98 return in;
100 QByteArray buffer( c, in->size() );
101 QDataStream dstream( buffer );
103 // ICO files are little-endian
104 dstream.setByteOrder( QDataStream::LittleEndian );
106 // read the beginning of the stream and make sure it looks ok
107 uint16_t ico_reserved;
108 uint16_t ico_type;
109 uint16_t ico_count;
111 dstream >> ico_reserved;
112 dstream >> ico_type;
113 dstream >> ico_count;
115 if ((ico_reserved != 0) || (ico_type != 1) || (ico_count < 1))
116 return in;
119 // now loop through each of the icon entries
120 uint8_t icoe_width;
121 uint8_t icoe_height;
122 uint8_t icoe_colorcount;
123 uint8_t icoe_reserved;
124 uint16_t icoe_planes;
125 uint16_t icoe_bitcount;
126 uint32_t icoe_bytesinres;
127 uint32_t icoe_imageoffset;
129 // read the data on the 1st icon
130 dstream >> icoe_width;
131 dstream >> icoe_height;
132 dstream >> icoe_colorcount;
133 dstream >> icoe_reserved;
134 dstream >> icoe_planes;
135 dstream >> icoe_bitcount;
136 dstream >> icoe_bytesinres;
137 dstream >> icoe_imageoffset;
139 idx->addValue( factory->numberField, ico_count );
141 idx->addValue( factory->widthField, icoe_width );
142 idx->addValue( factory->heightField, icoe_height );
144 if (icoe_bitcount > 0)
145 idx->addValue( factory->bitsPerPixelField, icoe_bitcount );
147 if (icoe_colorcount > 0)
148 idx->addValue( factory->colorCountField, icoe_colorcount );
149 else if (icoe_bitcount > 0)
150 idx->addValue( factory->colorCountField, 2 ^ icoe_bitcount );
152 return in;
155 class Factory : public AnalyzerFactoryFactory {
156 public:
157 std::list<StreamThroughAnalyzerFactory*>
158 getStreamThroughAnalyzerFactories() const {
159 std::list<StreamThroughAnalyzerFactory*> af;
160 af.push_back(new IcoThroughAnalyzerFactory());
161 return af;
165 STRIGI_ANALYZER_FACTORY(Factory)