Pointer to different data types

Pages: 12
Write some helper functions...?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include <cstdint>

enum class image_type { mono_x8, r8g8b8, };

struct rgb_triple { std::uint8_t rgb[3]; };

namespace {
    constexpr std::size_t pixel_machine_size(image_type fmt) {
        #pragma GCC diagnostic error "-Wswitch-enum"
        #pragma GCC diagnostic push
        switch (fmt) {
            case image_type::mono_x8: return sizeof (std::uint8_t);
            case image_type::r8g8b8:  return sizeof (rgb_triple);
        }
        #pragma GCC diagnostic pop
    }
    
    constexpr std::uint8_t* next_pixel(std::uint8_t *image, image_type fmt) {
        return image + pixel_machine_size(fmt);
    }
}

void process_image(std::uint8_t *image, image_type fmt) {
    bool more_pixels = false;
    while (more_pixels) {
        // format_independent_operation(image, fmt);
        // format_dependent_operation(image, fmt);
        // format_independent_operation(image, fmt);
        // ... 
        image = next_pixel(image, fmt);
    }
}


This is what @jonnin is discussing.
Last edited on
Yes I think I see your drift. Thanks for going to all that effort.
I've tried to put it into the context of my own program.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
 void ProcessImage (RGBTRIPLE *pImage) // pass a pointer to a bitmap
{

   uint8_t *ptrPixel;
   int ImageSizeX = 1400; // default to color camera
   int ImageSizeY = 980;   // ditto
   int PixSize= 3;              // ditto

   if (ImageIsMono)          // change settings for grayscale camera
   {
       PixSize = 1; 
       ImageSizeX = 2040;
       ImageSizeY = 1080;
   }
   // Examine every pixel in the image
   for (int y=0; y<ImageSizeY; y++)
   {
      for (int x=0; x<ImageSizeX; x++)
      {
         ptrPixel = (uint8_t *) pImage;
         ptrPixel = ptrPixel + ((x + (y * ImageSizeX)) * PixSize);
          // pointer is now pointing at a pixel in the image.
          // If grayscale image then pointing at one byte grayscale data      
          // If color image then it is pointing at the first color
          // of the RGB triple. The other two color bytes will be right next door

          // do something with pixel (Image Processing)

        }
    }
} 


Last edited on
I should have known, that if it could be done, mbozzi would find a way to make my C mess nicer.

But yes, what he said is what I said, without the cave man stuff. And sorry if I was not getting the point across. I don't always make sense.
Last edited on
Jonnin, you were spot on.
mbozzi ditto.

It took me a while to figure out what both of you were suggesting.

jonnin, your example was too simple for me to follow.
mbozzi, your example was too complex for me to follow.

Mine is the Goldilocks solution - just right.

Jokes aside, you were both right on the money, and I thank you for the help.
The solution was staring me in the face all the time.
Last edited on
Topic archived. No new replies allowed.
Pages: 12