I´ve followed Roman´s directions (thanks Roman!) and I´ve coded something that
(seems to) partially solves my problem.
I am not sure if it´s correct (it seems to be, based on a few png´s I´ve
generated), but I still have to test the performance using my application, that
generates much more than two small red squares :-)
Here goes the code. Please check my comments.
Simone, thanks. What would you suggest me to change in my code?
thanks in advance
public class HugeDiskImage {
public static void main(String[] args) {
try {
int h = 20000;
int w = 2000;
/*
* for w == 20000
* Exception in thread "main"
java.lang.IllegalArgumentException: Negative size
at sun.nio.ch.FileChannelImpl.map(Unknown
Source)
at YourSuggestion.main(YourSuggestion.java:27)
why?
*/
try{
BufferedImage bim = new
BufferedImage(w,h,BufferedImage.TYPE_INT_RGB);
}catch(java.lang.OutOfMemoryError error){
System.out.println("as expected....");
}
File file = new File("c:\\swap.mem");
// Create a read-write memory-mapped file
FileChannel rwChannel = new RandomAccessFile(file,
"rw").getChannel();
ByteBuffer wrBuf =
rwChannel.map(FileChannel.MapMode.READ_WRITE, 0,
6*w*h/*(int)rwChannel.size()*/); //negative size exception is raised here
IntBuffer iBuf = wrBuf.asIntBuffer();
//custom data buffer
DataBufferSubClass db = new
DataBufferSubClass(DataBuffer.TYPE_INT,3*h*w); //2 GB
db.setWrBuf(iBuf);
ColorModel cm = new DirectColorModel(24, 0x00ff0000,
0x0000ff00, 0x000000ff);
SampleModel sm = cm.createCompatibleSampleModel(w,h);
WritableRaster wr = new MyWritableRaster(sm, db, new
Point(0,0));
BufferedImage bim = new BufferedImage(cm, wr, true,
null);
/*
* the commented code does not work because of open bug
*
http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6353518
*
* probably related to
*
http://archives.java.sun.com/cgi-bin/wa?A2=ind0403&L=java2d-interest&P=5337
*
ColorModel colorModel = new DirectColorModel(24, 0x00ff0000,
0x0000ff00, 0x000000ff);
int[] masks= {0x00ff0000, 0x0000ff00, 0x000000ff};
WritableRaster raster = Raster.createPackedRaster(db,
w,
h,
3, masks, null);
BufferedImage image =
new BufferedImage(colorModel, raster, false, null);
*/
Graphics2D g2d = bim.createGraphics();
g2d.setColor(Color.BLUE);
g2d.fillRect(0, 0, bim.getWidth(), bim.getHeight());
g2d.setColor(Color.RED);
g2d.fillRect(0, 0, 10, 10);
g2d.fillRect(bim.getWidth()-10, bim.getHeight()-10, 10, 10);
ImageIO.write(bim, "png", new File("C:\\image.png"));
} catch (IOException e) {
e.printStackTrace();
}
}
}
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
import java.nio.IntBuffer;
public class DataBufferSubClass extends DataBuffer {
private IntBuffer wrBuf;
public void setWrBuf(IntBuffer wrBuf) {
this.wrBuf = wrBuf;
}
public DataBufferSubClass(int dataType, int size) {
super(dataType, size);
// TODO Auto-generated constructor stub
}
public DataBufferSubClass(int dataType, int size, int numBanks) {
super(dataType, size, numBanks);
// TODO Auto-generated constructor stub
}
public DataBufferSubClass(int dataType, int size, int numBanks, int
offset) {
super(dataType, size, numBanks, offset);
// TODO Auto-generated constructor stub
}
public DataBufferSubClass(int dataType, int size, int numBanks,
int[] offsets) {
super(dataType, size, numBanks, offsets);
// TODO Auto-generated constructor stub
}
@Override
public int getElem(int bank, int i) {
// TODO Auto-generated method stub
return wrBuf.get(i);
}
@Override
public void setElem(int bank, int i, int val) {
// TODO Auto-generated method stub
wrBuf.put(i, val);
}
}
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
import java.awt.Point;
import java.awt.image.DataBuffer;
import java.awt.image.SampleModel;
import java.awt.image.WritableRaster;
public class MyWritableRaster extends WritableRaster{
public MyWritableRaster(SampleModel sampleModel, DataBuffer dataBuffer,
Point origin)
{
super(sampleModel, dataBuffer, origin);
}
}
[Message sent by forum member 'shikida' (shikida)]
http://forums.java.net/jive/thread.jspa?messageID=204912
===========================================================================
To unsubscribe, send email to [EMAIL PROTECTED] and include in the body
of the message "signoff JAVA2D-INTEREST". For general help, send email to
[EMAIL PROTECTED] and include in the body of the message "help".