How to convert from Grayscale to single color channel

I hope that someone here might be able to help. I am writing an application which loads in color channels separately, to be merged by the application The images are multi-page TIFF files. The trouble that I am running into is that the application loads the images into grayscale, so when I merge the channels, they are merged as grayscale, not as the correct colors.
I use the following on each color channel to load them in:
public static BufferedImage[] loadTiff(File multiPageFile, int colour) {
    BufferedImage[] img = null;
    ArrayList<BufferedImage> tmp = new ArrayList<BufferedImage>();
    try {
        SeekableStream stream = new FileSeekableStream(multiPageFile);
        String[] names = ImageCodec.getDecoderNames(stream);
        ImageDecoder dec = ImageCodec.createImageDecoder(names[0], stream, null);
        int numPages = dec.getNumPages();
        for (int i=0; i<numPages; i++) {
         try {
                RenderedImage im = dec.decodeAsRenderedImage(i);
                BufferedImage bi = PlanarImage.wrapRenderedImage(im).getAsBufferedImage();
          //In case not all pages are valid images, we use ArrayList as it is dynamic
          tmp.add(bi);
            }catch (RuntimeException re) {
          //Once the exception is caught, we can decode the layer here for whatever means.   
    }catch (FileNotFoundException fnfe) {
        System.err.println("File not found.");
        fnfe.printStackTrace();
    }catch (Exception e) {
        System.err.println("General Exception caught");
        e.printStackTrace();
    //Convert to an array to be returned.
    img = new BufferedImage[tmp.size()];
    for (int i=0; i<img.length; i++) {
        img[i] = tmp.get(i);
    return img;
}So a BufferedImage array is returned (one element in the array for each page in the multi-page file). Before I return the BufferedImage array, though, it would be good to convert it to the correct color. I know which channel is currently being loaded. Does anyone have any tips on how to shift from a grayscale intensity to a single color intensity? I have tried a number of things regarding byte shifting, but I am really none the wiser.
I have tried, for example, reading the int color value:
int color = bi.getRGB(w,h);The trouble is that this number is not in the region 0..255, so I don't know how I can use it. If I shift the number:
color = color>>16I get a number smaller than 255 (although it seems to be negative), but I don't know whether I can use this value for a single color channel, i.e. if the current color is red, saying:
Color redC = new Color(color, 0, 0);
bi.setRGB(w,h,redC); //Slow and inefficient. There must be a better way.When I try doing that, I end up with nothing drawn or visible.
Does anyone have any ideas? Thanks in advance.
Edited by: doughnuts64 on Jun 12, 2008 6:27 AM

Thanks for the response. It has been helpful in getting my problem sorted. I was not creating a new BufferedImage - I was just using the one that had been loaded in, and as such had not realised that the type was not TYPE_INT_RGB. As soon as I instantiated a new BufferedImage of the same dimensions and colour TYPE_INT_RGB as you suggested, it worked! So thanks for that.
Whilst that does work, it does slow the application incredibly as it has so much data to cycle through. Ideally, I would be able to load the image in directly as the colour channel in question, rather than loading in as grayscale. This way, the application would only require the amount of time needed to load in the images, not to process them on a pixel-by-pixel basis (typical images are made up of over 16,000,000 pixels for each colour channel, and we typically load two files in at once - both for separate colour channels, so over 32,000,000 pixels to process).
As I am loading in multi-page tiff files, I am having to use the JAI (Java Advanced Imaging) toolkit. I use the following code to load in my multi-page tiff into a BufferedImage array, where each element of the array represents one of the 'pages' in the multi-page tiff file:
SeekableStream stream = new FileSeekableStream(multiPageFile);
String[] names = ImageCodec.getDecoderNames(stream);
ImageDecoder dec = ImageCodec.createImageDecoder(names[0], stream, null);
int numPages = dec.getNumPages();
BufferedImage[] finalImage = new BufferedImage[numPages];
for (int i=0; i<numPages; i++) {
     RenderedImage im = dec.decodeAsRenderedImage(i);
     finalImage[i] = PlanarImage.wrapRenderedImage(im).getAsBufferedImage();
     //...colour changing code currently here - loops through finalImage array, and on every pixel of every layer
...To be able to automatically convert the colour channel without having to loop on every pixel (and thus save an incredible amount of loading time) would be helpful.
Thanks.

Similar Messages

  • Re: How to converting from PL/SQL query to T-SQL query

    How to converting from PL/SQL query to T-SQL query... Its Urgent

    Download the
    SQL Server Migration Assistant for Oracle.  It will convert whole Oracle databases, or single queries or PL/SQL stored procedures.
    With caution that If your database is using Collation which is case sensitive SSMA will not work.SSMA doesnt guarantees 100% for conversion of Queries/stored proc /database if it fails to do so for some queries you will have to do it manually.
    But you can try
    Please mark this reply as the answer or vote as helpful, as appropriate, to make it useful for other readers

  • Hi iam new to java can u tell me  how to convert from hashmap to string

    how to convert from hashmap to string

    Hi,
    This is not pure Java forum. Its more on JDBC and data connectivity to Orcle db from Java API.
    This link may answer your question:
    http://stackoverflow.com/questions/960807/hashmapkey-string-value-arraylist-returns-an-object-instead-of-arraylist
    Twinkle

  • How to print from iphone to hp color laserjet CM1017 MFP printer

    How to print from iphone to hp color laserjet CM1017 MFP printer

    In order to AirPrint from your iPad/iPhone, here's all you need to do:
    1. Make sure the printer and iPad/iPhone are connected to the same local network.
    2. Open something you want to print (on the iPad/iPhone).
    3. On the iPad/iPhone, tap the "Action" button (arrow curving out of a box).
    4. Tap "Print".
    If you have any issues with printing, below are some steps that should help you out!
    1. Power off the printer.
    2. Completely power off the iOS devices (hold the Sleep and Home buttons simultaneously until a message appears asking you to "Slide to power off". Slide across that bar, and it should completely power off).
    3. Power the printer back on.
    4. Power the iOS devices back on.
    5. Try to print again.
    Those steps usually resolve the issue. If they do not, try this additional steps:
    6. Power cycle your router (usually done by unplugging the router’s power cord, waiting 20 seconds, and plugging it back in).
    7. Make sure that the iOS devices and the printer are all connected to the same network, then try to print.
    If I have SOLVED your issue, please feel free to provide KUDOS and make sure you mark this thread as SOLUTION PROVIDED!
    Although I work for HP, my posts and replies are my own opinion and not those of HP.

  • How to convert from Restricted material to Unrested Material

    Hi Every Body,
         I have Posted the Batch Material in MB1C . But it was posted in RESTRICTED IN USE. How to convert from Restrict To UNRESTRICTED IN USE. Can Anybody Help me.

    Thank you for solving the problem both SAP Community Network and Mr Jurgen .
    suribabu

  • How to convert from image based pdf to text based pdf

    I have Adobe 9 Pro. How to convert from image based pdf to text based pdf? For example, if someone emails a scanned pdf to me, how do I convert that document into a text based pdf?

    To perform OCR, open the document and select: Document > OCR Text Recognition > Recognize Text Using OCR
    More information on the various options is in the Acrobat help doc.

  • How to convert from java.lang.Integer to int

    Could you please show me
    how to convert from java.lang.Integer to int?
    and how to convert from java.lang.Integer to String?
    Thanks,
    Minh

    Could you please show me
    how to convert from java.lang.Integer to int?
    and how to convert from java.lang.Integer to String?Tip: always keep a browser open on the API docs; if you've got a
    couple of MBs to spare, download the docs; it's very convenient.
    kind regards,
    Jos

  • How to convert from array to graph ?

    how to convert from array to graph ?

    A graph will accept an array of numbers.  Do you have an example of what you are looking for?
    And since somebody mentioned tutorials...
    3 Hour Introduction
    6 Hour Introduction
    LabVEW Basics
    Self Paced training for students
    Self Paced training beginner to advanced, SSP Required
    LabVIEW Wiki on Training
    Learning NI
    Getting Started with NI Products
    There are only two ways to tell somebody thanks: Kudos and Marked Solutions
    Unofficial Forum Rules and Guidelines

  • How to convert from pdf to word

    how to convert from pdf to word

    If you are using Adobe Acrobat please post here: http://forums.adobe.com/community/acrobat/creating__editing_%26_exporting_pdfs
    If you are using Adobe Reader and ExportPDF, please read this: http://forums.adobe.com/docs/DOC-1812

  • How to convert from XML to Array ?

    how to convert from XML to Array ?
    thanks in advance

    this is a segment of the xml object:
    var myXML:XML =
    <data>
    <task>
    <taskID>2</taskID>
    <startDate>2/15/2007</startDate>
    </task>
    </data>
    i want to conver myXML into ArrayCollection: like this:
    private var expenses:Array = [
    {taskID:"1", startDate:"2/15/2007"},
    {taskID:"2", startDate:"4/15/2007"}
    how i can do it ? and tell me how to retrieve the data from
    the collection
    thanks

  • How to convert from military time (string) to regular time?

    How to convert from military time (string) to regular time?
    First of all I need to let you know I'm very new to Crystal Reports, so I apologize and will need detailed instructions if possible.
    I have created a Crystal Report pulling information from the received time field.  On the report, the time is showing in military time (ex. 16:00:00).
    How can I convert this time to normal time (ex. 4:00:00)
    Since the field is a 'String' field and not a 'Time' field, I do not have the option of changing it under the field properties.
    I hope someone can help with this.
    Thank you in advance.

    As my fields were date only and not datetime, I ended up using the following,
    If IsDate({CallLog.EmailRecdDate}) Then
         CDate({CallLog.EmailRecdDate})
    Else
         CDate(0,0,0)
    You answer lead me in the right direction.
    I'm still having an issue with the 'Else' statment returning 12:00:00 because of the CDate(0,0,0), but I'm not sure what else I can use to return a blank space on the report.  Any ideas?
    Thanks again!

  • How to convert from lowercase letters to uppercase letters?

    how to convert from lowercase letters to uppercase letters?

    Hi KS,
    In the transfer / update rules you can use a formula to convert lowercase to upper case letters.
    Functions for Character string>>TOUPPER(CHAR)
    Hope this helps.
    Bhargava
    Message was edited by:
            Sista Bhargava Kumar

  • How to convert from xml file to html using java code

    How to convert from xml file to html file using java code

    Get yourself Apache Xalan or Saxon or some XSLT processor
    String styleSheet = "/YourXSLTStylesheet.xsl";
    String dataSource = "/YourXMLDocument.xml";
    InputStream stylesheetSource = TransformMe.class.getResourceAsStream(styleSheet);
    InputStream dataSourceStream = TransformMe.class.getResourceAsStream(dataSource);
    OutputStream transformedOut = new FileOutputStream("filename.html");
    TransformerFactory tFactory = TransformerFactory.newInstance();
    Transformer transformer = tFactory.newTransformer(new StreamSource(stylesheetSource));
    transformer.transform(new StreamSource(dataSourceStream), new StreamResult(transformedOut));You'll also need to learn XSLT if you don't already know that. Here's a good place to start
    http://www.w3schools.com/xsl/

  • How to convert  from  varchar to blob ?

    How to convert from varchar to blob ?
    thanks

    Here is a small PL/SQL block that we have used to convert varchar2 to blob.
    declare
    cursor get_blob is
    select blob_statement
    from report
    where report_id = 205
    FOR UPDATE OF blob_statement;
    v_loc blob;
    v_raw_buffer raw(32767);
    v_amount binary_integer := 32767;
    v_offset binary_integer := 1;
    v_buffer VARCHAR2(32767);
    begin
    open get_blob;
    fetch get_blob into v_loc;
    close get_blob;
    v_buffer := 'Sample text';
    v_raw_buffer := utl_raw.cast_to_raw(v_buffer);
    v_amount := utl_raw.length(v_raw_buffer);
    dbms_lob.write(v_loc, v_amount, v_offset, v_raw_buffer);
    commit;
    end;

  • Automatically convert from RGB to CMYK color mode

    I have over 900 barcodes provided in EPS format. When I open them in illustrator, they are coming in as an RGB color space file. I need them gray scale solid black in CMYK color mode. I can batch using actions to convert the artwork using "Edit Colors > Convert to Grayscale". However, the document color mode remains RGB. It appears, even after using "Convert to Grayscale", that the RGB color mode goofs up the placed .ai file in InDesign, treating it as RGB and seeing it as a mix of CMYK rather than the 100% black only it needs to be for proper sharp printing.
    The actions pallet doesn't record converting the document color mode. Is there a way to automate that file conversion so they are all saved as CMYK rather than RGB, or am I stuck opening each of the 900+ files manually?

    While recording the action, from the popup menu in the Actions Panel choose Insert Menu Item, then from the main menu choose File > Document Color Mode > CMYK color.

Maybe you are looking for