Debian Codegraph @

src/gifSave.cpp

raw symbols ◀ ▶
Source reconstructed at patch state 2 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 #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 = MakeMapObject(paletteSize, NULL);
67 if (!outputPalette) return false;
68
69 if (QuantizeBuffer(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 (EGifPutExtensionFirst(gf, APPLICATION_EXT_FUNC_CODE, 11, nsle) == GIF_ERROR) {
118 //gif_push_error();
119 //i_push_error(0, "writing loop extension");
120 return false;
121 }
122 subblock[0] = 1;
123 subblock[2] = loop_count % 256;
124 subblock[1] = loop_count / 256;
125 //if (EGifPutExtension(gf, 0, 3, subblock) == GIF_ERROR) {
126 if (EGifPutExtensionLast(gf, APPLICATION_EXT_FUNC_CODE, 3, subblock) == GIF_ERROR) {
127 //gif_push_error();
128 //i_push_error(0, "writing loop extention sub-block");
129 return false;
130 }
131 #if 0
132 if (EGifPutExtension(gf, 0, 0, subblock) == GIF_ERROR) {
133 gif_push_error();
134 i_push_error(0, "writing loop extension terminator");
135 return false;
136 }
137 #endif
138 }
139 return true;
140 }
141
142
143 bool GifWrapper::Save(const char* filename){
144 if (frames.size()==0) return false;
145
146
147 GifFileType *GifFile = EGifOpenFileName(filename, FALSE);
148
149 if (!GifFile) return false;
150
151 if (EGifPutScreenDesc(
152 GifFile,
153 gifsx, gifsy, 8, 0,
154 outputPalette
155 ) == GIF_ERROR) return false;
156
157 if (!AddLoop(GifFile)) return false;
158
159 for (int ni=0; ni < (int)frames.size(); ni++) {
160
161 static unsigned char
162 ExtStr[4] = { 0x04, 0x00, 0x00, 0xff };
163
164
165 ExtStr[0] = (false) ? 0x06 : 0x04;
166 ExtStr[1] = delay[ni] % 256;
167 ExtStr[2] = delay[ni] / 256;
168
169 /* Dump graphics control block. */
170 EGifPutExtension(GifFile, GRAPHICS_EXT_FUNC_CODE, 4, ExtStr);
171
172
173 if (EGifPutImageDesc(
174 GifFile,
175 0, 0, gifsx, gifsy, FALSE, NULL
176 ) == GIF_ERROR) return false;
177
178
179 for (int y = 0, j=(gifsy-1)*gifsx; y < gifsy; y++, j-=gifsx) {
180 if (EGifPutLine(GifFile, &(frames[ni][j]), gifsx) == GIF_ERROR) return false;
181 }
182 }
183
184 if (EGifCloseFile(GifFile) == GIF_ERROR) return false;
185
186 mode_t mask = umask(0);
187 umask(mask);
188 chmod(filename, mask ^ 0666);
189
190 return true;
191 }
192
193