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>
28 #include <QDataStream>
30 using namespace Strigi
;
33 class IcoThroughAnalyzerFactory
;
34 class IcoThroughAnalyzer
: public StreamThroughAnalyzer
{
36 const IcoThroughAnalyzerFactory
* factory
;
39 void setIndexable( AnalysisResult
*i
) {
42 InputStream
* connectInputStream( InputStream
*in
);
43 bool isReadyWithStream() { return true; }
44 const char* name() const {
45 return "IcoThroughAnalyzer";
49 IcoThroughAnalyzer( const IcoThroughAnalyzerFactory
* f
) : factory( f
) {}
52 class IcoThroughAnalyzerFactory
: public StreamThroughAnalyzerFactory
{
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
;
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
) {
95 int32_t nread
= in
->read( c
, in
->size(), in
->size() );
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
;
111 dstream
>> ico_reserved
;
113 dstream
>> ico_count
;
115 if ((ico_reserved
!= 0) || (ico_type
!= 1) || (ico_count
< 1))
119 // now loop through each of the icon entries
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
);
155 class Factory
: public AnalyzerFactoryFactory
{
157 std::list
<StreamThroughAnalyzerFactory
*>
158 getStreamThroughAnalyzerFactories() const {
159 std::list
<StreamThroughAnalyzerFactory
*> af
;
160 af
.push_back(new IcoThroughAnalyzerFactory());
165 STRIGI_ANALYZER_FACTORY(Factory
)