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
|
/*
* tiffsave.cpp - class for saving an ImageSource to disk as a TIFF file.
*
* Copyright (c) 2004 by Alastair M. Robinson
* Distributed under the terms of the GNU General Public License -
* see the file named "COPYING" for more details.
*
* TODO: Add support for 16-bit output, and other colour-spaces
*
*/
#include <iostream>
#include <stdlib.h>
#include <tiff.h>
#include <tiffio.h>
#include <sys/stat.h>
#include "lcmswrapper.h"
#include "tiffsave.h"
using namespace std;
TIFFSaver::~TIFFSaver()
{
if(file)
{
TIFFWriteDirectory(file);
TIFFClose(file);
}
if(tmpbuffer)
free(tmpbuffer);
}
void TIFFSaver::Save()
{
if(file)
{
int firstrow,i;
unsigned char *dst;
ISDataType *src;
for(firstrow=0;firstrow<height;firstrow+=stripheight)
{
int lastrow=firstrow+stripheight;
int row;
if(lastrow>(height))
lastrow=height;
for(row=firstrow;row<lastrow;++row)
{
if(progress && !(row&31))
{
if(!progress->DoProgress(row,height))
return;
}
dst=tmpbuffer+(row-firstrow)*(deep ? bytesperrow*2 : bytesperrow);
src=imagesource->GetRow(row);
switch(bitsperpixel)
{
case 1: // 1 bit per sample - pack 8 samples into each byte.
for(i=0;i<width;i+=8)
{
int j;
unsigned int t,t2=0;
int l=(width-i)>8 ? 7 : width-(i+1);
for(j=0;j<=l;++j)
{
t=int(src[i+l-j]);
t2>>=1;
t2|=t&0x8000;
}
dst[i/8]=t2;
}
break;
default:
if(deep) // Save with 16 bits per sample?
{
unsigned short *dst16=(unsigned short *)dst;
for(i=0;i<bytesperrow;++i)
{
dst16[i]=src[i];
}
}
else // 8 bits per sample
{
for(i=0;i<bytesperrow;++i)
{
unsigned int t;
t=int(src[i]);
t=ISTOEIGHT(t);
if(t>255) t=255;
if(t<0) t=0;
dst[i]=t;
}
}
break;
}
}
TIFFWriteEncodedStrip(file, firstrow/stripheight, tmpbuffer, stripsize);
}
if(progress)
progress->DoProgress(height,height);
}
}
void TIFFSaver::SetProgress(Progress *progress)
{
this->progress=progress;
}
static void EmbedProfile(TIFF* Out,CMSProfile *profile)
{
if(!profile)
return;
FILE* f;
size_t size, EmbedLen;
char *EmbedBuffer;
const char *fn=profile->GetFilename();
if(!fn)
return;
if(!(f = fopen(fn, "rb")))
return;
fseek(f,0,SEEK_END);
size=ftell(f);
fseek(f,0,SEEK_SET);
cerr << "Profile " << fn << " is " << size << "bytes." << endl;
EmbedBuffer = (char *) malloc(size + 1);
EmbedLen = fread(EmbedBuffer, 1, size, f);
fclose(f);
EmbedBuffer[EmbedLen] = 0;
TIFFSetField(Out, TIFFTAG_ICCPROFILE, EmbedLen, EmbedBuffer);
free(EmbedBuffer);
}
TIFFSaver::TIFFSaver(const char *filename,struct ImageSource *is,bool deep,int bpp,int compression)
: deep(deep), imagesource(is), progress(NULL)
{
switch(is->type)
{
case IS_TYPE_BW:
bitsperpixel=1;
break;
default:
bitsperpixel=is->samplesperpixel*8;
break;
}
if(bpp && (bpp!=bitsperpixel))
{
if((bitsperpixel!=8)||(bpp!=1))
throw "Currently only 8-bit -> 1-bit conversion is supported.";
}
this->width=is->width;
this->height=is->height;
this->xres=is->xres;
this->yres=is->yres;
stripheight=TIFFSAVE_STRIPHEIGHT;
tmpbuffer=NULL;
if(!(file = TIFFOpen(filename, "w")))
{
throw "Can't open file";
}
switch(bitsperpixel)
{
case 32:
if(HAS_ALPHA(is->type))
{
TIFFSetField(file, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_RGB);
TIFFSetField(file, TIFFTAG_SAMPLESPERPIXEL, 4);
TIFFSetField(file, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG);
TIFFSetField(file, TIFFTAG_BITSPERSAMPLE, deep ? 16 : 8);
}
else
{
TIFFSetField(file, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_SEPARATED);
TIFFSetField(file, TIFFTAG_SAMPLESPERPIXEL, 4);
TIFFSetField(file, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG);
TIFFSetField(file, TIFFTAG_INKSET, INKSET_CMYK);
TIFFSetField(file, TIFFTAG_BITSPERSAMPLE, deep ? 16 : 8);
}
break;
case 24:
TIFFSetField(file, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_RGB);
TIFFSetField(file, TIFFTAG_SAMPLESPERPIXEL, 3);
TIFFSetField(file, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG);
TIFFSetField(file, TIFFTAG_BITSPERSAMPLE, deep ? 16 : 8);
break;
case 8:
TIFFSetField(file, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_MINISWHITE);
TIFFSetField(file, TIFFTAG_SAMPLESPERPIXEL, 1);
TIFFSetField(file, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG);
TIFFSetField(file, TIFFTAG_BITSPERSAMPLE, deep ? 16 : 8);
break;
case 1:
TIFFSetField(file, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_MINISWHITE);
TIFFSetField(file, TIFFTAG_SAMPLESPERPIXEL, 1);
TIFFSetField(file, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG);
TIFFSetField(file, TIFFTAG_BITSPERSAMPLE, 1);
break;
default:
fprintf(stderr,"FIXME: unsupported bitspersample: %d\n",bitsperpixel);
TIFFClose(file);
throw "Unsupported image type";
break;
}
TIFFSetField(file, TIFFTAG_IMAGEWIDTH, width);
TIFFSetField(file, TIFFTAG_IMAGELENGTH, height);
TIFFSetField(file, TIFFTAG_ROWSPERSTRIP, stripheight);
double xr=xres;
double yr=yres;
TIFFSetField(file, TIFFTAG_XRESOLUTION, xr);
TIFFSetField(file, TIFFTAG_YRESOLUTION, yr);
TIFFSetField(file, TIFFTAG_RESOLUTIONUNIT, RESUNIT_INCH);
TIFFSetField(file, TIFFTAG_COMPRESSION, compression);
if(is->GetEmbeddedProfile())
EmbedProfile(file,is->GetEmbeddedProfile());
stripsize = TIFFStripSize(file);
bytesperrow = (width*bitsperpixel+7)/8;
if(!(tmpbuffer=(unsigned char *)malloc(stripsize)))
{
TIFFClose(file);
throw "No memory for tmpbuffer";
}
}
|