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 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341
|
/* out_gif.cpp
* by pts@fazekas.hu at Sat Mar 23 11:02:36 CET 2002
*/
#include "image.hpp"
#if USE_OUT_GIF
#include <string.h>
#include <stdio.h>
/**** pts ****/
#ifdef __cplusplus
# define AALLOC(var,len,itemtype) var=new itemtype[len]
# define AFREE(expr) delete [] (expr)
#else
# include <stdlib.h> /* malloc(), free() */
# define AALLOC(var,len,itemtype) var=(itemtype*)malloc(len*sizeof(itemtype));
# define AFREE(expr) free((expr))
#endif
#define HasLZW HAVE_LZW /* Imp: or USE_BUILTIN_LZW? */
#define True true
#define False false
/* The code of GIFEncodeImage is based on an early version of ImageMagick. */
static unsigned int GIFEncodeImage(GenBuffer::Writable& out, char const*ppbeg, register char const*ppend, const unsigned int data_size)
{
#define MaxCode(number_bits) ((1 << (number_bits))-1)
#define MaxHashTable 5003
#define MaxGIFBits 12
#if defined(HasLZW)
#define MaxGIFTable (1 << MaxGIFBits)
#else
#define MaxGIFTable max_code
#endif
#define GIFOutputCode(code) \
{ \
/* \
Emit a code. \
*/ \
if (bits > 0) \
datum|=((long) code << bits); \
else \
datum=(long) code; \
bits+=number_bits; \
while (bits >= 8) \
{ \
/* \
Add a character to current packet. \
*/ \
packet[byte_count++]=(unsigned char) (datum & 0xff); \
if (byte_count >= 254) \
{ \
packet[-1]=byte_count; \
out.vi_write((char*)packet-1, byte_count+1); \
byte_count=0; \
} \
datum>>=8; \
bits-=8; \
} \
if (free_code > max_code) \
{ \
number_bits++; \
if (number_bits == MaxGIFBits) \
max_code=MaxGIFTable; \
else \
max_code=MaxCode(number_bits); \
} \
}
int
bits,
byte_count,
i,
next_pixel,
number_bits;
long
datum;
register int
displacement,
k;
register char const*pp;
short
clear_code,
end_of_information_code,
free_code,
*hash_code,
*hash_prefix,
index,
max_code,
waiting_code;
unsigned char
*packet,
*hash_suffix;
/*
Allocate encoder tables.
*/
AALLOC(packet,257,unsigned char);
AALLOC(hash_code,MaxHashTable,short);
AALLOC(hash_prefix,MaxHashTable,short);
AALLOC(hash_suffix,MaxHashTable,unsigned char);
if ((packet == (unsigned char *) NULL) || (hash_code == (short *) NULL) ||
(hash_prefix == (short *) NULL) ||
(hash_suffix == (unsigned char *) NULL))
return(False);
packet++;
/* Now: packet-1 == place for byte_count */
/*
Initialize GIF encoder.
*/
number_bits=data_size;
max_code=MaxCode(number_bits);
clear_code=((short) 1 << (data_size-1));
end_of_information_code=clear_code+1;
free_code=clear_code+2;
byte_count=0;
datum=0;
bits=0;
for (i=0; i < MaxHashTable; i++)
hash_code[i]=0;
GIFOutputCode(clear_code);
/*
Encode pixels.
*/
/**** pts ****/
pp=ppbeg;
waiting_code=*(unsigned char const*)pp++; /* unsigned char BUGFIX at Sun Dec 8 13:17:00 CET 2002 */
while (pp!=ppend) {
/*
Probe hash table.
*/
index=*(unsigned char const*)pp++;
k=(int) ((int) index << (MaxGIFBits-8))+waiting_code;
if (k >= MaxHashTable)
k-=MaxHashTable;
#if defined(HasLZW)
if (hash_code[k] > 0)
{
if ((hash_prefix[k] == waiting_code) && (hash_suffix[k] == index))
{
waiting_code=hash_code[k];
continue;
}
if (k == 0)
displacement=1;
else
displacement=MaxHashTable-k;
next_pixel=False;
for ( ; ; )
{
k-=displacement;
if (k < 0)
k+=MaxHashTable;
if (hash_code[k] == 0)
break;
if ((hash_prefix[k] == waiting_code) && (hash_suffix[k] == index))
{
waiting_code=hash_code[k];
next_pixel=True;
break;
}
}
if (next_pixel != False) /* pacify VC6.0 */
continue;
}
#endif
GIFOutputCode(waiting_code);
// printf("wc=%u\n", waiting_code);
if (free_code < MaxGIFTable)
{
hash_code[k]=free_code++;
hash_prefix[k]=waiting_code;
hash_suffix[k]=index;
}
else
{
/*
Fill the hash table with empty entries.
*/
for (k=0; k < MaxHashTable; k++)
hash_code[k]=0;
/*
Reset compressor and issue a clear code.
*/
free_code=clear_code+2;
GIFOutputCode(clear_code);
number_bits=data_size;
max_code=MaxCode(number_bits);
}
waiting_code=index;
#if 0 /**** pts ****/
if (QuantumTick(i,image) && (image->previous == (Image2 *) NULL))
ProgressMonitor(SaveImageText,i,image->packets);
#endif
}
/*
Flush out the buffered code.
*/
GIFOutputCode(waiting_code);
GIFOutputCode(end_of_information_code);
if (bits > 0)
{
/*
Add a character to current packet.
*/
packet[byte_count++]=(unsigned char) (datum & 0xff);
if (byte_count >= 254)
{
packet[-1]=byte_count;
out.vi_write((char*)packet-1, byte_count+1);
byte_count=0;
}
}
/*
Flush accumulated data.
*/
if (byte_count > 0)
{
packet[-1]=byte_count;
out.vi_write((char*)packet-1, byte_count+1);
}
/*
Free encoder memory.
*/
AFREE(hash_suffix);
AFREE(hash_prefix);
AFREE(hash_code);
AFREE(packet-1);
return pp==ppend;
}
/** This isn't a complete GIF writer. For example, it doesn't support
* animation or multiple sub-images. But it supports transparency and
* compression. Only works when . The user should call
* packPal() first to ensure img->getBpc()==8, and to get a minimal palette.
*/
void out_gif_write(GenBuffer::Writable& out, Image::Indexed *img) {
/* Tested and proven to work at Sat Mar 23 13:11:41 CET 2002 */
unsigned i, c, bits_per_pixel;
signed transp;
char hd[19];
assert(img->getBpc()==8); /* 1 palette entry == 8 bits */
transp=img->getTransp();
memcpy(hd, transp!=-1 ? "GIF89a" : "GIF87a", 6);
i=img->getWd(); hd[6]=i; hd[7]=i>>8;
i=img->getHt(); hd[8]=i; hd[9]=i>>8;
// transp=-1; /* With this, transparency will be ignored */
c=img->getNcols();
bits_per_pixel=1; while (((c-1)>>bits_per_pixel)!=0) bits_per_pixel++;
/* ^^^ (c-1) BUGFIX at Mon Oct 20 15:18:24 CEST 2003 */
/* 63 -> 6, 64 -> 6, 65 -> 7 */
// if (bits_per_pixel>1) bits_per_pixel--; /* BUGFIX at Wed Apr 30 15:55:27 CEST 2003 */ /* BUGFIX at Mon Oct 20 15:18:14 CEST 2003 */
// fprintf(stderr, "GIF89 write transp=%d ncols=%d bpp=%d\n", transp, c, bits_per_pixel);
assert(1<=bits_per_pixel && bits_per_pixel<=8);
c=3*((1<<bits_per_pixel)-c);
/* Now: c is the number of padding bytes */
hd[10]= 0x80 /* have global colormap */
| ((8-1) << 4) /* color resolution: bpc==8 */
| (bits_per_pixel-1); /* size of global colormap */
hd[11]=0; /* background color: currently unused */
hd[12]=0; /* reversed */
out.vi_write(hd, 13);
// out.vi_write("\xFF\x00\x00" "\x00\xFF\x00" "\x00\x00\xFF", 9);
out.vi_write(img->getHeadp(), img->getRowbeg()-img->getHeadp()); /* write colormap */
if (c!=0) {
char *padding=new char[(unsigned char)c]; /* BUGFIX at Fri Oct 17 18:05:09 CEST 2003 */
memset(padding, '\0', (unsigned char)c); /* Not automatic! */
out.vi_write(padding, (unsigned char)c);
delete [] padding;
}
if (transp!=-1) {
/* Write Graphics Control extension. Only GIF89a */
hd[0]=0x21; hd[1]=(char)0xf9; hd[2]=0x04;
hd[3]=transp!=-1; /* dispose==0 */
hd[4]=hd[5]=0; /* delay==0 */
hd[6]=transp; /* transparent color index -- or 255 */
hd[7]=0;
out.vi_write(hd, 8);
}
/* Write image header */
hd[8]=',';
hd[ 9]=hd[10]=0; /* left */
hd[11]=hd[12]=0; /* top */
i=img->getWd(); hd[13]=i; hd[14]=i>>8;
i=img->getHt(); hd[15]=i; hd[16]=i>>8;
hd[17]=0; /* no interlace, no local colormap, no bits in local colormap */
if ((c=bits_per_pixel)<2) c=4;
hd[18]=c; /* compression bits_per_pixel */
out.vi_write(hd+8, 11);
#if 0
printf("GIFEncodeImage out r r+%u %u; off=%u\n", img->getRlen()*img->getHt(), c+1, img->getRowbeg()-img->getHeadp());
FILE *f=fopen("tjo.dat","wb");
fprintf(f, "P6 %u %u 255\n", img->getWd(), img->getHt());
// fwrite(img->getRowbeg(), 1, img->getRlen()*img->getHt(), f);
for (unsigned u=0; u<img->getRlen()*img->getHt(); u++) {
char *p=img->getHeadp()+3* *(unsigned char*)(img->getRowbeg()+u);
putc(p[0],f);
putc(p[1],f);
putc(p[2],f);
}
#endif
i=GIFEncodeImage(out, img->getRowbeg(), img->getRowbeg()+img->getRlen()*img->getHt(), c+1);
#if 0
{ char buf[500000];
FILE *f=fopen("tjo.dat","rb");
int got=fread(buf, 1, sizeof(buf), f);
assert(got==486109);
assert(got==img->getRlen()*img->getHt());
i=GIFEncodeImage(out, buf, buf+img->getRlen()*img->getHt(), c+1);
}
#endif
assert(i!=0);
/* Write trailer */
hd[0]=0; hd[1]=';';
out.vi_write(hd, 2);
}
#else
#include <stdlib.h>
void out_gif_write(GenBuffer::Writable&, Image::Indexed *) {
assert(0);
abort();
}
#endif
|