void SharpenFilter::sharpenImage(double radius, double sigma) { if (m_orgImage.isNull()) { qCWarning(DIGIKAM_DIMG_LOG) << "No image data available!"; return; } if (radius <= 0.0) { m_destImage = m_orgImage; return; } double alpha, normalize = 0.0; long i = 0, u, v; int kernelWidth = getOptimalKernelWidth(radius, sigma); int halfKernelWidth = kernelWidth / 2; if ((int)m_orgImage.width() < kernelWidth) { qCWarning(DIGIKAM_DIMG_LOG) << "Image is smaller than radius!"; return; } QScopedArrayPointer<double> kernel(new double[kernelWidth * kernelWidth]); if (kernel.isNull()) { qCWarning(DIGIKAM_DIMG_LOG) << "Unable to allocate memory!"; return; } for (v = -halfKernelWidth; v <= halfKernelWidth; ++v) { for (u = -halfKernelWidth; u <= halfKernelWidth; ++u) { alpha = exp(-((double) u * u + v * v) / (2.0 * sigma * sigma)); kernel[i] = alpha / (2.0 * M_PI * sigma * sigma); normalize += kernel[i]; ++i; } } kernel[i / 2] = (-2.0) * normalize; convolveImage(kernelWidth, kernel.data()); }
void RefocusFilter::refocusImage(uchar* const data, int width, int height, bool sixteenBit, int matrixSize, double radius, double gauss, double correlation, double noise) { CMat* matrix = 0; // Compute matrix qCDebug(DIGIKAM_DIMG_LOG) << "RefocusFilter::Compute matrix..."; CMat circle, gaussian, convolution; RefocusMatrix::make_gaussian_convolution(gauss, &gaussian, matrixSize); RefocusMatrix::make_circle_convolution(radius, &circle, matrixSize); RefocusMatrix::init_c_mat(&convolution, matrixSize); RefocusMatrix::convolve_star_mat(&convolution, &gaussian, &circle); matrix = RefocusMatrix::compute_g_matrix(&convolution, matrixSize, correlation, noise, 0.0, true); RefocusMatrix::finish_c_mat(&convolution); RefocusMatrix::finish_c_mat(&gaussian); RefocusMatrix::finish_c_mat(&circle); // Apply deconvolution kernel to image. qCDebug(DIGIKAM_DIMG_LOG) << "RefocusFilter::Apply Matrix to image..."; Args prm; prm.orgData = data; prm.destData = d->preImage.bits(); prm.width = width; prm.height = height; prm.sixteenBit = sixteenBit; prm.matrix = matrix->data; prm.mat_size = 2 * matrixSize + 1; convolveImage(prm); // Clean up memory delete matrix; }
void CharcoalFilter::filterImage() { if (m_orgImage.isNull()) { qCWarning(DIGIKAM_DIMG_LOG) << "No image data available!"; return; } if (d->pencil <= 0.0) { m_destImage = m_orgImage; return; } // -- Applying Edge effect ----------------------------------------------- register long i = 0; int kernelWidth = getOptimalKernelWidth(d->pencil, d->smooth); if ((int)m_orgImage.width() < kernelWidth) { qCWarning(DIGIKAM_DIMG_LOG) << "Image is smaller than radius!"; return; } QScopedArrayPointer<double> kernel(new double[kernelWidth * kernelWidth]); if (kernel.isNull()) { qCWarning(DIGIKAM_DIMG_LOG) << "Unable to allocate memory!"; return; } for (i = 0 ; i < (kernelWidth * kernelWidth) ; ++i) { kernel[i] = (-1.0); } kernel[i / 2] = kernelWidth * kernelWidth - 1.0; convolveImage(kernelWidth, kernel.data()); // -- Applying Gaussian blur effect --------------------------------------- BlurFilter(this, m_destImage, m_destImage, 80, 85, (int)(d->smooth / 10.0)); if (!runningFlag()) { return; } // -- Applying stretch contrast color effect ------------------------------- StretchFilter stretch(&m_destImage, &m_destImage); stretch.startFilterDirectly(); m_destImage.putImageData(stretch.getTargetImage().bits()); postProgress(90); if (!runningFlag()) { return; } // -- Inverting image color ----------------------------------------------- InvertFilter invert(&m_destImage); invert.startFilterDirectly(); m_destImage.putImageData(invert.getTargetImage().bits()); postProgress(95); if (!runningFlag()) { return; } // -- Convert to neutral black & white ------------------------------------ MixerContainer settings; settings.bMonochrome = true; settings.blackRedGain = 0.3; settings.blackGreenGain = 0.59; settings.blackBlueGain = 0.11; MixerFilter mixer(&m_destImage, 0L, settings); mixer.startFilterDirectly(); m_destImage.putImageData(mixer.getTargetImage().bits()); postProgress(100); if (!runningFlag()) { return; } }
int main (int argc, const char* const argv[]) { char garbage[2]; int command; double sigma; if (4 > argc || 5 < argc) { fprintf (stderr, "syntax: %s <input file> <output file> <command #> ...\n", argv[0]); return 2; } if (1 != sscanf (argv[3], "%d%1s", &command, garbage) || 1 > command || 3 < command) { fprintf (stderr, "Command must be from 1 to 3.\n"); fprintf (stderr, " 1 -- gaussian filter <sigma>\n"); fprintf (stderr, " 2 -- greyscale\n"); fprintf (stderr, " 3 -- invert colors\n"); return 2; } if (1 == command && (5 != argc || 1 != sscanf (argv[4], "%lf%1s", &sigma, garbage) || 0 >= sigma || 100 <= sigma)) { fprintf (stderr, "Sigma must be greater than 0 and less than 100.\n"); return 2; } Image* inputImage = decode (argv[1]); printf ("Width: %d, height: %d\n", inputImage->width, inputImage->height); Image* outputImage = generateOutput (inputImage); int height = inputImage->height; int width = inputImage->width; uint8_t* inRed = inputImage->redChannel; uint8_t* inBlue = inputImage->blueChannel; uint8_t* inGreen = inputImage->greenChannel; uint8_t* inAlpha = inputImage->alphaChannel; uint8_t* outRed = outputImage->redChannel; uint8_t* outBlue = outputImage->blueChannel; uint8_t* outGreen = outputImage->greenChannel; uint8_t* outAlpha = outputImage->alphaChannel; switch (command) { case 1: { int radius = ceil (3 * sigma); int fSize = 1 + 2 * radius; double* gauss = malloc (sizeof (gauss[0]) * fSize * fSize); gaussianFilter (gauss, sigma); convolveImage (width, height, inRed, inGreen, inBlue, inAlpha, radius, gauss, outRed, outGreen, outBlue, outAlpha); encode (argv[2], outputImage); break; } case 2: greyscale (width, height, inRed, inGreen, inBlue, inAlpha, gMonoMult, outRed, outGreen, outBlue, outAlpha); encode (argv[2], outputImage); break; case 3: invertColors (width, height, inRed, inGreen, inBlue, inAlpha, outRed, outGreen, outBlue, outAlpha); encode (argv[2], outputImage); break; } freeImage (inputImage); freeImage (outputImage); return 0; }
int main(int argc,char *argv[]) { //Handles user input if(argc<4 || argc>5) { printf("Incorrect number of arguments\n"); printf("Number of arguments: %d\n",argc); exit(1); } //const char *inputFilename=argv[1]; const char *inputFilename=argv[1]; printf("Inputfile: %s\n",inputFilename); const char *outputFilename=argv[2]; char garbage[2]; int command; double sigma=3; if(1!=sscanf(argv[3],"%d%1s",&command,garbage) || command<0 || command>11) { printf("Incorrect command\n"); exit(1); } if(command>0 && command<11 && argc==5) { printf("Incorrect number of arguments, exclude the sigma value"); exit(1); } if(((command==0 || command==11) && argc==5 && 1!=sscanf(argv[4],"%lf%1s",&sigma,garbage)) || sigma<0) { printf("Incorrect sigma value\n"); exit(1); } Filter *filters=initializeFilters(sigma); Image *inputImage=decode(inputFilename); printf("Width: %d, height: %d\n",inputImage->width,inputImage->height); Image *outputImage=generateOutput(inputImage); uint8_t *inRed=inputImage->redChannel; uint8_t *inBlue=inputImage->blueChannel; uint8_t *inGreen=inputImage->greenChannel; uint8_t *inAlpha=inputImage->alphaChannel; uint8_t *outRed=outputImage->redChannel; uint8_t *outBlue=outputImage->blueChannel; uint8_t *outGreen=outputImage->greenChannel; uint8_t *outAlpha=outputImage->alphaChannel; int height=inputImage->height; int width=inputImage->width; switch(command) { case(0): { convolveImage(inRed,inBlue,inGreen,inAlpha,outRed,outBlue,outGreen, outAlpha,filters[0].filter,filters[0].radius,width,height); break; } case(1): { convolveImage(inRed,inBlue,inGreen,inAlpha,outRed,outBlue,outGreen, outAlpha,filters[1].filter,filters[1].radius,width,height); break; } case(2): { convolveImage(inRed,inBlue,inGreen,inAlpha,outRed,outBlue,outGreen, outAlpha,filters[2].filter,filters[2].radius,width,height); break; } case(3): { convolveImage(inRed,inBlue,inGreen,inAlpha,outRed,outBlue,outGreen, outAlpha,filters[3].filter,filters[3].radius,width,height); break; } case(4): { convolveImage(inRed,inBlue,inGreen,inAlpha,outRed,outBlue,outGreen, outAlpha,filters[4].filter,filters[4].radius,width,height); break; } case(5): { convolveImage(inRed,inBlue,inGreen,inAlpha,outRed,outBlue,outGreen, outAlpha,filters[5].filter,filters[5].radius,width,height); break; } case(6): { convolveImage(inRed,inBlue,inGreen,inAlpha,outRed,outBlue,outGreen, outAlpha,filters[6].filter,filters[6].radius,width,height); break; } case(7): { convertToGray(inRed,inBlue,inGreen,inAlpha,outRed,outBlue,outGreen, outAlpha,gMonoMult,width,height); break; } case(8): { invertImage(inRed,inBlue,inGreen,inAlpha,outRed,outBlue,outGreen, outAlpha,width,height); break; } case(9): { flipImage(inRed,inBlue,inGreen,inAlpha,outRed,outBlue,outGreen, outAlpha,width,height); break; } case(10): { pixelate(inRed,inBlue,inGreen,inAlpha,outRed,outBlue,outGreen, outAlpha,8,8,width,height); break; } case(11): { Image *invImage=generateOutput(inputImage); Image *blurImage=generateOutput(inputImage); pencilSketch(inRed,inBlue,inGreen,inAlpha,invImage->redChannel, invImage->blueChannel,invImage->greenChannel, invImage->alphaChannel,blurImage->redChannel, blurImage->blueChannel,blurImage->greenChannel, blurImage->alphaChannel,outRed,outBlue,outGreen, outAlpha,filters[0].filter,filters[0].radius,width,height, gMonoMult); //NOTE THAT I NEED TO FREE EACH OF THE CHANNEL INDIVIDUALLY //MAKE A FREE IMAGE FUNCTION freeImage(invImage); freeImage(blurImage); break; } default: exit(1); } if(command!=12) encode(outputFilename,outputImage); free((double*)filters[0].filter); free(filters); freeImage(inputImage); freeImage(outputImage); return 0; }