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


Groups > comp.lang.c > #13732

Re: Changing type at runtime for image acquisition

From James Kuyper <jameskuyper@verizon.net>
Newsgroups comp.lang.c
Subject Re: Changing type at runtime for image acquisition
Date 2011-11-21 15:03 -0500
Organization A noiseless patient Spider
Message-ID <4ECAAE78.7050304@verizon.net> (permalink)
References <KMxyq.101324$GZ3.69880@tornado.fastwebnet.it>

Show all headers | View raw


On 11/21/2011 02:38 PM, Davide 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:
> 
> #define UTYPE __int8
> .
> .
> 
> UTYPE *internal_buffer;
> .
> .
> 
> unsigned long __stdcall ThreadFunc(void *pContext)
> {
>    if (!(internal_buffer = malloc(number_of_frames * 
> frame_size_in_bytes))) {
>      fprintf(stderr, "Failed to allocate internal memory buffer!\n");
>      exit(EXIT_FAILURE);
>    }
>    return 0;
> }
> 
> Now if I want to switch to a buffer of __int16, is there a way to get it
> using the same function? Is there a way to change the type of the pointer
> internal_buffer to __int16* at runtime? I would need that so i have a:
> 
> __int8 *internal_buffer;
> 
> when i need 8bit color frames, and a:
> 
> __int16 *internal_buffer;
> 
> for 14bit (i.e. 16bit) frames, but I want to use the same variable
> internal_buffer to manipulate my frames. Sorry if I use __int8, __int16,
> __stdcall, but I'm writing this program under win32 and these are microsoft
> reserved keywords.

You could use void* to store the pointer to the buffer, and convert to
__int8 or __int16 as needed:

    if(eight_bit)
        n = ((__int8*)internal_buffer)[j];
    else
        n = ((__int16*)internal_buffer)[j];

In some ways, that would be the cleanest approach. However, if that
would require you to doing too many explicit conversions, then it might
be easier to use a union:

    union {
	__int8 *pi8;
	__int16 *pi16;
    } internal_buffer;

    temp = malloc(....;
    if(temp)
    {
        if(eight_bit)
            internal_buffer.pi8 = temp;
        else
            internal_buffer.pi16 = temp;
    }

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