Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

I have a byte array stored in a file. I want the quickest, easiest way of viewing the image whether it is writing code or using a utility or web page.

share|improve this question

closed as not a real question by Martin Beckett, Glenn Nelson, Walter, nikie, ChrisF Feb 21 '12 at 23:31

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.If this question can be reworded to fit the rules in the help center, please edit the question.

    
Do you know what format the picture is (eg. bmp, jpeg, gif, etc.)? –  Hand-E-Food Feb 19 '12 at 22:03
    
its not in any of those formats its just a byte array containing rgb values repeating –  Xavier Feb 19 '12 at 22:08
3  
Depends on your windowing library you're using/want to use. Native Windows application? Or Linux (X.org / Qt / Gtk / ..)? Or MacOS? Or ...? –  johannes Feb 19 '12 at 22:14
2  
How would you know the resolution to turn it into a meaningful picture if it's just an array of bytes? What's the color depth (8,16,24 bit)? 800x600? 1024x768? 100x100? –  jmq Feb 20 '12 at 6:00
    
-1, without knowing the OS or other requirements, 'easiest' isnt something that can be answered. –  GrandmasterB Feb 20 '12 at 7:14
add comment

4 Answers

If your situation is that you've got a piece of data and you want to see what it looks like as RGB values, try using the PPM file format. It's trivially simple, and writing a tool to convert the byte array to PPM should be no more than a few lines of code.

PPM file format description: http://en.wikipedia.org/wiki/Netpbm_format

share|improve this answer
add comment

Most UI frameworks will give you a Graphics object that you can write directly to. You have to know the stride for the image (I.E. how many pixels per row) and the bits per pixel. But knowing those two values, you will be able to feed the Array to a function that will render the image in a defined area.

share|improve this answer
add comment

In Java, you can create and display an image from a data array pretty easily, the example code below creates a GUI that shows a 100x100 random image constructed from an int[] array:

package testswing;

import java.awt.*;
import java.awt.image.*;
import java.util.Random;
import javax.swing.*;

public class App {
    static class MyPanel extends JPanel {
        BufferedImage b;
        Random r=new java.util.Random();

        public MyPanel() {
            int w=100;
            int h=100;
            int[] pix=new int[w*h];
            for (int i=0; i<pix.length; i++) {
                pix[i]=r.nextInt();
            }
            Image im= createImage(new MemoryImageSource(w, h, pix, 0, w));
            b=new BufferedImage(w,h,BufferedImage.TYPE_INT_ARGB);
            b.getGraphics().drawImage(im, 0,0,w,h,null);
            setPreferredSize(new Dimension(b.getHeight(),b.getHeight()));
        }

        @Override public void paintComponent(Graphics g) {
            g.drawImage(b,0,0,b.getWidth(),b.getHeight(),null);
        }
    }

    public static void main(String[] args) {
        JFrame f=new JFrame("Hello World");
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.setVisible(true);

        MyPanel mp=new MyPanel();
        f.getContentPane().add(mp);
        f.pack();
    }

}

To display your image from a file, all you need to do is load your bytes and then convert them into the appropriate int[] ARGB array. Something like:

  File f=new File("mybytes.raw");
  DataInputStream din = new DataInputStream(new FileInputStream(f));
  byte b[] = new byte[f.length()];
  din.read(b);
  din.close();

Note that how you convert the bytes to the int[] ARGB format will depend on your particular image format.

share|improve this answer
add comment

I haven't tried this, but as I understand this, ImageMagick's RGB format should do just what you want.

share|improve this answer
add comment

Not the answer you're looking for? Browse other questions tagged or ask your own question.