1

I have a struct that basically has these contents:

typedef struct {
  unsigned int width, height;
  float *r, *g, *b;
} image;

Using the FreeImage.h library I am able to open a certain image and associate the pixels with its RGB colours. So the code is as follows:

imagem open_image(char *name_of_file) {
  FIBITMAP *bitmapIn;
  int x, y;
  RGBQUAD color;
  image I;

  bitmapIn = FreeImage_Load(FIF_JPEG, name_of_file, 0);

  if (bitmapIn == 0) {
    printf("Error! File not found - %s\n", name_of_file);
  } else {
    printf("File found!\n");
   }

  x = FreeImage_GetWidth(bitmapIn);
  y = FreeImage_GetHeight(bitmapIn);

  I.width = x;
  I.height = y;
  I.r = malloc(sizeof(float) * x * y);
  I.g = malloc(sizeof(float) * x * y);
  I.b = malloc(sizeof(float) * x * y);

   for (int i=0; i<x; i++) {
     for (int j=0; j <y; j++) {
      int idx;
      FreeImage_GetPixelColor(bitmapIn, i, j, &color);

      idx = i + (j*x);

      I.r[idx] = color.rgbRed;
      I.g[idx] = color.rgbGreen;
      I.b[idx] = color.rgbBlue;
    }
   }
  return I;
}

So now when I call the function in my main file like:

image img = open_image("file_name");

I have my image "map" in my reference named img.

Basically what I want to know is what is the best way to make a copy of this img so that I can apply a filter in this buffer img, for exemple a blur filter. That way when I get the surrounding pixels values to write to the central one, it is always the original pixels so get the pixels from "img" and write them with the filter to the "img_buffer".

I guess I could just make another reference named img_buff and call the open_image function again but that takes to much real and user time.

So how would you suggest I made the copy? From my research I found that memcpy() maybe won't do the trick since it gives me a shallow copy but not to sure about it.

Any suggestion that won't take to much more real/user time is welcome!

fdev
  • 87
  • 1
  • 7
  • 1
    You don't need to copy the image, you just need to allocate a buffer (or 3) that you can write to while filtering. Using `malloc` just like you did here. After filtering you can free the memory for the original image if you don't need that any more, and have that image point to the new buffers. – Cris Luengo May 30 '18 at 22:17

1 Answers1

2

If you want to improve performances,you should try to avoid copies which are time-consuming specially for large arrays.

But I assume you can't, so there is a solution :

Image imgCopy;
imgCopy.width  = img.width 
imgCopy.height  = img.height

In a first time you allocate memory :

imgCopy.r = (float*)malloc(sizeof(float) * imgCopy.width * imgCopy.height );
imgCopy.g = (float*)malloc(sizeof(float) * imgCopy.width * imgCopy.height );
imgCopy.b = (float*)malloc(sizeof(float) * imgCopy.width * imgCopy.height );

Then you can copy the pointer's content :

*(imgCopy.r) = *(img.r);
*(imgCopy.g) = *(img.r);
*(imgCopy.b) = *(img.r);
Cryckx
  • 659
  • 6
  • 18
  • Actually it doesn't copy the contents of the pixels. But just creating the buffer and doing a sweep pixel by pixel copying it gives me what I want – fdev May 31 '18 at 23:20