summaryrefslogtreecommitdiffstats
path: root/kfile-plugins/tga/kfile_tga.cpp
blob: 21d2c6d21e8fff87a0d33ce942be49cb1a678f4d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
/* This file is part of the KDE project
 * Copyright (C) 2002 Shane Wright <me@shanewright.co.uk>
 *
 * 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.
 *
 */

#include <config.h>
#include "kfile_tga.h"

#include <kprocess.h>
#include <klocale.h>
#include <kgenericfactory.h>
#include <kstringvalidator.h>
#include <kdebug.h>

#include <tqdict.h>
#include <tqvalidator.h>
#include <tqcstring.h>
#include <tqfile.h>
#include <tqdatetime.h>

#if !defined(__osf__)
#include <inttypes.h>
#else
typedef unsigned long uint32_t;
typedef unsigned short uint16_t;
typedef unsigned char uint8_t;
#endif

typedef KGenericFactory<KTgaPlugin> TgaFactory;

K_EXPORT_COMPONENT_FACTORY(kfile_tga, TgaFactory( "kfile_tga" ))

KTgaPlugin::KTgaPlugin(TQObject *parent, const char *name,
                       const TQStringList &args)

    : KFilePlugin(parent, name, args)
{
    KFileMimeTypeInfo* info = addMimeTypeInfo( "image/x-targa" );

    KFileMimeTypeInfo::GroupInfo* group = 0L;

    group = addGroupInfo(info, "Technical", i18n("Technical Details"));

    KFileMimeTypeInfo::ItemInfo* item;

    item = addItemInfo(group, "Dimensions", i18n("Dimensions"), TQVariant::Size);
    setHint( item, KFileMimeTypeInfo::Size );
    setUnit(item, KFileMimeTypeInfo::Pixels);

    item = addItemInfo(group, "BitDepth", i18n("Bit Depth"), TQVariant::Int);
    setUnit(item, KFileMimeTypeInfo::BitsPerPixel);

    item = addItemInfo(group, "ColorMode", i18n("Color Mode"), TQVariant::String);
    item = addItemInfo(group, "Compression", i18n("Compression"), TQVariant::String);

}

bool KTgaPlugin::readInfo( KFileMetaInfo& info, uint what)
{


    TQFile file(info.path());

    if (!file.open(IO_ReadOnly))
    {
        kdDebug(7034) << "Couldn't open " << TQFile::encodeName(info.path()) << endl;
        return false;
    }

    TQDataStream dstream(&file);

    // TGA files are little-endian
    dstream.setByteOrder(TQDataStream::LittleEndian);

    // the vars for the image data
    uint8_t  tga_idlength;
    uint8_t  tga_colormaptype;
    uint8_t  tga_imagetype;
    uint16_t tga_colormap_fei;
    uint16_t tga_colormap_length;
    uint8_t  tga_colormap_entrysize;
    uint16_t tga_imagespec_origin_x;
    uint16_t tga_imagespec_origin_y;
    uint16_t tga_imagespec_width;
    uint16_t tga_imagespec_height;
    uint8_t  tga_imagespec_depth;
    uint8_t  tga_imagespec_descriptor;

    // read the image data
    dstream >> tga_idlength;
    dstream >> tga_colormaptype;
    dstream >> tga_imagetype;
    dstream >> tga_colormap_fei;
    dstream >> tga_colormap_length;
    dstream >> tga_colormap_entrysize;
    dstream >> tga_imagespec_origin_x;
    dstream >> tga_imagespec_origin_y;
    dstream >> tga_imagespec_width;
    dstream >> tga_imagespec_height;
    dstream >> tga_imagespec_depth;
    dstream >> tga_imagespec_descriptor;

    // output the useful bits
    KFileMetaInfoGroup group = appendGroup(info, "Technical");
    appendItem(group, "Dimensions", TQSize(tga_imagespec_width, tga_imagespec_height));
    appendItem(group, "BitDepth", tga_imagespec_depth);

    switch (tga_imagetype) {
    case 1 :
    case 9 :
    case 32 :
        appendItem(group, "ColorMode", i18n("Color-Mapped"));
        break;
    case 2 :
    case 10 :
    case 33 :
        appendItem(group, "ColorMode", i18n("RGB"));
        break;
    case 3 :
    case 11 :
        appendItem(group, "ColorMode", i18n("Black and White"));
        break;
    default :
        appendItem(group, "ColorMode", i18n("Unknown"));
    }

    switch (tga_imagetype) {
    case 1 :
    case 2 :
    case 3 :
        appendItem(group, "Compression", i18n("Uncompressed"));
        break;
    case 9 :
    case 10 :
    case 11 :
        appendItem(group, "Compression", i18n("Runlength Encoded"));
        break;
    case 32 :
        appendItem(group, "Compression", i18n("Huffman, Delta & RLE"));
        break;
    case 33 :
        appendItem(group, "Compression", i18n("Huffman, Delta, RLE (4-pass quadtree)"));
        break;
    default :
        appendItem(group, "Compression", i18n("Unknown"));
    };

    return true;
}

#include "kfile_tga.moc"