Search...
FAQs
Subscribe
Pie
FAQs
Recent topics
Flagged topics
Hot topics
Best topics
Search...
Search within I/O and Streams
Search Coderanch
Advance search
Google search
Register / Login
Post Reply
Bookmark Topic
Watch Topic
New Topic
programming forums
Java
Mobile
Certification
Databases
Caching
Books
Engineering
Micro Controllers
OS
Languages
Paradigms
IDEs
Build Tools
Frameworks
Application Servers
Open Source
This Site
Careers
Other
Pie Elite
all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Campbell Ritchie
Jeanne Boyarsky
Ron McLeod
Paul Clapham
Liutauras Vilda
Sheriffs:
paul wheaton
Rob Spoor
Devaka Cooray
Saloon Keepers:
Stephan van Hulst
Tim Holloway
Carey Brown
Frits Walraven
Tim Moores
Bartenders:
Mikalai Zaikin
Forum:
I/O and Streams
BufferedImage 1-bit-per-pixel
Orna Vel
Greenhorn
Posts: 12
posted 18 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
I am trying to create the application with "compact" BufferedImage which uses just 1 bit per pixel. But it does not work properly - it looks like it still uses a byte per pixel. Any thoughts?
class BWBufImagePanel extends JPanel{ BufferedImage fBufferedImage = null; int fWidth = 10, fHeight = 10; byte [] data = null; /** Build a BufferedImage from a pixel array. **/ void makeImage () { System.out.println("Size:" + fWidth + " : " + fHeight); data = new byte[fWidth * fHeight]; for (int i = 0; i < fWidth*fHeight; data[i]=(byte)(i%2), i++); byte[] bw = {(byte) 0xff, (byte) 0}; IndexColorModel blackAndWhite = new IndexColorModel( 1, 2, bw, bw, bw); fBufferedImage = new BufferedImage( fWidth, fHeight, BufferedImage.TYPE_BYTE_BINARY, blackAndWhite); // Get the writable raster so that data can be changed. WritableRaster wr = fBufferedImage.getRaster(); // Now write the byte data to the raster wr.setDataElements (0, 0, fWidth, fHeight, data); } /** Draw the image on the panel. **/ public void paintComponent (Graphics g) { super.paintComponent (g); if (fBufferedImage!= null) g.drawImage (fBufferedImage, 0, 0, this ); } // makeImage } // class BWBufImagePanel
Thanks.
Orna Vel
Greenhorn
Posts: 12
posted 18 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
Ok, now it works fine:
import javax.swing.*; import java.awt.*; import java.awt.image.*; class BWBufImagePanel extends JPanel{ BufferedImage fBufferedImage = null; int fWidth = 100, fHeight = 100; byte [] data = null; int bytesPerRow = fWidth/8 + (fWidth%8!=0?1:0); /** Build a BufferedImage from a pixel array. **/ void makeImage () { System.out.println("Size:" + fWidth + " : " + fHeight); data = new byte[fHeight * bytesPerRow]; for (int i = 0; i < data.length; data[i]=(byte)170 , System.out.print(" : " + data[i]), i++); System.out.println(); byte[] bw = {(byte) 0xff, (byte) 0}; IndexColorModel blackAndWhite = new IndexColorModel( 1, // One bit per pixel 2, // Two values in the component arrays bw, // Red Components bw, // Green Components bw);// Blue Components DataBuffer db = new DataBufferByte( data, data.length); // Get the writable raster so that data can be changed. WritableRaster wr = Raster.createPackedRaster(db, fWidth, fHeight, 1, null); // Create the BufferedImage fBufferedImage = new BufferedImage(blackAndWhite, wr, true, null); } /** Draw the image on the panel. **/ public void paintComponent (Graphics g) { super.paintComponent (g); if (fBufferedImage != null) g.drawImage (fBufferedImage, 0, 0, this ); } // makeImage } // class BWBufImagePanel
Put the moon back where you found it! We need it for tides and poetry and stuff. Like this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
reply
Bookmark Topic
Watch Topic
New Topic
Boost this thread!
Similar Threads
BufferedImage 1-bit-per-pixel
changing gray pixels....
Creating an image from a byte array and a color palette
bufferd Image!
Is there a better way to blend colors when painting with Graphics2D?
More...