| 1 |
/* |
| 2 |
GIFWRAPPER! |
| 3 |
|
| 4 |
This file is included in QuteMol project |
| 5 |
as temporary hack waiting for WxWidgets to support |
| 6 |
saving in GIF format (patent is now expired!) |
| 7 |
|
| 8 |
*/ |
| 9 |
|
| 10 |
#include <gif_lib.h> |
| 11 |
#include <stdio.h> |
| •12 |
#include <sys/types.h> |
| 13 |
#include <sys/stat.h> |
| 14 |
|
| 15 |
#include <vector> |
| 16 |
|
| 17 |
using namespace std; |
| 18 |
|
| 19 |
typedef unsigned char Byte; |
| 20 |
|
| 21 |
typedef vector<GifByteType> Frame; |
| 22 |
|
| 23 |
#include "gifSave.h" |
| 24 |
|
| 25 |
// global vairables |
| 26 |
|
| 27 |
|
| 28 |
static vector<Frame> frames; |
| 29 |
static vector<int> delay; |
| 30 |
|
| 31 |
static ColorMapObject* outputPalette; |
| 32 |
|
| 33 |
GifWrapper::GifWrapper(){ |
| 34 |
frames.clear(); |
| 35 |
delay.clear(); |
| 36 |
} |
| 37 |
|
| 38 |
|
| 39 |
static int gifsx, gifsy; |
| 40 |
bool GifWrapper::AddFrame(Byte* data, int sx, int sy, float dt){ |
| 41 |
|
| 42 |
gifsx=sx; |
| 43 |
gifsy=sy; |
| 44 |
|
| 45 |
unsigned int npix=sx*sy; |
| 46 |
|
| 47 |
|
| 48 |
int paletteSize=256; |
| 49 |
|
| 50 |
|
| 51 |
Frame output(npix);//=(Frame) malloc(npix); |
| 52 |
|
| 53 |
|
| 54 |
if (frames.size()==0) { |
| 55 |
|
| 56 |
|
| 57 |
Frame r(npix),g(npix),b(npix); |
| 58 |
|
| 59 |
// de-interlaeve |
| •60 |
for (unsigned int i=0, j=0; i<npix; i++){ |
| 61 |
r[i]=data[j++]; |
| 62 |
g[i]=data[j++]; |
| 63 |
b[i]=data[j++]; |
| 64 |
} |
| 65 |
|
| •66 |
outputPalette = GifMakeMapObject(paletteSize, NULL); |
| 67 |
if (!outputPalette) return false; |
| 68 |
|
| •69 |
if (GifQuantizeBuffer(sx, sy, &paletteSize, |
| 70 |
&(r[0]),&(g[0]),&(b[0]), &(output[0]), |
| 71 |
outputPalette->Colors) == GIF_ERROR) return false; |
| 72 |
|
| 73 |
|
| 74 |
} else { |
| 75 |
// maunal assignment of color indices |
| •76 |
for (unsigned int i = 0, j=0; i < npix; i++) { |
| 77 |
int minIndex = 0, |
| 78 |
minDist = 3 * 256 * 256; |
| 79 |
GifColorType *c = outputPalette->Colors; |
| 80 |
|
| 81 |
/* Find closest color in first color map to this color. */ |
| 82 |
for (int k = 0; k < outputPalette->ColorCount; k++) { |
| 83 |
int dr = (int(c[k].Red) - data[j] ) ; |
| 84 |
int dg = (int(c[k].Green) - data[j+1] ) ; |
| 85 |
int db = (int(c[k].Blue) - data[j+2] ) ; |
| 86 |
|
| 87 |
int dist=dr*dr+dg*dg+db*db; |
| 88 |
|
| 89 |
if (minDist > dist) { |
| 90 |
minDist = dist; |
| 91 |
minIndex = k; |
| 92 |
} |
| 93 |
} |
| 94 |
j+=3; |
| 95 |
output[i] = minIndex; |
| 96 |
} |
| 97 |
|
| 98 |
} |
| 99 |
|
| 100 |
frames.push_back(output); |
| 101 |
|
| 102 |
delay.push_back(int(dt*100.0)); |
| 103 |
return true; |
| 104 |
} |
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
bool AddLoop(GifFileType *gf){ |
| 109 |
|
| 110 |
/* yes this was another attempt at supporting the loop extension */ |
| 111 |
int loop_count; |
| 112 |
loop_count=0; |
| 113 |
{ |
| 114 |
unsigned char nsle[12] = "NETSCAPE2.0"; |
| 115 |
unsigned char subblock[3]; |
| 116 |
//if (EGifPutExtension(gf, 0xFF, 11, nsle) == GIF_ERROR) { |
| •117 |
if (EGifPutExtensionLeader(gf, APPLICATION_EXT_FUNC_CODE) == GIF_ERROR) { |
| 118 |
return false; |
| 119 |
} |
| 120 |
if (EGifPutExtensionBlock(gf, 11, nsle) == GIF_ERROR) { |
| 121 |
//gif_push_error(); |
| 122 |
//i_push_error(0, "writing loop extension"); |
| 123 |
return false; |
| 124 |
} |
| 125 |
subblock[0] = 1; |
| 126 |
subblock[2] = loop_count % 256; |
| 127 |
subblock[1] = loop_count / 256; |
| 128 |
//if (EGifPutExtension(gf, 0, 3, subblock) == GIF_ERROR) { |
| •129 |
if (EGifPutExtensionTrailer(gf) == GIF_ERROR) { |
| 130 |
//gif_push_error(); |
| 131 |
//i_push_error(0, "writing loop extention sub-block"); |
| 132 |
return false; |
| 133 |
} |
| 134 |
#if 0 |
| 135 |
if (EGifPutExtension(gf, 0, 0, subblock) == GIF_ERROR) { |
| 136 |
gif_push_error(); |
| 137 |
i_push_error(0, "writing loop extension terminator"); |
| 138 |
return false; |
| 139 |
} |
| 140 |
#endif |
| 141 |
} |
| 142 |
return true; |
| 143 |
} |
| 144 |
|
| 145 |
|
| 146 |
bool GifWrapper::Save(const char* filename){ |
| 147 |
if (frames.size()==0) return false; |
| 148 |
|
| 149 |
|
| •150 |
GifFileType *GifFile = EGifOpenFileName(filename, 0, NULL); |
| 151 |
|
| 152 |
if (!GifFile) return false; |
| 153 |
|
| 154 |
if (EGifPutScreenDesc( |
| 155 |
GifFile, |
| 156 |
gifsx, gifsy, 8, 0, |
| 157 |
outputPalette |
| 158 |
) == GIF_ERROR) return false; |
| 159 |
|
| 160 |
if (!AddLoop(GifFile)) return false; |
| 161 |
|
| •162 |
for (int ni=0; ni < (int)frames.size(); ni++) { |
| 163 |
|
| 164 |
static unsigned char |
| 165 |
ExtStr[4] = { 0x04, 0x00, 0x00, 0xff }; |
| 166 |
|
| 167 |
|
| 168 |
ExtStr[0] = (false) ? 0x06 : 0x04; |
| 169 |
ExtStr[1] = delay[ni] % 256; |
| 170 |
ExtStr[2] = delay[ni] / 256; |
| 171 |
|
| 172 |
/* Dump graphics control block. */ |
| 173 |
EGifPutExtension(GifFile, GRAPHICS_EXT_FUNC_CODE, 4, ExtStr); |
| 174 |
|
| 175 |
|
| 176 |
if (EGifPutImageDesc( |
| 177 |
GifFile, |
| •178 |
0, 0, gifsx, gifsy, 0, NULL |
| 179 |
) == GIF_ERROR) return false; |
| 180 |
|
| 181 |
|
| 182 |
for (int y = 0, j=(gifsy-1)*gifsx; y < gifsy; y++, j-=gifsx) { |
| 183 |
if (EGifPutLine(GifFile, &(frames[ni][j]), gifsx) == GIF_ERROR) return false; |
| 184 |
} |
| 185 |
} |
| 186 |
|
| •187 |
if (EGifCloseFile(GifFile, NULL) == GIF_ERROR) return false; |
| 188 |
|
| •189 |
mode_t mask = umask(0); |
| 190 |
umask(mask); |
| 191 |
chmod(filename, mask ^ 0666); |
| 192 |
|
| 193 |
return true; |
| 194 |
} |
| 195 |
|
| 196 |
|