[d5d988]: oct_quan.cpp  Maximize  Restore  History

Download this file

302 lines (261 with data), 6.8 kB

  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
// OCT_QUAN.CPP - Octree Color Quantization Class
#include "stdafx.h"
#include <stdio.h>
#include "oct_quan.h"
#include "chitem.h"
#include "options.h"
#include "Dl1quant.h"
// Color palette matching
// Build octree
// quantizeall colors deletes the tree
int OctQuant::QuantizeAllColors(LPBYTE pFrameData, const DWORD *pRawBits, CPoint nDimension, palettetype &palette)
{
int nPixelNum=nDimension.x*nDimension.y;
OctColor color;
int i;
int qualityloss;
int qcolor;
qualityloss=0;
for(i=0;i<nPixelNum;i++)
{
memcpy(&color.data,pRawBits+i,3);
qcolor = QuantizeColor(proot, color);
pFrameData[i]=(BYTE) qcolor;
qualityloss+=ChiSquare((LPBYTE) &color.data,(LPBYTE) (palette+qcolor) );
}
DeleteTree();
return qualityloss;
}
int OctQuant::QuantizeAllColors(LPBYTE pFrameData, const LPBYTE pRawBits, CPoint nDimension,
palettetype &oldpalette, palettetype &palette)
{
int nPixelNum=nDimension.x*nDimension.y;
OctColor color;
int i;
int qualityloss;
int qcolor;
qualityloss=0;
for(i=0;i<nPixelNum;i++)
{
//this will work only if both bams got the 0. index as transparency, but it is default
if(pRawBits[i])
{
memcpy(&color.data,oldpalette+pRawBits[i],3);
qcolor = QuantizeColor(proot, color);
}
else
{
qcolor=0;
}
pFrameData[i]=(BYTE) qcolor;
qualityloss+=ChiSquare((LPBYTE) &color.data,(LPBYTE) (palette+qcolor) );
}
DeleteTree();
return qualityloss;
}
int OctQuant::AddPalette(palettetype &palette)
{
OctColor color;
int i;
if(!proot)
{
if ((proot = MakeNode(0)) == NULL)
return -1;
}
for (i = 0; i < 256; i++)
{
// Insert pixel color into octree
memcpy(&color.data,palette+i,3);
if (InsertNode(proot, color, i ) == FALSE)
{
DeleteTree(); // Delete the octree
return -1;
}
// Reduce octree if too many colors
if (num_leaf > O_NumColor)
ReduceTree();
}
return 0;
}
int OctQuant::BuildTree(const DWORD *pRawBits, LPBYTE pFrameData, CPoint nDimension, palettetype &palette)
{
int nPixelNum=nDimension.x*nDimension.y;
int i;
OctColor color;
if(editflg&OCTREE)
{
return dl1quant((LPBYTE) pRawBits, pFrameData, nDimension, palette);
}
if(proot) return -1;
// Allocate octree root node
if ((proot = MakeNode(0)) == NULL)
return -1;
// Build the octree
for (i = 0; i < nPixelNum; i++)
{
// Insert pixel color into octree
memcpy(&color.data,pRawBits+i,3);
if (InsertNode(proot, color, -1 ) == FALSE)
{
DeleteTree(); // Delete the octree
return -1;
}
// Reduce octree if too many colors
if (num_leaf > O_NumColor)
ReduceTree();
}
i=0;
FillPalette(proot, palette, &i);
return QuantizeAllColors(pFrameData, pRawBits, nDimension, palette);
}
// Recursively delete octree nodes
void OctQuant::DeleteNode( OctNode *pn )
{
int i; // Loop index
OctNode *pc; // Child node pointer
if (pn == NULL)
return;
if (pn->IsLeaf() == FALSE)
{
// Delete child nodes
for (i = 0; i < 8; i++)
{
if ((pc = pn->GetChild(i)) != NULL)
{
DeleteNode(pc);
pn->SetChild(i, NULL);
pn->DecNumChild();
}
}
}
else
num_leaf--;
delete pn;
pn=NULL;
}
// Set color palette entries
void OctQuant::FillPalette( OctNode *pn, palettetype &Palette, int *pindex)
{
int i; // Loop index
// Perform recursive depth-first traversal of octree
if (pn != NULL)
{
if ((pn->IsLeaf() == TRUE) || pn->GetLevel() ==
leaf_level)
{
// Set color palette entry
Palette[*pindex] = pn->GetColor();
// Set node color palette index
pn->SetIndex(*pindex);
// Advance to next color palette entry
*pindex = *pindex + 1;
}
else
{
// Visit child nodes
for (i = 0; i < 8; i++)
FillPalette(pn->GetChild(i), Palette, pindex);
}
}
}
// Get next reducible node pointer
OctNode *OctQuant::GetReducible()
{
int new_level; // New reducible node level
OctNode *prn; // Reducible node pointer
OctNode *plcn = NULL; // Largest pixel count node
OctNode *pnext; // Next node
OctNode *pprev; // Previous node
new_level = leaf_level - 1;
// Find lowest reducible node level
while (prnl[new_level] == NULL)
new_level--;
// Find node with largest pixel count
prn = prnl[new_level];
while (prn != NULL)
{
if (plcn == NULL)
plcn = prn;
else if (prn->GetCount() < plcn->GetCount())
plcn = prn;
prn = prn->GetNext();
}
// Remove node from reducible list
pnext = plcn->GetNext();
pprev = plcn->GetPrev();
if (pprev == NULL)
{
prnl[new_level] = pnext;
if (pnext != NULL)
pnext->SetPrev(NULL);
}
else
{
pprev->SetNext(pnext);
if (pnext != NULL)
pnext->SetPrev(pprev);
}
plcn->SetNext(NULL);
plcn->SetPrev(NULL);
plcn->SetMark(FALSE);
return plcn;
}
void OctQuant::ReduceTree() // Reduce octree
{
int i; // Loop index
OctNode *pn; // Node pointer
OctNode *pc; // Child node pointer
pn = GetReducible(); // Get next reducible node
// Delete children
for (i = 0; i < 8; i++)
{
if ((pc = pn->GetChild(i)) != NULL)
{
DeleteNode(pc);
pn->SetChild(i, NULL);
pn->DecNumChild();
}
}
pn->SetLeaf(TRUE); // Mark node as leaf
num_leaf++; // Increment leaf count
// Update reduction and leaf levels
if (pn->GetLevel() < (leaf_level - 1))
leaf_level = pn->GetLevel() + 1;
}
// Insert node into octree
BOOL OctQuant::InsertNode( OctNode *pn, OctColor &c, int idx)
{
int c_index; // Child index
int level; // Node level
BOOL status = TRUE; // Return status
OctNode *pc; // Child node pointer
level = pn->GetLevel(); // Get node level
pn->AddColor(c); // Add RGB color to node
if (pn->IsLeaf() == FALSE && level < leaf_level)
{
// Find child node
c_index = pn->FindChild(c);
if ((pc = pn->GetChild(c_index)) != NULL)
{
if ((pn->GetNumChild() > 1) && (pn->IsMark() ==
FALSE))
{
// Mark node as candidate for reduction
MakeReducible(pn);
}
}
else
{
// Allocate child node
if ((pc = MakeNode(level + 1)) == NULL)
return FALSE;
// Set child node pointer
pn->SetChild(c_index, pc);
pn->IncNumChild(); // Increment child count
}
// Insert child node into octree
status = InsertNode(pc, c, idx);
}
if(idx!=-1) pn->SetIndex(idx);
return status;
}

Get latest updates about Open Source Projects, Conferences and News.

Sign up for the SourceForge newsletter:





No, thanks