Login  Register

Re: Possible to access pixels without creating java.awt.image

Posted by Brian Willkie-2 on May 06, 2008; 3:07pm
URL: http://imagej.273.s1.nabble.com/Possible-to-access-pixels-without-creating-java-awt-image-tp3696328p3696329.html

Hi Wayne,

Ah, great! I'll take a look at ImageReader. Also off-list, Johannes  
Schindelin mentioned this:

> On X11 (Linux & Unix) it does not matter, because it is not heeded,  
> but on MacOSX, it could be the solution:
>
>        $ java -Djava.awt.headless=true -jar ij.jar -batch Arrays.txt
>
> I _think_ that the java.awt.headless property is respected on MacOSX.

A quick check on my laptop confirmed that this works. Unless some  
other obstacle comes up, this is likely a better solution. It might be  
worth adding to the wiki:

http://imagejdocu.tudor.lu/imagej-documentation-wiki/faq/how-do-i-run-imagej-without-a-graphics-environment-headless

I could set up an account to add it, unless someone who already has an  
account would like to. :)

Thanks to Johannes, Wayne, and Alberto for your help.

Cheers,
Brian


Quoting Wayne Rasband:

>
> On May 6, 2008, at 8:15 AM, [hidden email] wrote:
>
>> Hi Wayne,
>>
>> Thanks for the reply.
>>
>>> public ColorProcessor(int width, int height, int[] pixels) {
>> But that presupposes that I have the pixels. How do I get the  
>> pixels without creating an instance java.awt.Image?
>
> The ij.io.ImageReader class returns pixel arrays.
>
>> I'm trying to use the ImageJ API to build a command-line  
>> application. The problem that I'm running into is that core ImageJ  
>> classes (like ImagePlus, ImageProcessor, ImageStack) instantiate  
>> java.awt components (like ColorModel and Image). These cause my app  
>>  to create a GUI, even if they aren't visible, and even if I use  
>> headless ImageJA. Creating a GUI generates a window server error on  
>>  the Mac cluster I'm using.
>
> One way to avoid window server errors is to use Xvfb (X virtual framebuffer).
>
> -wayne
>
>> So I've been hacking up custom versions of the above classes,  
>> trying to strip out calls to java.awt classes, but I've reached a  
>> point where I have to instantiate a ColorProcessor. In order to  
>> make this work, I need to get rid of calls to  
>> ImageProcessor:getImage(). In order to do that, I need some  
>> alternative method to read in the pixels. java.awt.Image does it,  
>> so I assume it's possible. The question is "How?" (and the  
>> follow-up question is "How much code do I really want to re-write?").
>>
>> So I'm looking for suggestions to either question.
>>
>> Thanks,
>> Brian
>>
>>
>> Quoting Rasband Wayne:
>>
>>>> Is it possible to create an ImageProcessor (one that accesses the  
>>>>   pixels in a given tif file) without creating an instance of    
>>>> java.awt.Image?
>>>>
>>>>
>>>> public ColorProcessor(Image img) {
>>>> width = img.getWidth(null);
>>>> height = img.getHeight(null);
>>>> pixels = new int[width * height];
>>>> PixelGrabber pg = new PixelGrabber(img, 0, 0, width, height,    
>>>> pixels, 0, width);
>>>> try {
>>>> pg.grabPixels();
>>>> } catch (InterruptedException e){};
>>>> createColorModel();
>>>> fgColor = 0xff000000; //black
>>>> resetRoi();
>>>> }
>>>>
>>>> Thanks,
>>>> Brian
>>>
>>> Brian,
>>>
>>> You can use this constructor
>>>
>>> /**Creates a ColorProcessor from a pixel array. */
>>> public ColorProcessor(int width, int height, int[] pixels) {
>>> if (pixels!=null && width*height!=pixels.length)
>>> throw new IllegalArgumentException(WRONG_LENGTH);
>>> this.width = width;
>>> this.height = height;
>>> createColorModel();
>>> fgColor = 0xff000000; //black
>>> resetRoi();
>>> this.pixels = pixels;
>>> }
>>>
>>> to create an ImageProcessor from an array of RGB pixels. What are you
>>> trying to accomplish?
>>>
>>> -wayne
>>
>>
>>