Delfino
Delfino

Reputation: 1009

Reproducing an Image using C

I'm trying to write a program to blur an image, but first of am trying to see if I can even reproduce the image, pixel by pixel, in another file. I've allocated a 2-dimensional char array to hold the value of each pixel in the image.

Note: The image is in grayscale, and it is of type .raw

However, whenever I attempt to read the pixels into my 2D array, my program crashes. I feel like it has something to do with me not looping through the dimensions of the image correctly, but I'm not sure.

Code:

#include <stdio.h>
#include <stdlib.h>

int main()
{
    FILE *fin, *fout;
    char path_in[64], path_out[64], **rev, px;
    int width, height, read, i, j;

    printf("Input file name: ");
    scanf("%s", path_in);
    printf("Output file name: ");
    scanf("%s", path_out);

    printf("Width of image (in pixels): ");
    scanf("%d", &width);
    printf("Height of image (in pixels): ");
    scanf("%d", &height);

    fin = fopen(path_in, "rb");
    fout = fopen(path_out, "wb");

    row = 0;
    rev = (char **)malloc(height * sizeof(char));
    for(i = 0; i < width; i++)
        rev[i] = (char *)malloc(width * sizeof(char));

    for(i = 0; i < height; i++)
    {
        for(j = 0; j < width; j++)
        {
            read = fread(&px, sizeof(char), 1, fin);
            rev[i][j] = px;
        }
    }

    fclose(fout);
    fclose(fin);

    return 0;
}

My program is pretty narrow, as it only accepts grayscale .raw image type.

Upvotes: 0

Views: 94

Answers (3)

Sir Jo Black
Sir Jo Black

Reputation: 2096

I think is better to use contiguous memory to manage the image!

Using char ** in the way as in your code you don't have a block of memory that contains the whole image, but a lot of not contiguous vectors each one containing a line!

The following code reads the image in a contiguous block of memory using only one malloc:

char * rev;

/* HERE your code to insert width and height */

rev = malloc(height * width *sizeof(char));
if (rev==NULL) {
    /* Ops! The buffer is not allocated!!! */
}

/* HERE you open the file and so on */

for(i = 0; i < height; i++)
{
    for(j = 0; j < width; j++)
    {
        read = fread(&rev[i*width+j], sizeof(char), 1, fin);
        if (read!=1) { /* 1 is the number of bytes fread reads if ok */
            /* Ops, there's a problem! The fread didn't read! */
        }
    }
}

Using char * you might load all the image with one fread, thus avoiding the two for loops :)

   fread(rev,witdh,height,fin);

In this way you may point each single pixel using:

rev[y*width+x];

Upvotes: 0

mcleod_ideafix
mcleod_ideafix

Reputation: 11438

Change these lines as this:

row = 0;
rev = malloc(height * sizeof *rev);
for(i = 0; i < width; i++)
    rev[i] = malloc(width * sizeof **rev);

You were allocating memory for height chars, not for height pointers to chars, as you intended.

Also, don't cast the result of malloc if developing with C

Upvotes: 3

Buddy
Buddy

Reputation: 11038

This line: rev = (char **)malloc(height * sizeof(char));

should be rev = (char **)malloc(height * sizeof(char*));

Upvotes: 2

Related Questions