LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-10-2008, 12:44 AM   #1
rvca
LQ Newbie
 
Registered: Dec 2005
Location: Vancouver
Distribution: Debian/Ubuntu
Posts: 29

Rep: Reputation: 15
How to access a specific byte in a void * ?


Hi everybody,

I am writting a C code using the v4l2 API that will track different colored leds. so far I have managed to get my camera workng and reading a frame. However the data is put in a pointer:

void * buffer

However my image is a planar YUV420, because i am tracking color of each pixel i have to get the color value of each pixel by "reconstructing the images" the fact is that i want to access a certain byte of the pointer (lets say the byte 36 of such buffer and the byte 18 to do the appropiate calculations) How do I achieve that? Can I access the memory as if being an array of characters? Something like:

char * imagearray = buffer;

and then access it by:

imagearray[36]

Is that possible? or how can I do this?

Thanx for your help.
 
Old 03-10-2008, 02:51 AM   #2
Guttorm
Senior Member
 
Registered: Dec 2003
Location: Trondheim, Norway
Distribution: Debian and Ubuntu
Posts: 1,453

Rep: Reputation: 447Reputation: 447Reputation: 447Reputation: 447Reputation: 447
Hi

You can do it yes. But when changing a pointer type like that, you will get a warning from the compiler unless you use a type cast:

char * imagearray = (char*) buffer;
 
Old 03-10-2008, 07:33 AM   #3
dmail
Member
 
Registered: Oct 2005
Posts: 970

Rep: Reputation: Disabled
Quote:
Originally Posted by Guttorm View Post
Hi

You can do it yes. But when changing a pointer type like that, you will get a warning from the compiler unless you use a type cast:

char * imagearray = (char*) buffer;
This is not correct as the OP is using C and a cast from void pointer to another type pointer is implicit.
 
Old 03-10-2008, 07:45 AM   #4
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
Quote:
Originally Posted by dmail View Post
This is not correct as the OP is using C and a cast from void pointer to another type pointer is implicit.
Actually it's the other way around. Any pointer will implicitly cast to void* but you must explicitly cast void* to turn it into something else. That's because void* is useless and you shouldn't be able to implicitly make it into an arbitrary useful type. Polymorphism doesn't work with void*, either, which is another reason. Take for example
Code:
class A {};
 
class B : virtual public A {};
A B* cast to a void* should only be cast back to B* or to unsigned char*, even if just to further cast to A*. You'll end up with an invalid pointer if you cast the void* directly to A*, hence the requirement for explicit casting.

Technically you should be using unsigned char* because that's the standard binary representation of data. char* may be signed, and as such could have anomalous effects when performing binary operations.
ta0kira

Edit: I think you might be right in a normal C context as far as implicit casting. I guess I'm thinking in terms of C++.

Last edited by ta0kira; 03-10-2008 at 07:47 AM.
 
Old 03-10-2008, 07:48 AM   #5
dmail
Member
 
Registered: Oct 2005
Posts: 970

Rep: Reputation: Disabled
Quote:
Originally Posted by ta0kira View Post
Actually it's the other way around. Any pointer will implicitly cast to void* but you must explicitly cast void* to turn it into something else. That's because void* is useless and you shouldn't be able to implicitly make it into an arbitrary useful type. Polymorphism doesn't work with void*, either, which is another reason. Take for example[code]class A {};

class B : virtual public A {};[code]A B* cast to a void* should only be cast back to B* or to unsigned char*, even if just to further cast to A*. You'll end up with an invalid pointer if you cast the void* directly to A*, hence the requirement for explicit casting.
Quote:
Originally Posted by rvca
I am writting a C code ...
Quote:
Originally Posted by dmail
This is not correct as the OP is using C...
Why are you talking about classes?

Edit:
Lol no problem I see your edit now.

And just for confirmation
Quote:
6.2.5 Types...
Pointers to other types coerce silently to and from void* in assignments, function prototypes, comparisons, and conditional expressions, whereas other pointer type clashes are invalid. It is undefined what will happen if a pointer of some type is converted to void*, and then the void* pointer is converted to a type with a stricter alignment requirement.

Last edited by dmail; 03-10-2008 at 07:57 AM.
 
Old 03-10-2008, 01:49 PM   #6
rvca
LQ Newbie
 
Registered: Dec 2005
Location: Vancouver
Distribution: Debian/Ubuntu
Posts: 29

Original Poster
Rep: Reputation: 15
Hi,

Thank you, it compiles without warning. Now how do I access a specific byte, say the 5th byte? I assume that the following is correct?

Code:
void * buffer; //already containing the image data

char * imagearray = buffer;

int pixel_y = atoi(imagearray[5]); // number just as example
printf ("Y Value of 5 pixel is: %d",pixel_y);
However I get a Segmentation Fault when running the app. What am I missing or doing wrong?

Thank you,
 
Old 03-10-2008, 01:57 PM   #7
rvca
LQ Newbie
 
Registered: Dec 2005
Location: Vancouver
Distribution: Debian/Ubuntu
Posts: 29

Original Poster
Rep: Reputation: 15
Hi,

I think I am wrong in using atoi(), because i assume that char just gives me a collection of bytes, so... (I am assuming all this) The next code should give me a correct value?

Code:
char * imagearray = (char*) buffer;
unsigned int pixel_y = imagearray[5]; //5 as example
unsigned int pixel_cr = imagearray[6]; //6 as example
printf ("Pixel 5 has final value of %d\n",pixel_y*pixel_cr); //multiplication just as aritmetic example
Am I right?

Thanx,
 
Old 03-10-2008, 06:56 PM   #8
osor
HCL Maintainer
 
Registered: Jan 2006
Distribution: (H)LFS, Gentoo
Posts: 2,450

Rep: Reputation: 78
Quote:
Originally Posted by rvca View Post
Am I right?
You are correct in your approach, just remember that array indexing starts at 0.
 
Old 03-11-2008, 12:27 AM   #9
rvca
LQ Newbie
 
Registered: Dec 2005
Location: Vancouver
Distribution: Debian/Ubuntu
Posts: 29

Original Poster
Rep: Reputation: 15
Thanks for your help, got it working.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Need to copy an entire disk byte-for-byte Pawprint Linux - Software 6 06-16-2011 11:01 AM
byte to byte remote comparison louisJ Linux - Newbie 3 09-21-2007 05:28 PM
Access directory from specific IP mikeshn Linux - General 1 12-19-2003 01:57 PM
backup byte-for-byte axion0917 Linux - Software 2 12-11-2003 05:01 PM
void main(void) linuxanswer Programming 4 10-26-2003 12:37 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:44 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration