| 1 |
/* |
|---|
| 2 |
* Moonlight|3D Copyright (C) 2005 The Moonlight|3D team |
|---|
| 3 |
* |
|---|
| 4 |
* This library is free software; you can redistribute it and/or modify it under |
|---|
| 5 |
* the terms of the GNU Lesser General Public License as published by the Free |
|---|
| 6 |
* Software Foundation; either version 2.1 of the License, or (at your option) |
|---|
| 7 |
* any later version. |
|---|
| 8 |
* |
|---|
| 9 |
* This library is distributed in the hope that it will be useful, but WITHOUT |
|---|
| 10 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
|---|
| 11 |
* FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more |
|---|
| 12 |
* details. |
|---|
| 13 |
* |
|---|
| 14 |
* You should have received a copy of the GNU Lesser General Public License |
|---|
| 15 |
* along with this library; if not, write to the Free Software Foundation, Inc., |
|---|
| 16 |
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
|---|
| 17 |
* |
|---|
| 18 |
* Created on Oct 31, 2005 |
|---|
| 19 |
*/ |
|---|
| 20 |
package eu.moonlight3d.common.image.pfm; |
|---|
| 21 |
|
|---|
| 22 |
import java.io.FileNotFoundException; |
|---|
| 23 |
import java.io.IOException; |
|---|
| 24 |
import java.io.RandomAccessFile; |
|---|
| 25 |
import java.nio.ByteOrder; |
|---|
| 26 |
import java.nio.MappedByteBuffer; |
|---|
| 27 |
import java.nio.channels.FileChannel; |
|---|
| 28 |
import java.util.ArrayList; |
|---|
| 29 |
|
|---|
| 30 |
import eu.moonlight3d.common.image.core.Channel; |
|---|
| 31 |
import eu.moonlight3d.common.image.core.Framebuffer; |
|---|
| 32 |
import eu.moonlight3d.framework.State; |
|---|
| 33 |
import eu.moonlight3d.framework.helper.FileType; |
|---|
| 34 |
import eu.moonlight3d.framework.helper.StringTools; |
|---|
| 35 |
import eu.moonlight3d.framework.localization.L10N; |
|---|
| 36 |
|
|---|
| 37 |
/** |
|---|
| 38 |
* Support for the PFM immage file format. |
|---|
| 39 |
* |
|---|
| 40 |
* @author gregor |
|---|
| 41 |
*/ |
|---|
| 42 |
public class ImageFormat implements eu.moonlight3d.common.image.core.ImageFormat { |
|---|
| 43 |
|
|---|
| 44 |
public String getName() { |
|---|
| 45 |
return "Portable Float Map"; |
|---|
| 46 |
} |
|---|
| 47 |
|
|---|
| 48 |
public FileType getFileType() { |
|---|
| 49 |
ArrayList<String> patterns=new ArrayList<String>(); |
|---|
| 50 |
patterns.add("*.pfm"); |
|---|
| 51 |
return new FileType("Portable Float Map",patterns); |
|---|
| 52 |
} |
|---|
| 53 |
|
|---|
| 54 |
public boolean isLoadable() { |
|---|
| 55 |
return true; |
|---|
| 56 |
} |
|---|
| 57 |
|
|---|
| 58 |
public boolean canLoadFile(String filename) { |
|---|
| 59 |
try { |
|---|
| 60 |
// check if file has required signature |
|---|
| 61 |
RandomAccessFile input=new RandomAccessFile(filename,"r"); |
|---|
| 62 |
|
|---|
| 63 |
String header=input.readLine(); |
|---|
| 64 |
if(header.equals("Pf")) { |
|---|
| 65 |
State.getInstance().logger().warning(L10N.translate("Loading of grayscale portable float maps not implemented")); |
|---|
| 66 |
return false; |
|---|
| 67 |
} |
|---|
| 68 |
if(!header.equals("PF")) { // TODO grayscale float maps |
|---|
| 69 |
return false; |
|---|
| 70 |
} |
|---|
| 71 |
input.close(); |
|---|
| 72 |
} catch (FileNotFoundException e) { |
|---|
| 73 |
return false; |
|---|
| 74 |
} catch (IOException e) { |
|---|
| 75 |
return false; |
|---|
| 76 |
} |
|---|
| 77 |
return true; |
|---|
| 78 |
} |
|---|
| 79 |
|
|---|
| 80 |
public Framebuffer loadFile(String filename) { |
|---|
| 81 |
try { |
|---|
| 82 |
RandomAccessFile input=new RandomAccessFile(filename,"r"); |
|---|
| 83 |
|
|---|
| 84 |
String header=input.readLine(); |
|---|
| 85 |
|
|---|
| 86 |
if(!header.equals("PF")) { |
|---|
| 87 |
return null; |
|---|
| 88 |
} |
|---|
| 89 |
|
|---|
| 90 |
String resString=input.readLine(); |
|---|
| 91 |
String res[]=StringTools.split(resString," "); |
|---|
| 92 |
int width=Integer.parseInt(res[0]); |
|---|
| 93 |
int height=Integer.parseInt(res[1]); |
|---|
| 94 |
|
|---|
| 95 |
String scaleString=input.readLine(); |
|---|
| 96 |
float scale=Float.parseFloat(scaleString); |
|---|
| 97 |
|
|---|
| 98 |
ByteOrder byteOrder; |
|---|
| 99 |
if(scale<0) { |
|---|
| 100 |
byteOrder=ByteOrder.LITTLE_ENDIAN; |
|---|
| 101 |
} else { |
|---|
| 102 |
byteOrder=ByteOrder.BIG_ENDIAN; |
|---|
| 103 |
} |
|---|
| 104 |
scale=Math.abs(scale); |
|---|
| 105 |
|
|---|
| 106 |
Framebuffer framebuffer=new Framebuffer(width, height); |
|---|
| 107 |
framebuffer.addChannel("red"); |
|---|
| 108 |
framebuffer.addChannel("green"); |
|---|
| 109 |
framebuffer.addChannel("blue"); |
|---|
| 110 |
|
|---|
| 111 |
FileChannel fileChannel=input.getChannel(); |
|---|
| 112 |
MappedByteBuffer buffer=fileChannel.map(FileChannel.MapMode.READ_ONLY, 0, input.length()); |
|---|
| 113 |
buffer.position((int)input.getFilePointer()); |
|---|
| 114 |
buffer.order(byteOrder); |
|---|
| 115 |
|
|---|
| 116 |
Channel red=framebuffer.getChannel("red"); |
|---|
| 117 |
Channel green=framebuffer.getChannel("green"); |
|---|
| 118 |
Channel blue=framebuffer.getChannel("blue"); |
|---|
| 119 |
for(int y=0;y<framebuffer.getHeight();y++) { |
|---|
| 120 |
for(int x=0;x<framebuffer.getWidth();x++) { |
|---|
| 121 |
red.putPixel(x, y, scale*buffer.getFloat()); |
|---|
| 122 |
green.putPixel(x, y, scale*buffer.getFloat()); |
|---|
| 123 |
blue.putPixel(x, y, scale*buffer.getFloat()); |
|---|
| 124 |
} |
|---|
| 125 |
} |
|---|
| 126 |
|
|---|
| 127 |
return framebuffer; |
|---|
| 128 |
} catch (FileNotFoundException e) { |
|---|
| 129 |
e.printStackTrace(); |
|---|
| 130 |
} catch (IOException e) { |
|---|
| 131 |
e.printStackTrace(); |
|---|
| 132 |
} |
|---|
| 133 |
|
|---|
| 134 |
return null; |
|---|
| 135 |
} |
|---|
| 136 |
|
|---|
| 137 |
public boolean isSaveable() { |
|---|
| 138 |
return true; |
|---|
| 139 |
} |
|---|
| 140 |
|
|---|
| 141 |
public void saveFile(String filename, Framebuffer buffer) { |
|---|
| 142 |
try { |
|---|
| 143 |
RandomAccessFile outputFile=new RandomAccessFile(filename,"rw"); |
|---|
| 144 |
|
|---|
| 145 |
outputFile.setLength(0); |
|---|
| 146 |
|
|---|
| 147 |
outputFile.writeBytes("PF\n"); |
|---|
| 148 |
outputFile.writeBytes(buffer.getWidth() + " " + buffer.getHeight() + "\n"); |
|---|
| 149 |
if(ByteOrder.nativeOrder()==ByteOrder.BIG_ENDIAN) { |
|---|
| 150 |
outputFile.writeBytes("1.0\n"); |
|---|
| 151 |
} else { |
|---|
| 152 |
outputFile.writeBytes("-1.0\n"); |
|---|
| 153 |
} |
|---|
| 154 |
|
|---|
| 155 |
Channel red=buffer.getChannel("red"); |
|---|
| 156 |
Channel green=buffer.getChannel("green"); |
|---|
| 157 |
Channel blue=buffer.getChannel("blue"); |
|---|
| 158 |
for(int y=0;y<buffer.getHeight();y++) { |
|---|
| 159 |
for(int x=0;x<buffer.getWidth();x++) { |
|---|
| 160 |
outputFile.writeFloat(red.getPixel(x, y)); |
|---|
| 161 |
outputFile.writeFloat(green.getPixel(x, y)); |
|---|
| 162 |
outputFile.writeFloat(blue.getPixel(x, y)); |
|---|
| 163 |
} |
|---|
| 164 |
} |
|---|
| 165 |
|
|---|
| 166 |
outputFile.close(); |
|---|
| 167 |
} catch (FileNotFoundException e) { |
|---|
| 168 |
e.printStackTrace(); |
|---|
| 169 |
} catch (IOException e) { |
|---|
| 170 |
e.printStackTrace(); |
|---|
| 171 |
} |
|---|
| 172 |
} |
|---|
| 173 |
|
|---|
| 174 |
} |
|---|