473,379 Members | 1,302 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,379 software developers and data experts.

Dynamically assigning data type

Hi guys,
I'm trying to write a program that will read in a series of files and
create a 3D array from the files read in for converting 2D images to 3D
objects. The values read in will be considered as pixel colours and the
size that 1 pixel occupies will depend on an input from the user when
running the program. How can I dynamically change the data type of my 3D
array at runtime. eg. 3D array was defined as type unsigned char in the
code but the file to be read requires the array to be type unsigned short.
I've tried using

typedef unsigned char pixeltype; // assuming data is unsigned char

in my header file and then writing all my functions using the type
pixeltype.
I've tried casting the pointer to the array to a variable of type
unsigned short but it didn't really work. Googling also didn't produce
anything fruitful. Thanks and best regards.

Yogi
Nov 14 '05 #1
3 1827

"yogi" <le******@tartarus.uwa.edu.au> wrote

How can I dynamically change the data type of my 3D
array at runtime.

unsigned char *image; /* your raw data */

int pixelsize; /* size of each pixel, in bytes */
int redoffset; /* offset of the start of the red channel from pixel
beginning */
int redbits; /* number of bits used for the red channel */

For extra brownie points, you can have a big / little endian flag.
Nov 14 '05 #2
yogi <le******@tartarus.uwa.edu.au> wrote:
I'm trying to write a program that will read in a series of files and
create a 3D array from the files read in for converting 2D images to 3D
objects. The values read in will be considered as pixel colours and the
size that 1 pixel occupies will depend on an input from the user when
running the program. How can I dynamically change the data type of my 3D
array at runtime. eg. 3D array was defined as type unsigned char in the
code but the file to be read requires the array to be type unsigned short.


That's not really possible. If you've only allocated memory for a
certain number of chars, however are you going to fit the same number of
shorts in there? It would mean that sizeof(short) <= sizeof(char), and
that is only very rarely true on desktop computers. (And where it is, of
course, sizeof(short) == sizeof(char) == 1, and CHAR_BIT >= 16.)

If you know the maximum size of your elements, going the other way
(allocating memory for an array of the largest possible data type, and
then possibly filling it with a smaller type) is possible, but may cost
a lot of memory.

Richard
Nov 14 '05 #3
Yeah... I guess I might have to do it that way by using unsigned short
in my code and then cast it to chars as I need to. Thanks for the help.
Cheers!

Richard Bos wrote:
yogi <le******@tartarus.uwa.edu.au> wrote:

I'm trying to write a program that will read in a series of files and
create a 3D array from the files read in for converting 2D images to 3D
objects. The values read in will be considered as pixel colours and the
size that 1 pixel occupies will depend on an input from the user when
running the program. How can I dynamically change the data type of my 3D
array at runtime. eg. 3D array was defined as type unsigned char in the
code but the file to be read requires the array to be type unsigned short.

That's not really possible. If you've only allocated memory for a
certain number of chars, however are you going to fit the same number of
shorts in there? It would mean that sizeof(short) <= sizeof(char), and
that is only very rarely true on desktop computers. (And where it is, of
course, sizeof(short) == sizeof(char) == 1, and CHAR_BIT >= 16.)

If you know the maximum size of your elements, going the other way
(allocating memory for an array of the largest possible data type, and
then possibly filling it with a smaller type) is possible, but may cost
a lot of memory.

Richard

Nov 14 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Eric | last post by:
How can I dynamically assign an event to an element? I have tried : (myelement is a text input) document.getElementById('myelement').onKeyUp = "myfnc(param1,param2,param3)"; ...
16
by: sirsean | last post by:
Hi all. I'm trying to dynamically build menus and objects after my page loads. Data is stored in an XML file and is parsed at runtime into Javascript objects. At the moment, I'm working on creating...
8
by: Kevin Little | last post by:
#!/usr/bin/env python ''' I want to dynamically add or replace bound methods in a class. I want the modifications to be immediately effective across all instances, whether created before or...
7
by: cjl | last post by:
Hey all: I've searched the newsgroup, and googled, but I'm stuck. I want to be able to 'dynamically' add a .js file to a web page after the page has loaded, based on user interaction. For...
4
by: RobG | last post by:
I have a function whose parameter is a reference the element that called it: function someFunction(el) { ... } The function is assigned to the onclick event of some elements in the HTML...
1
by: Marcus | last post by:
I have a problem maybe one of you could help me with. I've created a data entry screen with lots of dynamically-created client-side controls. I create HTML texboxes client-side by assigning a...
7
by: pmclinn | last post by:
I was wondering if it is possible to dynamically create a structure. Something like this: public sub main sql = "Select Col1, Col2 from Table a" dim al as new arraylist al =...
11
by: skumar434 | last post by:
Hi everybody, I am faceing problem while assigning the memory dynamically to a array of structures . Suppose I have a structure typedef struct hom_id{ int32_t nod_de; int32_t hom_id;
0
by: lucindaa | last post by:
Hi EveryBody, i have a problem in setting the column type of datagrid dynamically in C# .Net Windows App. i am generating the grid automatically by assigning datasource of a datatable the...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.