www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - How do I read data with ByChunk?

reply Craig Dillabaugh <cdillaba nrcan.gc.ca> writes:
Hi,
I have two binary files containing image data, and I want to go through them
pixel by
pixel and read the image data into arrays and compare the pixel values (images
have the
exact same dimensions but one is unsigned 1 byte per pixel, and the other
signed 2 bytes
per pixel).

I am trying to read the data in using the following loop:

	for(int i = 0; i < num_blocks; i++) {
		auto resampbytes = resampFile.byChunk( resamp_buffer_size );
		auto normbytes = normalFile.byChunk( normal_buffer_size );
		ubyte[] resamp = cast(ubyte[]) resampbytes;
		short[] normaldata = cast(short[]) normbytes;

                //Process the data ...
	}

However, when I attempt to compile this I get the following errors:

Error: cannot cast from ByChunk to ubyte[]
Error: cannot cast from ByChunk to short[]

Oddly, the example for ByChunk in the documentation seems to do exactly what I
think I
am trying here, but apparently I am missing something.

Any help would be appreciated (also if there is a better way of doing what I am
trying
to do any pointers on that would be appreciated too!)

Regards,

Craig
Mar 18 2011
parent reply Zirneklis <avoi dingspam.cc> writes:
On 18/03/2011 14:35, Craig Dillabaugh wrote:
 Hi,
 I have two binary files containing image data, and I want to go through them
pixel by
 pixel and read the image data into arrays and compare the pixel values (images
have the
 exact same dimensions but one is unsigned 1 byte per pixel, and the other
signed 2 bytes
 per pixel).

 I am trying to read the data in using the following loop:

 	for(int i = 0; i<  num_blocks; i++) {
 		auto resampbytes = resampFile.byChunk( resamp_buffer_size );
 		auto normbytes = normalFile.byChunk( normal_buffer_size );
 		ubyte[] resamp = cast(ubyte[]) resampbytes;
 		short[] normaldata = cast(short[]) normbytes;

                  //Process the data ...
 	}

 However, when I attempt to compile this I get the following errors:

 Error: cannot cast from ByChunk to ubyte[]
 Error: cannot cast from ByChunk to short[]

 Oddly, the example for ByChunk in the documentation seems to do exactly what I
think I
 am trying here, but apparently I am missing something.

 Any help would be appreciated (also if there is a better way of doing what I
am trying
 to do any pointers on that would be appreciated too!)

 Regards,

 Craig

File.byChunk is designed to be used with a foreach loop, you could try: ubyte[] resamp = new ubyte[resamp_buffer_size]; short[] normaldata = new short[normal_buffer_size]; for(int i = 0; i < num_blocks; i++) { resampFile.rawRead(resamp); normaldata.rawRead(normaldata); //Process the data ... } -- Aku MoD.
Mar 18 2011
parent Craig Dillabaugh <cdillaba nrcan.gc.ca> writes:
== Quote from Zirneklis (avoi dingspam.cc)'s article
 On 18/03/2011 14:35, Craig Dillabaugh wrote:
 Hi,
 I have two binary files containing image data, and I want to go


 pixel and read the image data into arrays and compare the pixel


 exact same dimensions but one is unsigned 1 byte per pixel, and


 per pixel).

 I am trying to read the data in using the following loop:

 	for(int i = 0; i<  num_blocks; i++) {
 		auto resampbytes = resampFile.byChunk


 		auto normbytes = normalFile.byChunk


 		ubyte[] resamp = cast(ubyte[]) resampbytes;
 		short[] normaldata = cast(short[]) normbytes;

                  //Process the data ...
 	}

 However, when I attempt to compile this I get the following


 Error: cannot cast from ByChunk to ubyte[]
 Error: cannot cast from ByChunk to short[]

 Oddly, the example for ByChunk in the documentation seems to do


 am trying here, but apparently I am missing something.

 Any help would be appreciated (also if there is a better way of


 to do any pointers on that would be appreciated too!)

 Regards,

 Craig


 ubyte[] resamp = new ubyte[resamp_buffer_size];
 short[] normaldata = new short[normal_buffer_size];
 for(int i = 0; i < num_blocks; i++)
 {
      resampFile.rawRead(resamp);
      normaldata.rawRead(normaldata);
      //Process the data ...
 }

Thanks, that did the trick! Craig
Mar 18 2011