filterset.cpp revision 1958a512623ac2a147c228b63bf323e3d199fd8b
/*
* Some filters for Potrace in Inkscape
*
* Authors:
* Bob Jamison <rjamison@titan.com>
* Stéphane Gimenez <dev@gim.name>
*
* Copyright (C) 2004-2006 Authors
*
* Released under GNU GPL, read the file 'COPYING' for more information
*/
#include <cstdio>
#include <stdlib.h>
#include "imagemap-gdk.h"
#include "filterset.h"
#include "quantize.h"
/*#########################################################################
### G A U S S I A N (smoothing)
#########################################################################*/
/**
*
*/
static int gaussMatrix[] =
{
2, 4, 5, 4, 2,
4, 9, 12, 9, 4,
5, 12, 15, 12, 5,
4, 9, 12, 9, 4,
2, 4, 5, 4, 2
};
/**
*
*/
{
int firstX = 2;
int firstY = 2;
if (!newGm)
return NULL;
for (int y = 0 ; y<height ; y++)
{
for (int x = 0 ; x<width ; x++)
{
/* image boundaries */
{
continue;
}
/* all other pixels */
int gaussIndex = 0;
unsigned long sum = 0;
for (int i= y-2 ; i<=y+2 ; i++)
{
for (int j= x-2; j<=x+2 ; j++)
{
}
}
sum /= 159;
}
}
return newGm;
}
/**
*
*/
{
int firstX = 2;
int firstY = 2;
if (!newGm)
return NULL;
for (int y = 0 ; y<height ; y++)
{
for (int x = 0 ; x<width ; x++)
{
/* image boundaries */
{
continue;
}
/* all other pixels */
int gaussIndex = 0;
int sumR = 0;
int sumG = 0;
int sumB = 0;
for (int i= y-2 ; i<=y+2 ; i++)
{
for (int j= x-2; j<=x+2 ; j++)
{
}
}
}
}
return newGm;
}
/*#########################################################################
### C A N N Y E D G E D E T E C T I O N
#########################################################################*/
static int sobelX[] =
{
-1, 0, 1 ,
-2, 0, 2 ,
-1, 0, 1
};
static int sobelY[] =
{
1, 2, 1 ,
0, 0, 0 ,
-1, -2, -1
};
/**
* Perform Sobel convolution on a GrayMap
*/
double dLowThreshold, double dHighThreshold)
{
int firstX = 1;
int firstY = 1;
if (!newGm)
return NULL;
for (int y = 0 ; y<height ; y++)
{
for (int x = 0 ; x<width ; x++)
{
unsigned long sum = 0;
/* image boundaries */
{
sum = 0;
}
else
{
/* ### SOBEL FILTERING #### */
long sumX = 0;
long sumY = 0;
int sobelIndex = 0;
for (int i= y-1 ; i<=y+1 ; i++)
{
for (int j= x-1; j<=x+1 ; j++)
{
sobelX[sobelIndex++];
}
}
sobelIndex = 0;
for (int i= y-1 ; i<=y+1 ; i++)
{
for (int j= x-1; j<=x+1 ; j++)
{
sobelY[sobelIndex++];
}
}
/*### GET VALUE ### */
if (sum > 765)
sum = 765;
#if 0
/*### GET ORIENTATION (slow, pedantic way) ### */
double orient = 0.0;
if (sumX==0)
{
if (sumY==0)
orient = 0.0;
else if (sumY<0)
{
orient = 90.0;
}
else
orient = 90.0;
}
else
{
if (orient < 0.0)
orient += 180.0;
}
/*### GET EDGE DIRECTION ### */
int edgeDirection = 0;
if (orient < 22.5)
edgeDirection = 0;
else if (orient < 67.5)
edgeDirection = 45;
else if (orient < 112.5)
edgeDirection = 90;
else if (orient < 157.5)
edgeDirection = 135;
#else
/*### GET EDGE DIRECTION (fast way) ### */
int edgeDirection = 0; /*x,y=0*/
if (sumX==0)
{
if (sumY!=0)
edgeDirection = 90;
}
else
{
/*long slope = sumY*1024/sumX;*/
edgeDirection = 90;
edgeDirection = 45;
edgeDirection = 135;
}
#endif
/* printf("%ld %ld %f %d\n", sumX, sumY, orient, edgeDirection); */
/*### Get two adjacent pixels in edge direction ### */
unsigned long leftPixel;
unsigned long rightPixel;
if (edgeDirection == 0)
{
}
else if (edgeDirection == 45)
{
}
else if (edgeDirection == 90)
{
}
else /*135 */
{
}
/*### Compare current value to adjacent pixels ### */
/*### if less that either, suppress it ### */
sum = 0;
else
{
unsigned long highThreshold =
(unsigned long)(dHighThreshold * 765.0);
unsigned long lowThreshold =
(unsigned long)(dLowThreshold * 765.0);
if (sum >= highThreshold)
else if (sum < lowThreshold)
sum = 0; /* NONEDGE */
else
{
else
sum = 0; /* NONEDGE */
}
}
}/* else */
if (sum==0) /* invert light & dark */
sum = 765;
else
sum = 0;
}/* for (x) */
}/* for (y) */
return newGm;
}
/**
*
*/
GrayMap *
{
if (!gm)
return NULL;
if (!cannyGm)
return NULL;
/*cannyGm->writePPM(cannyGm, "canny.ppm");*/
return cannyGm;
}
/*#########################################################################
### Q U A N T I Z A T I O N
#########################################################################*/
/**
* Experimental. Work on this later
*/
{
//gaussMap->writePPM(gaussMap, "rgbgauss.ppm");
//qMap->writePPM(qMap, "rgbquant.ppm");
// RGB is quantized. There should now be a small set of (R+G+B)
{
{
if (sum & 1)
sum = 765;
else
sum = 0;
// printf("%d %d %d : %d\n", rgb.r, rgb.g, rgb.b, index);
}
}
return gm;
}
/*#########################################################################
### E N D O F F I L E
#########################################################################*/