filterset.cpp revision a00d232ef8ecb04fb29c1c7ea67f8e14df15b309
9e39c5ba00a55fa05777cc94b148296af305e135Bill Taylor * Some filters for Potrace in Inkscape
9e39c5ba00a55fa05777cc94b148296af305e135Bill Taylor * Bob Jamison <rjamison@titan.com>
9e39c5ba00a55fa05777cc94b148296af305e135Bill Taylor * Copyright (C) 2004 Bob Jamison
9e39c5ba00a55fa05777cc94b148296af305e135Bill Taylor * Released under GNU GPL, read the file 'COPYING' for more information
9e39c5ba00a55fa05777cc94b148296af305e135Bill Taylor/*#########################################################################
9e39c5ba00a55fa05777cc94b148296af305e135Bill Taylor### G A U S S I A N (smoothing)
9e39c5ba00a55fa05777cc94b148296af305e135Bill Taylor#########################################################################*/
9e39c5ba00a55fa05777cc94b148296af305e135Bill Taylor for (int y = 0 ; y<height ; y++)
9e39c5ba00a55fa05777cc94b148296af305e135Bill Taylor for (int x = 0 ; x<width ; x++)
9e39c5ba00a55fa05777cc94b148296af305e135Bill Taylor /* image boundaries */
9e39c5ba00a55fa05777cc94b148296af305e135Bill Taylor newGm->setPixel(newGm, x, y, me->getPixel(me, x, y));
9e39c5ba00a55fa05777cc94b148296af305e135Bill Taylor /* all other pixels */
9e39c5ba00a55fa05777cc94b148296af305e135Bill Taylor unsigned long sum = 0;
9e39c5ba00a55fa05777cc94b148296af305e135Bill Taylor for (int y = 0 ; y<height ; y++)
9e39c5ba00a55fa05777cc94b148296af305e135Bill Taylor for (int x = 0 ; x<width ; x++)
9e39c5ba00a55fa05777cc94b148296af305e135Bill Taylor /* image boundaries */
9e39c5ba00a55fa05777cc94b148296af305e135Bill Taylor newGm->setPixelRGB(newGm, x, y, me->getPixel(me, x, y));
9e39c5ba00a55fa05777cc94b148296af305e135Bill Taylor /* all other pixels */
9e39c5ba00a55fa05777cc94b148296af305e135Bill Taylor/*#########################################################################
9e39c5ba00a55fa05777cc94b148296af305e135Bill Taylor### C A N N Y E D G E D E T E C T I O N
9e39c5ba00a55fa05777cc94b148296af305e135Bill Taylor#########################################################################*/
9e39c5ba00a55fa05777cc94b148296af305e135Bill Taylorstatic int sobelX[] =
9e39c5ba00a55fa05777cc94b148296af305e135Bill Taylorstatic int sobelY[] =
9e39c5ba00a55fa05777cc94b148296af305e135Bill Taylor * Perform Sobel convolution on a GrayMap
9e39c5ba00a55fa05777cc94b148296af305e135Bill Taylor for (int y = 0 ; y<height ; y++)
9e39c5ba00a55fa05777cc94b148296af305e135Bill Taylor for (int x = 0 ; x<width ; x++)
9e39c5ba00a55fa05777cc94b148296af305e135Bill Taylor unsigned long sum = 0;
9e39c5ba00a55fa05777cc94b148296af305e135Bill Taylor /* image boundaries */
9e39c5ba00a55fa05777cc94b148296af305e135Bill Taylor /* ### SOBEL FILTERING #### */
9e39c5ba00a55fa05777cc94b148296af305e135Bill Taylor /*### GET VALUE ### */
9e39c5ba00a55fa05777cc94b148296af305e135Bill Taylor /*### GET ORIENTATION (slow, pedantic way) ### */
9e39c5ba00a55fa05777cc94b148296af305e135Bill Taylor orient = 57.295779515 * atan2( ((double)sumY),((double)sumX) );
9e39c5ba00a55fa05777cc94b148296af305e135Bill Taylor /*### GET EDGE DIRECTION ### */
9e39c5ba00a55fa05777cc94b148296af305e135Bill Taylor /*### GET EDGE DIRECTION (fast way) ### */
9e39c5ba00a55fa05777cc94b148296af305e135Bill Taylor /*long slope = sumY*1024/sumX;*/
9e39c5ba00a55fa05777cc94b148296af305e135Bill Taylor if (slope > 2472 || slope< -2472) /*tan(67.5)*1024*/
9e39c5ba00a55fa05777cc94b148296af305e135Bill Taylor /* printf("%ld %ld %f %d\n", sumX, sumY, orient, edgeDirection); */
9e39c5ba00a55fa05777cc94b148296af305e135Bill Taylor /*### Get two adjacent pixels in edge direction ### */
9e39c5ba00a55fa05777cc94b148296af305e135Bill Taylor unsigned long leftPixel;
9e39c5ba00a55fa05777cc94b148296af305e135Bill Taylor unsigned long rightPixel;
9e39c5ba00a55fa05777cc94b148296af305e135Bill Taylor else /*135 */
9e39c5ba00a55fa05777cc94b148296af305e135Bill Taylor /*### Compare current value to adjacent pixels ### */
9e39c5ba00a55fa05777cc94b148296af305e135Bill Taylor /*### if less that either, suppress it ### */
9e39c5ba00a55fa05777cc94b148296af305e135Bill Taylor unsigned long highThreshold =
9e39c5ba00a55fa05777cc94b148296af305e135Bill Taylor unsigned long lowThreshold =
9e39c5ba00a55fa05777cc94b148296af305e135Bill Taylor sum = 765; /* EDGE. 3*255 this needs to be settable */
9e39c5ba00a55fa05777cc94b148296af305e135Bill Taylor if ( gm->getPixel(gm, x-1, y-1)> highThreshold ||
9e39c5ba00a55fa05777cc94b148296af305e135Bill Taylor }/* for (x) */
9e39c5ba00a55fa05777cc94b148296af305e135Bill Taylor }/* for (y) */
9e39c5ba00a55fa05777cc94b148296af305e135Bill TaylorgrayMapCanny(GrayMap *gm, double lowThreshold, double highThreshold)
9e39c5ba00a55fa05777cc94b148296af305e135Bill Taylor /*gaussGm->writePPM(gaussGm, "gauss.ppm");*/
9e39c5ba00a55fa05777cc94b148296af305e135Bill Taylor GrayMap *cannyGm = grayMapSobel(gaussGm, lowThreshold, highThreshold);
9e39c5ba00a55fa05777cc94b148296af305e135Bill Taylor /*cannyGm->writePPM(cannyGm, "canny.ppm");*/
9e39c5ba00a55fa05777cc94b148296af305e135Bill TaylorgdkCanny(GdkPixbuf *img, double lowThreshold, double highThreshold)
9e39c5ba00a55fa05777cc94b148296af305e135Bill Taylor /*grayMap->writePPM(grayMap, "gbefore.ppm");*/
9e39c5ba00a55fa05777cc94b148296af305e135Bill Taylor GrayMap *cannyGm = grayMapCanny(grayMap,lowThreshold, highThreshold);
9e39c5ba00a55fa05777cc94b148296af305e135Bill Taylor /*grayMap->writePPM(grayMap, "gafter.ppm");*/
9e39c5ba00a55fa05777cc94b148296af305e135Bill Taylor GdkPixbuf *newImg = grayMapToGdkPixbuf(cannyGm);
9e39c5ba00a55fa05777cc94b148296af305e135Bill Taylor/*#########################################################################
9e39c5ba00a55fa05777cc94b148296af305e135Bill Taylor### Q U A N T I Z A T I O N
9e39c5ba00a55fa05777cc94b148296af305e135Bill Taylor#########################################################################*/
9e39c5ba00a55fa05777cc94b148296af305e135Bill Taylor * The basic octree node
9e39c5ba00a55fa05777cc94b148296af305e135Bill Taylor unsigned long r;
9e39c5ba00a55fa05777cc94b148296af305e135Bill Taylor unsigned long g;
9e39c5ba00a55fa05777cc94b148296af305e135Bill Taylor unsigned long b;
9e39c5ba00a55fa05777cc94b148296af305e135Bill Taylor unsigned int index;
9e39c5ba00a55fa05777cc94b148296af305e135Bill Taylor unsigned long nrPixels;
9e39c5ba00a55fa05777cc94b148296af305e135Bill Taylor * create an octree node, and initialize it
9e39c5ba00a55fa05777cc94b148296af305e135Bill Taylor OctreeNode *node = (OctreeNode *)malloc(sizeof(OctreeNode));
9e39c5ba00a55fa05777cc94b148296af305e135Bill Taylor for (int i=0 ; i<8 ; i++)
9e39c5ba00a55fa05777cc94b148296af305e135Bill Taylor * delete an octree node and its children
9e39c5ba00a55fa05777cc94b148296af305e135Bill Taylor for (int i=0 ; i<8 ; i++)
9e39c5ba00a55fa05777cc94b148296af305e135Bill Taylor * delete the children of an octree node
9e39c5ba00a55fa05777cc94b148296af305e135Bill Taylor for (int i=0 ; i<8 ; i++)
9e39c5ba00a55fa05777cc94b148296af305e135Bill Taylor * insert an RGB value into an octree node according to its
9e39c5ba00a55fa05777cc94b148296af305e135Bill Taylor * high-order rgb vector bits
9e39c5ba00a55fa05777cc94b148296af305e135Bill Taylorint octreeNodeInsert(OctreeNode *root, RGB rgb, int bitsPerSample)
9e39c5ba00a55fa05777cc94b148296af305e135Bill Taylor /* update values of all nodes from the root to the leaf */
int r = rgb.r;
int g = rgb.g;
int b = rgb.b;
if (!child)
shift--;
for (int i=0; i<nr ; i++)
if (!child)
if (!child)
return count;
if (!node)
if (!node)
int leaves = 0;
return leaves;
if (!node)
if (!node)
if (!lowestLeaf)
if (!parent)
int index = 0;
return leafCount;
if (!root)
return NULL;
return NULL;
return root;
static int compRGB(const void *a, const void *b)
return comp;
if (!palette)
return NULL;
int len = 0;
if (!rgbpal)
return NULL;
for (int i=0; i<len ; i++)
return rgbpal;
*closestIndex = 0;
for (int i=0 ; i<paletteSize ; i++)
*closestIndex = i;
return closestRGB;
if (!rgbMap)
return NULL;
if (!otree)
return NULL;
if (!rgbpal)
return NULL;
if (!newMap)
return NULL;
for (int i=0 ; i< nrColors ; i++)
int closestIndex;
return newMap;
sum = 0;
return gm;