Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c > #13736

Re: Changing type at runtime for image acquisition

From Kaz Kylheku <kaz@kylheku.com>
Newsgroups comp.lang.c
Subject Re: Changing type at runtime for image acquisition
Date 2011-11-21 20:48 +0000
Organization A noiseless patient Spider
Message-ID <20111121122635.360@kylheku.com> (permalink)
References <KMxyq.101324$GZ3.69880@tornado.fastwebnet.it>

Show all headers | View raw


On 2011-11-21, Davide <idontwant@emailsfrom.spammers> wrote:
> Dear comp.lang.c,
> I want to allocate a buffer to store 8bit or 16bit integers,
> depending on the color bitdepth of the images I want to capture.
> I allocate this buffer in a thread, so I have something like:

You're trading abstraction for storage density. How about defining a higher
abstractio of an image as consisting of pixels in some color space?  R G B
triplets, or whatever.

You could make a polymorphic type with abstract accessors so that you can
achieve the storage density also.  An 8 bit grayscale image could have
its pixels stored as byte values ranging from 0 to 255; yet be accessible
thorugh an interface where these pictures look like RGB triplets.

You have to think through issues like: if images are mutable object, what
happens if the program tries to put a red pixel into grayscale image?

> #define UTYPE __int8
> .
> .
>
> UTYPE *internal_buffer;
> .

Here is a general gist of an idea hinting at a possible structure for a
solution. Keep in mind that I'm painting in watercolor with a broad brush:

It might be helpful to look at how some open source software handles images.

#include <stdlib.h> /* for malloc */


typedef struct rgba {
  int r, g, b, a;
} rgba_t;

struct image_operations {
  /* ... */
  rgba_t (*get_pixel_at)(struct image *, int x, int y);
  /* ... */
}

typedef enum image_type { IMG_GS8 /* ... */ } imgtype_t;

struct abstract_image {
  struct image_operations *ops;
  int width, height;
  imgtype_t type;
};

struct gs8_image { /* grayscale 8 bit */
  /* common members from abstract image */
  struct image_operations *ops; 
  int width, height;
  enum image_type type;
  /* specific members */
  array_of_bytes_t *data;
}

/* ... */

union image {
  struct abtract image i;
  struct gs8_image image gs8;
  /* ... */
};

static rgba_t gs8_get_pixel_at(struct image *img, int x, int y)
{
  /* check that img->i.type == IMG_GS8 */
  int gs8_pix = array_of_bytes(img->gs8.data, y * img->i.width + x);
  rgba_t ret = { gs8_pix, gs8_pix, gs8_pix, 255 }; /* a == 255 -> opaque */
  return ret;
}

static struct image_operations gs8_ops {
  /* ... */
  gs8_get_pixel_at
  /* ... */
}

static struct image *make_image(int width, int height, imgtype_t type,
                                struct image_operations *ops)
{
  struct image *img = (struct image *) malloc(sizeof *img);
  if (img) {
    img->ops = ops;
    img->width = width;
    img->height = height;
    img->type = type;
  }
  return img;
}

rgba_t *image_get_pixel_at(struct image *image, int x, int y)
{
  return image->ops->get_pixel_at(image, x, y);
}

struct image *make_gs8_image(int width, int height, int fill, int fill_value)
{
  struct image *img = make_image(width, height, IMG_GS8, &gs8_ops);
  array_of_bytes_t *a = make_array_of_bytes(width * height);

  if (img && a) {
    img->data = a;
    if (fill) 
      fill_array_of_bytes(a, fill_value);

    return img;
  }

  free_image(img);
  free_array_of_bytes(a);
  return 0;
}

Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

Changing type at runtime for image acquisition Davide <idontwant@emailsfrom.spammers> - 2011-11-21 20:38 +0100
  Re: Changing type at runtime for image acquisition James Kuyper <jameskuyper@verizon.net> - 2011-11-21 15:03 -0500
    Re: Changing type at runtime for image acquisition Davide <idontwant@emailsfrom.spammers> - 2011-11-21 21:32 +0100
      Re: Changing type at runtime for image acquisition James Kuyper <jameskuyper@verizon.net> - 2011-11-21 15:42 -0500
      Re: Changing type at runtime for image acquisition James Kuyper <jameskuyper@verizon.net> - 2011-11-21 16:04 -0500
  Re: Changing type at runtime for image acquisition Kaz Kylheku <kaz@kylheku.com> - 2011-11-21 20:48 +0000
  Re: Changing type at runtime for image acquisition "BartC" <bc@freeuk.com> - 2011-11-21 21:34 +0000

csiph-web