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 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280
|
/*
* Copyright (C) 2007 - 2023 René Rebe, ExactCODE GmbH
*
* 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. A copy of the GNU General
* Public License can be found in the file LICENSE.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANT-
* ABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
* Public License for more details.
*
* Alternatively, commercial licensing options are available from the
* copyright holder ExactCODE GmbH Germany.
*/
#include <ctype.h>
#include <iostream>
#include <string>
#include <sstream>
#include <vector>
#include "xpm.hh"
#include "Colorspace.hh"
// Format:
//
// /* XPM */"
// static char * XFACE[] = {
// "48 48 2 1",
// "a c #ffffff",
// "b c #000000",
// "abaabaababaaabaabababaabaabaababaabaaababaabaaab"
// ...
uint8_t parse_hex (std::istream* stream)
{
uint8_t x = 0;
char c = tolower (stream->get());
if (c >= '0' && c <= '9')
x = (x << 4) | (c - '0');
else
x = (x << 4) | (c - 'a' + 10);
c = tolower (stream->get());
if (c >= '0' && c <= '9')
x = (x << 4) | (c - '0');
else
x = (x << 4) | (c - 'a' + 10);
return x;
}
void skip_comments (std::istream* stream)
{
if (stream->peek() == '/')
{
stream->get();
if (stream->peek() == '*')
{
// std::cerr << "comment" << std::endl;
do {
char c = stream->get();
// std::cerr << c;
if (c == '*' && stream->peek() == '/') {
stream->get();
break;
}
} while (*stream);
while (*stream && stream->peek() == '\n')
stream->get();
// std::cerr << std::endl;
}
else
stream->putback('/');
}
}
int XPMCodec::readImage (std::istream* stream, Image& image, const std::string& decompress)
{
// check signature
std::string line;
std::getline (*stream, line);
if (line != "/* XPM */") {
stream->seekg (0);
return false;
}
// TODO: write a more sophisticated "C" parser here
// drop the C declaration
std::getline (*stream, line);
skip_comments (stream);
// dimensions and #colors
if (stream->peek() == '"')
stream->get();
int colors, cpp; // chars per pixel
*stream >> image.w >> image.h >> colors >> cpp;
std::getline (*stream, line);
if (false)
std::cerr << "XPM: " << image.w << "x" << image.h
<< ", colors: " << colors << ", chars per pix: "
<< cpp << std::endl;
#ifndef _MSC_VER
uint16_t rmap[colors];
uint16_t gmap[colors];
uint16_t bmap[colors];
#else
std::vector<uint16_t> rmap(colors);
std::vector<uint16_t> gmap(colors);
std::vector<uint16_t> bmap(colors);
#endif
std::vector<std::string> cmap;
skip_comments (stream);
// read color definitions
for (int c = 0; c < colors; ++c)
{
std::string name, type;
// std::cerr << "Reading color: " << c << std::endl;
if (stream->peek() == '"')
stream->get();
for (int i = 0; i < cpp; ++i)
name.push_back (stream->get());
*stream >> type;
// Type: c -> colour, m -> monochrome, g -> grayscale, and s -> symbolic
if (type != "c") {
std::cerr << "XPM color type: '" << type << "' not yet implemented." << std::endl;
return false;
}
while (stream->peek() == ' ')
stream->get();
if (stream->peek() == '#') {
stream->get();
rmap[c] = parse_hex (stream) << 8;
gmap[c] = parse_hex (stream) << 8;
bmap[c] = parse_hex (stream) << 8;
std::getline (*stream, line);
}
else {
rmap[c] = gmap[c] = bmap[c] = 0;
std::getline (*stream, line);
if (line != "None\",")
std::cerr << "Unrecognized color: " << line << std::endl;
}
// symbol -> index map
cmap.push_back (name);
//std::cerr << cmap[c] << ": " << rmap[c] << " " << gmap[c] << " " << bmap[c] << std::endl;
}
image.bps = 8; // for now, later this could be optimized
image.spp = 1; // for now, later this could be optimized
image.resize (image.w, image.h);
image.setResolution(0, 0);
skip_comments (stream);
// read in the pixel data
uint8_t* dst = image.getRawData();
std::string last = ""; // to cache to avoid character index lookup
int c = 0;
for (int y = 0; y < image.h; ++y)
{
if (stream->peek() == '"')
stream->get();
for (int x = 0; x < image.w; ++x)
{
std::string str;
for (int i = 0; i < cpp; ++i)
str.append (1, (char)stream->get());
if (str != last) { // optimization: avoid re-search
// TODO: make this a hash lookup ...
std::vector<std::string>::iterator it =
find (cmap.begin(), cmap.end(), str);
if (it == cmap.end())
std::cerr << "Not in color map: '" << str << "'!" << std::endl;
else {
c = (it - cmap.begin());
last = str;
}
}
*dst++ = c;
}
std::getline (*stream, line);
}
colorspace_de_palette (image, colors, &rmap[0], &gmap[0], &bmap[0]);
return true;
}
std::string symbol (int i)
{
std::string s;
s.append (1, (char) ('a' + i));
return s;
}
std::string put_hex (unsigned char hi)
{
std::string s;
unsigned char lo = hi & 0xF;
hi >>= 4;
if (hi < 10)
s.append (1, (char)'0' + hi);
else
s.append (1, (char)'A' + hi - 10);
if (lo < 10)
s.append (1, (char)'0' + lo);
else
s.append (1, (char)'A' + lo - 10);
return s;
}
bool XPMCodec::writeImage (std::ostream* stream, Image& image, int quality,
const std::string& compress)
{
// TODO: count colors later
if (image.spp > 1) {
std::cerr << "Too many colors for XPM." << std::endl;
return false;
}
int colors = (1 << image.bps);
*stream << "/* XPM */\n"
<< "static char * ExactImage[] = {\n"
<< "\"" << image.w << " " << image.h << " "
<< colors << " " << 1 /* cpp */ << "\",\n";
// write colors
for (int c = 0; c < colors; ++c)
{
int l = c * 255 / (colors-1);
*stream << "\"" << symbol (c) << "\t" << "c #"
<< put_hex(l)
<< put_hex(l)
<< put_hex(l)
<< "\",\n";
}
// write pixels
Image::iterator it = image.begin();
for (int y = 0; y < image.h; ++y)
{
it = it.at(0, y);
*stream << "\"";
for (int x = 0; x < image.w; ++x)
{
*it;
*stream << symbol (it.getL() >> (8 - image.bps) );
++it;
}
*stream << "\"" << (y < image.h-1 ? ",\n" : "};\n");
}
return true;
}
XPMCodec xpm_loader;
|