| 1 |
/* |
| 2 |
* This file is part of libcxxsupport. |
| 3 |
* |
| 4 |
* libcxxsupport is free software; you can redistribute it and/or modify |
| 5 |
* it under the terms of the GNU General Public License as published by |
| 6 |
* the Free Software Foundation; either version 2 of the License, or |
| 7 |
* (at your option) any later version. |
| 8 |
* |
| 9 |
* libcxxsupport 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 |
| 12 |
* GNU General Public License for more details. |
| 13 |
* |
| 14 |
* You should have received a copy of the GNU General Public License |
| 15 |
* along with libcxxsupport; if not, write to the Free Software |
| 16 |
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 17 |
*/ |
| 18 |
|
| 19 |
/* |
| 20 |
* libcxxsupport is being developed at the Max-Planck-Institut fuer Astrophysik |
| 21 |
* and financially supported by the Deutsches Zentrum fuer Luft- und Raumfahrt |
| 22 |
* (DLR). |
| 23 |
*/ |
| 24 |
|
| 25 |
/* |
| 26 |
* Classes for creation and output of image files |
| 27 |
* |
| 28 |
* Copyright (C) 2003-2015 Max-Planck-Society |
| 29 |
* Author: Martin Reinecke |
| 30 |
*/ |
| 31 |
|
| 32 |
#include <fstream> |
| 33 |
#include <sstream> |
| 34 |
#include "ls_image.h" |
| 35 |
#include "bstream.h" |
| 36 |
#include "font_data.inc" |
| 37 |
#include "string_utils.h" |
| 38 |
#include "share_utils.h" |
| 39 |
|
| 40 |
using namespace std; |
| 41 |
|
| 42 |
const MP_Font medium_bold_font = { 0, 128, 7, 13, medium_bold_font_data }; |
| 43 |
const MP_Font giant_font = { 0, 128, 9, 15, giant_font_data }; |
| 44 |
|
| 45 |
void Palette::setPredefined (int num) |
| 46 |
{ |
| 47 |
clear(); |
| 48 |
switch(num) |
| 49 |
{ |
| 50 |
case 0: |
| 51 |
add(0,Colour(0,0,0)); |
| 52 |
add(1,Colour(1,1,1)); |
| 53 |
break; |
| 54 |
case 1: |
| 55 |
add(0,Colour(0,0,0)); |
| 56 |
add(0.4f,Colour(0,0,0.5f)); |
| 57 |
add(0.75f,Colour(0,0.6f,1)); |
| 58 |
add(1,Colour(1,1,1)); |
| 59 |
break; |
| 60 |
case 4: |
| 61 |
add(0,Colour(0,0,.5f)); |
| 62 |
add(0.15f,Colour(0,0,1)); |
| 63 |
add(0.4f,Colour(0,1,1)); |
| 64 |
add(0.7f,Colour(1,1,0)); |
| 65 |
add(0.9f,Colour(1,.33f,0)); |
| 66 |
add(1,Colour(.5f,0,0)); |
| 67 |
break; |
| 68 |
case 10: // Planck colours 1 |
| 69 |
addb( 0, 0, 0,255); |
| 70 |
addb( 42, 0,112,255); |
| 71 |
addb( 85, 0,221,255); |
| 72 |
addb(127,255,237,217); |
| 73 |
addb(170,255,180, 0); |
| 74 |
addb(212,255, 75, 0); |
| 75 |
addb(255,100, 0, 0); |
| 76 |
break; |
| 77 |
case 11: // Planck colours 2 |
| 78 |
addb( 0, 0, 0,255); |
| 79 |
addb( 13, 10, 20,255); |
| 80 |
addb( 26, 30,184,255); |
| 81 |
addb( 38, 80,235,255); |
| 82 |
addb( 52,191,239,250); |
| 83 |
addb( 65,228,240,245); |
| 84 |
addb( 76,241,241,212); |
| 85 |
addb( 77,241,241,212); |
| 86 |
addb( 88,245,240,175); |
| 87 |
addb(101,248,235,130); |
| 88 |
addb(114,250,204, 38); |
| 89 |
addb(127,243,153, 13); |
| 90 |
addb(140,204, 77, 0); |
| 91 |
addb(153,165, 32, 32); |
| 92 |
addb(166,114, 0, 32); |
| 93 |
addb(179,128,128,153); |
| 94 |
addb(192,179,179,204); |
| 95 |
addb(205,204,204,230); |
| 96 |
addb(218,230,230,242); |
| 97 |
addb(231,242,242,250); |
| 98 |
addb(255,252,252,255); |
| 99 |
break; |
| 100 |
case 100: // Comptel |
| 101 |
addb( 0, 0, 0, 0); |
| 102 |
addb( 36, 64, 0,128); |
| 103 |
addb( 71, 0, 0,255); |
| 104 |
addb(117, 0,255,255); |
| 105 |
addb(152, 0,255, 0); |
| 106 |
addb(188,255,255, 0); |
| 107 |
addb(223,255, 0, 0); |
| 108 |
addb(255,255,255,255); |
| 109 |
break; |
| 110 |
default: |
| 111 |
planck_fail("Palette #"+dataToString(num)+" not yet supported."); |
| 112 |
} |
| 113 |
} |
| 114 |
|
| 115 |
void LS_Image::write_char (int xpos, int ypos, const Colour &col, char c, |
| 116 |
int scale) |
| 117 |
{ |
| 118 |
planck_assert ((c>=font.offset) && (c<font.offset+font.num_chars), |
| 119 |
"write_char: character out of range"); |
| 120 |
for (int i=0; i<font.xpix; ++i) |
| 121 |
for (int j=0; j<font.ypix; ++j) |
| 122 |
{ |
| 123 |
int ofs = (c-font.offset)*font.xpix*font.ypix + j*font.xpix + i; |
| 124 |
if (font.data[ofs]!=' ') |
| 125 |
for (int m=0; m<scale; ++m) |
| 126 |
for (int n=0; n<scale; ++n) |
| 127 |
put_pixel(xpos+scale*i+m,ypos+scale*j+n,col); |
| 128 |
} |
| 129 |
} |
| 130 |
|
| 131 |
LS_Image::LS_Image () |
| 132 |
: font(medium_bold_font) {} |
| 133 |
|
| 134 |
LS_Image::LS_Image (int xres, int yres) |
| 135 |
: font(medium_bold_font), pixel(xres,yres,Colour(0,0,0)) {} |
| 136 |
|
| 137 |
void LS_Image::annotate (int xpos, int ypos, const Colour &col, |
| 138 |
const string &text, int scale) |
| 139 |
{ |
| 140 |
for (tsize m=0; m<text.length(); ++m) |
| 141 |
write_char(xpos+m*scale*font.xpix, ypos, col, text[m],scale); |
| 142 |
} |
| 143 |
|
| 144 |
void LS_Image::annotate_centered (int xpos, int ypos, const Colour &col, |
| 145 |
const string &text, int scale) |
| 146 |
{ |
| 147 |
xpos-=(scale*text.length()*font.xpix)/2; |
| 148 |
ypos-=scale*font.ypix/2; |
| 149 |
annotate (xpos,ypos,col,text,scale); |
| 150 |
} |
| 151 |
|
| 152 |
void LS_Image::set_font (const MP_Font &fnt) |
| 153 |
{ font = fnt; } |
| 154 |
|
| 155 |
void LS_Image::write_TGA (const string &file) const |
| 156 |
{ |
| 157 |
ofstream out(file.c_str(), ios_base::out | ios_base::binary); |
| 158 |
planck_assert(out, "could not create file '" + file + "'"); |
| 159 |
|
| 160 |
tsize xres=pixel.size1(), yres=pixel.size2(); |
| 161 |
|
| 162 |
bostream bo(out); |
| 163 |
const uint8 header[18] = { 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 164 |
uint8(xres%256), uint8(xres/256), uint8(yres%256), uint8(yres/256), 24, 32}; |
| 165 |
|
| 166 |
bo.put (header, 18); |
| 167 |
vector <uint8> line(3*xres); |
| 168 |
for (tsize j=0; j<yres; ++j) |
| 169 |
{ |
| 170 |
for (tsize i=0; i<xres; ++i) |
| 171 |
{ |
| 172 |
line[i*3 ]=pixel[i][j].b; |
| 173 |
line[i*3+1]=pixel[i][j].g; |
| 174 |
line[i*3+2]=pixel[i][j].r; |
| 175 |
} |
| 176 |
bo.put(line.data(),3*xres); |
| 177 |
} |
| 178 |
planck_assert(out,"error writing output file '" + file + "'"); |
| 179 |
} |
| 180 |
|
| 181 |
namespace { |
| 182 |
|
| 183 |
void write_equal_range (const arr<Colour8> &px, tsize begin, tsize end, |
| 184 |
vector<uint8> &buf) |
| 185 |
{ |
| 186 |
chunkMaker cm (end-begin,128); |
| 187 |
uint64 cbeg, csz; |
| 188 |
while (cm.getNext(cbeg,csz)) |
| 189 |
{ |
| 190 |
buf.push_back(uint8(csz-1+128)); |
| 191 |
buf.push_back(px[begin].b); |
| 192 |
buf.push_back(px[begin].g); |
| 193 |
buf.push_back(px[begin].r); |
| 194 |
} |
| 195 |
} |
| 196 |
void write_unequal_range (const arr<Colour8> &px, tsize begin, tsize end, |
| 197 |
vector<uint8> &buf) |
| 198 |
{ |
| 199 |
chunkMaker cm (end-begin,128); |
| 200 |
uint64 cbeg, csz; |
| 201 |
while (cm.getNext(cbeg,csz)) |
| 202 |
{ |
| 203 |
buf.push_back(uint8(csz-1)); |
| 204 |
for (tsize cnt=begin+cbeg; cnt< begin+cbeg+csz; ++cnt) |
| 205 |
{ |
| 206 |
buf.push_back(px[cnt].b); |
| 207 |
buf.push_back(px[cnt].g); |
| 208 |
buf.push_back(px[cnt].r); |
| 209 |
} |
| 210 |
} |
| 211 |
} |
| 212 |
|
| 213 |
} // unnamed namespace |
| 214 |
|
| 215 |
void LS_Image::write_TGA_rle(const string &file) const |
| 216 |
{ |
| 217 |
ofstream out(file.c_str(), ios_base::out | ios_base::binary); |
| 218 |
planck_assert(out, "could not create file '" + file + "'"); |
| 219 |
|
| 220 |
tsize xres=pixel.size1(), yres=pixel.size2(); |
| 221 |
|
| 222 |
bostream bo(out); |
| 223 |
const uint8 header[18] = { 0, 0, 10, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 224 |
uint8(xres%256), uint8(xres/256), uint8(yres%256), uint8(yres/256), 24, 32}; |
| 225 |
|
| 226 |
bo.put(header,18); |
| 227 |
vector<uint8> buf; |
| 228 |
arr<Colour8> px(xres); |
| 229 |
for (tsize y=0; y<yres; ++y) |
| 230 |
{ |
| 231 |
buf.clear(); |
| 232 |
for (tsize x=0; x<xres; ++x) |
| 233 |
px[x] = pixel[x][y]; |
| 234 |
tsize xstart=0; |
| 235 |
while (xstart<xres) |
| 236 |
{ |
| 237 |
if (xstart==xres-1) |
| 238 |
{ |
| 239 |
write_unequal_range (px,xstart,xstart+1,buf); |
| 240 |
xstart=xres; |
| 241 |
} |
| 242 |
else |
| 243 |
{ |
| 244 |
if (px[xstart+1]==px[xstart]) // range of equal pixels |
| 245 |
{ |
| 246 |
tsize xend=xstart+2; |
| 247 |
while ((xend<xres) && (px[xend]==px[xstart])) ++xend; |
| 248 |
write_equal_range (px,xstart,xend,buf); |
| 249 |
xstart=xend; |
| 250 |
} |
| 251 |
else |
| 252 |
{ |
| 253 |
tsize xend=xstart+2; |
| 254 |
while ((xend<xres) && (px[xend]!=px[xend-1])) ++xend; |
| 255 |
write_unequal_range (px,xstart,xend,buf); |
| 256 |
xstart=xend; |
| 257 |
} |
| 258 |
} |
| 259 |
} |
| 260 |
bo.put(buf.data(), buf.size()); |
| 261 |
} |
| 262 |
planck_assert(out,"error writing output file '" + file + "'"); |
| 263 |
} |
| 264 |
|
| 265 |
void LS_Image::write_PPM (const string &file) const |
| 266 |
{ |
| 267 |
ofstream out(file.c_str(), ios_base::out | ios_base::binary); |
| 268 |
planck_assert(out, "could not create file '" + file + "'"); |
| 269 |
|
| 270 |
tsize xres=pixel.size1(), yres=pixel.size2(); |
| 271 |
|
| 272 |
bostream bo(out); |
| 273 |
|
| 274 |
ostringstream header; |
| 275 |
header << "P6" << endl << xres << endl << yres << endl << 255 << endl; |
| 276 |
string hdrdata = header.str(); |
| 277 |
bo.put(hdrdata.c_str(),hdrdata.size()); |
| 278 |
|
| 279 |
vector <uint8> line(3*xres); |
| 280 |
for (tsize j=0; j<yres; ++j) |
| 281 |
{ |
| 282 |
for (tsize i=0; i<xres; ++i) |
| 283 |
{ |
| 284 |
line[i*3 ]=pixel[i][j].r; |
| 285 |
line[i*3+1]=pixel[i][j].g; |
| 286 |
line[i*3+2]=pixel[i][j].b; |
| 287 |
} |
| 288 |
bo.put(line.data(),3*xres); |
| 289 |
} |
| 290 |
planck_assert(out,"error writing output file '" + file + "'"); |
| 291 |
} |