Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- typedef struct {
- unsigned char red;
- unsigned char green;
- unsigned char blue;
- } pixel;
- typedef struct {
- unsigned short red;
- unsigned short green;
- unsigned short blue;
- } summed_pixel;
- typedef union {
- summed_pixel sum;
- unsigned long wrapper;
- unsigned char bytes[8];
- } long_summed_pixel;
- // adds a pixel with a given scale to a long_summed_pixel
- #define ADD_TO_SUM(summed, pix, scale) \
- summed.sum.red += pix.red * scale; \
- summed.sum.green += pix.green * scale; \
- summed.sum.blue += pix.blue * scale;
- void applyKernelBlur(int dim, pixel* curr, pixel* out) {
- long_summed_pixel val = {0};
- // line before
- ADD_TO_SUM(val, curr[-dim - 1], 1);
- ADD_TO_SUM(val, curr[-dim], 1);
- ADD_TO_SUM(val, curr[-dim + 1], 1);
- // current line
- ADD_TO_SUM(val, curr[-1], 1);
- ADD_TO_SUM(val, curr[0], 1);
- ADD_TO_SUM(val, curr[1], 1);
- // line after
- ADD_TO_SUM(val, curr[dim - 1], 1);
- ADD_TO_SUM(val, curr[dim], 1);
- ADD_TO_SUM(val, curr[dim + 1], 1);
- val.sum.red /= 9;
- val.sum.green /= 9;
- val.sum.blue /= 9;
- out->red = val.bytes[0];
- out->green = val.bytes[2];
- out->blue = val.bytes[4];
- }
- #define KERNEL_SIZE 3
- #define HALF_KERNEL (KERNEL_SIZE / 2)
- /*
- * Apply the kernel over each pixel.
- */
- void smooth(int dim, pixel *src, pixel *dst, char isBlur) {
- int rowoffset, coloffset;
- size_t numPixels = n * m;
- const int rowMin = HALF_KERNEL * dim;
- const int rowMax = dim * (dim - HALF_KERNEL);
- const int rowJump = dim;
- const int colMax = dim - HALF_KERNEL;
- if (isBlur) {
- // in case it's blur, do this
- // dst += rowMin;
- // src += rowMin;
- for (rowoffset = rowMin; rowoffset < rowMax; rowoffset += rowJump) {
- // dst += HALF_KERNEL;
- // src += HALF_KERNEL;
- for (coloffset = HALF_KERNEL; coloffset < colMax; coloffset++) {
- applyKernelBlur(dim, &src[rowoffset + coloffset], &dst[rowoffset + coloffset]);
- // src++;
- // dst++;
- }
- // src += rowJump;
- // dst += rowJump;
- }
- }
- else {
- // in case it's sharpen
- }
- }
- void charsToPixels(Image *charsImg, pixel* pixels) {
- int row, col;
- for (row = 0 ; row < m ; row++) {
- for (col = 0 ; col < n ; col++) {
- pixels[row*n + col].red = image->data[3*row*n + 3*col];
- pixels[row*n + col].green = image->data[3*row*n + 3*col + 1];
- pixels[row*n + col].blue = image->data[3*row*n + 3*col + 2];
- }
- }
- }
- void pixelsToChars(pixel* pixels, Image *charsImg) {
- int row, col;
- for (row = 0 ; row < m ; row++) {
- for (col = 0 ; col < n ; col++) {
- image->data[3*row*n + 3*col] = pixels[row*n + col].red;
- image->data[3*row*n + 3*col + 1] = pixels[row*n + col].green;
- image->data[3*row*n + 3*col + 2] = pixels[row*n + col].blue;
- }
- }
- }
- void doConvolution(Image *image, int isBlur) {
- pixel* pixelsImg = malloc(m*n*sizeof(pixel));
- pixel* dest = malloc(m*n*sizeof(pixel));
- charsToPixels(image, pixelsImg);
- printf("m: %d, n: %d\n", m ,n);
- smooth(m, pixelsImg, dest, 1);
- pixelsToChars(dest, image);
- free(pixelsImg);
- free(dest);
- }
- void myfunction(Image *image, char* srcImgpName, char* blurRsltImgName, char* sharpRsltImgName) {
- /*
- * [1, 1, 1]
- * [1, 1, 1]
- * [1, 1, 1]
- */
- int blurKernel[3][3] = {{1, 1, 1}, {1, 1, 1}, {1, 1, 1}};
- /*
- * [-1, -1, -1]
- * [-1, 9, -1]
- * [-1, -1, -1]
- */
- int sharpKernel[3][3] = {{-1,-1,-1},{-1,9,-1},{-1,-1,-1}};
- // blur image
- doConvolution(image, 1);
- // write result image to file
- writeBMP(image, srcImgpName, blurRsltImgName);
- //smooth(m, backupOrg, pixelsImg, kernelSize, sharpKernel, 1);
- // sharpen the resulting image
- doConvolution(image, 1);
- // write result image to file
- writeBMP(image, srcImgpName, sharpRsltImgName);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement