?
Solved

Read a raster

Posted on 1998-10-30
12
Medium Priority
?
476 Views
Last Modified: 2013-11-20
I have a function named TIFFReadRGBAImage that reads a TIFF image into memory, storing the result in a supplied raster.
Raster pixels are 8-bit packed red,green,blue alpha samples.

What must i do now to acces individual samples??

(The raster is assumed tobe organizated such thet the pixel at localitation (x,y) is raster [y*image.width+x] with the raster origin in the lower-left hand cornar)
0
Comment
Question by:Sol
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 6
12 Comments
 
LVL 12

Accepted Solution

by:
rwilson032697 earned 320 total points
ID: 1324065
Each sample occupies 32 bits or 4 bytes. So you can cast the memory containing the raster image to an array of structures with each structure containing 4 fields, each of one byte in size called red, green, blue and alpha.

When you access this pizel array in the manner you describe you then get a structure with the individual samples all nicely separated out for you.

eg (excuse the rusty syntax - you get the picture (sic))

 struct APixel { tByte red, green, blue, alpha }; // tByte is whatever you use for a byte

typedef PixelArray APixel[];

Then ((PixelArray)RasterMemoryBlock)[y*image.width+x].red is the red sample for the pixel at [x, y]

Cheers,

Raymond.
0
 

Author Comment

by:Sol
ID: 1324066
Hi, Cheers!!

I’m a begginer in Visual C++, i suppose you have noticed that.. ;-D. Would you explain me writing the “needed” lines that you are talking about??? PLEASE!!!

This is the code done (less or more) :

.

in = TIFFOpen(m_ovl+".tif", "r");
   
TIFFGetField(in, TIFFTAG_IMAGEWIDTH, &imageWidth);
if (in) {
      
            TIFFRGBAImage img;
            char emsg[1024];
            uint32* buf;

            if(TIFFRGBAImageBegin(&img,in,0,emsg)) {
                  
            buf = (uint32*)_TIFFmalloc(imageWidth*imageLength*SamplePerPixel);  //THE RASTER

            if (!buf)
                    goto OutOfDIBMemory;

            if (buf != NULL) {

                  if (TIFFRGBAImageGet(&img,buf,img.width,img.height)) {

                        // PROCESS RASTER DATA
                        // ¿¿WHAT MUST I DO HERE???

                  }
                  free (buf);
            }

            TIFFRGBAImageEnd(&img);
            
            }
      }


Thanks!!!

0
 

Author Comment

by:Sol
ID: 1324067
Adjusted points to 160
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 12

Expert Comment

by:rwilson032697
ID: 1324068
// PROCESS RASTER DATA

// Assuming buf now points to the memory containing the pixels and the declarations in my answer then

// To get the RedValue and to something with it do this
 RedValue = ((PixelArray *)Buf)[y*imageWidth+x].red;

This could be optimised by extracting the entire pixel structure to reduce the calculations and pointer dereferences...

Cheers,

Raymond.
 
0
 

Author Comment

by:Sol
ID: 1324069
Hi Raymond!!

I’ve done:

struct APixel              
{
BYTE red,green,blue,alpha;  
};

On the other hand,

Typedef PixelArray Apixel();

Doesn’t work...Why?? What must i write??? PixelArray isn’t a type declaration....

Then, when i wrote :

RedValue = ((PixelArray *)Buf)[y*imageWidth+x].red;

in the process raster data...but:

1- What kind of data is RedValue?? BYTE??

Would you explain me the typedef and the redvalue errors??

THANKS!!!

0
 
LVL 12

Expert Comment

by:rwilson032697
ID: 1324070
Hi,

