Debian Codegraph @

src/gifSave.cpp

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