RedValue is a BYTE (the same as the samples in the Pixel struct.

Try Typdef APixel PixelArray[]; // What I'm saying is PixelArray is an unsized array of APixel structures. I'm sorry but my C declaration syntax is a little rusty.

You may also need to change

RedValue = ((PixelArray *)Buf)[y*imageWidth+x].red;

to

RedValue = (* (PixelArray *)Buf)[y*imageWidth+x].red;

if the compiler still gives you trouble with it.

Raymond.

0
 

Author Comment

by:Sol
ID: 1324071
The last question rwilson ;-D

I've tried Typdef APixel PixelArray[]; but, unfortunatelly, it doen't work... :-((

I know that you want to define PixelArray as an unsized array of APixel structures. How can i do that!! I've tried a lot of things...

I think it's the last step to resolve the problem...

You have helped me very much. Thanks...
0
 
LVL 12

Expert Comment

by:rwilson032697
ID: 1324072
Sorry for the delay Sol - E-E went off-line for me for 2-3 days...

I assume when you say it does not work that it does not compile...

Try

Typdef Struct APixel PixelArray[];

That should do it.

Raymond.
0
 

Author Comment

by:Sol
ID: 1324073
Hi Raymond!!

Oh my God....I’ve tried your solution but this message appear:

First-chance exception in X_Sheet.exe: 0xC0000005: Access Violation.

at the first time that i was reading the buffer!!! (a=0,b=0) ......

Do you know what’s happening???

Look at the code done...

struct APixel              
{
BYTE red,green,blue,alpha;  
};
                        
buf = (uint32*)_TIFFmalloc(imageWidth*imageLength*SamplePerPixel);

if (buf != NULL) {
      
      if (TIFFReadRGBAImage(in,imageWidth,imageLength,buf,0)) {
                                    
            int Alpha,Red,Green,Blue,Offset;
            BYTE RedValue,GreenValue,BlueValue,AlphaValue;
            typedef struct APixel PixelArray[];

            for (a=0;a<=imageWidth;a++) {
                                    
            for (b=0;b<=imageLength;b++) {

                        RedValue = (*(PixelArray *)buf[b*imageWidth+a])->red;
                           GreenValue = (*(PixelArray *)buf[b*imageWidth+a])->green;
                        BlueValue = (*(PixelArray *)buf[b*imageWidth+a])->blue;
                        AlphaValue = (*(PixelArray *)buf[b*imageWidth+a])->alpha;

                  }
            }
      }
                  
      _TIFFfree (buf);

I don't understand it, because the image is 2138x889....
0
 
LVL 12

Expert Comment

by:rwilson032697
ID: 1324074
Do it like this...


RedValue = (*(PixelArray *)buf)[b*imageWidth+a].red;
GreenValue = (*(PixelArray *)buf)[b*imageWidth+a].green;
BlueValue = (*(PixelArray *)buf)[b*imageWidth+a].lue;
AlphaValue = (*(PixelArray *)buf)[b*imageWidth+a].alpha;

I may be getting the pointers a bit messed up, if this doesn't work try:

RedValue = (PixelArray)buf[b*imageWidth+a].red;
GreenValue = (PixelArray)buf[b*imageWidth+a].green;
BlueValue = (PixelArray)buf[b*imageWidth+a].lue;
AlphaValue = (PixelArray)buf[b*imageWidth+a].alpha;

(C may be treating PixelArray as a pointer already...)

Raymond
0
 

Author Comment

by:Sol
ID: 1324075
I’ve tried this:

RedValue = (*(PixelArray *)buf)[b*imageWidth+a].red;
GreenValue = (*(PixelArray *)buf)[b*imageWidth+a].green;
BlueValue = (*(PixelArray *)buf)[b*imageWidth+a].lue;
AlphaValue = (*(PixelArray *)buf)[b*imageWidth+a].alpha;

(The other doesn’t compile...)

But, unfortunately, look at the answer:

x = 0, y = 0, Alpha = 1598471007, Red = 2, Green = 7009384, Blue = 7009384

That’s imposible!!

Do you know why???

0
 
LVL 12

Expert Comment

by:rwilson032697
ID: 1324076
Try changing the (*(PixelArray to just ((PixelArray

Unfortunately I don't have the setup here to test it :-(

Alternatively, declare a variable pf PixelArray type and assign Buf to it, then just just the pizelarray variable instead of buf in a simple array lookup without the casts.

Raymond.
0

Featured Post

Moving data to the cloud? Find out if you’re ready

Before moving to the cloud, it is important to carefully define your db needs, plan for the migration & understand prod. environment. This wp explains how to define what you need from a cloud provider, plan for the migration & what putting a cloud solution into practice entails.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This is to be the first in a series of articles demonstrating the development of a complete windows based application using the MFC classes.  I’ll try to keep each article focused on one (or a couple) of the tasks that one may meet.   Introductio…
Introduction: Finishing the grid – keyboard support for arrow keys to manoeuvre, entering the numbers.  The PreTranslateMessage function is to be used to intercept and respond to keyboard events. Continuing from the fourth article about sudoku. …
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…
Suggested Courses

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question