removed image processing code
This commit is contained in:
parent
68b7ddff5c
commit
d5ed16d709
2 changed files with 0 additions and 324 deletions
|
@ -1,154 +0,0 @@
|
|||
package btools.mapcreator;
|
||||
|
||||
import java.io.BufferedOutputStream;
|
||||
import java.io.DataOutputStream;
|
||||
import java.io.File;
|
||||
import java.io.FileOutputStream;
|
||||
|
||||
import btools.util.Raster2Png;
|
||||
|
||||
public class TrafficData2Png
|
||||
{
|
||||
private static int minLon;
|
||||
private static int minLat;
|
||||
private static int maxLon;
|
||||
private static int maxLat;
|
||||
private static int ncols;
|
||||
private static int nrows;
|
||||
private static int[] pixels;
|
||||
|
||||
public static void main( String[] args ) throws Exception
|
||||
{
|
||||
if ( args.length == 8 )
|
||||
{
|
||||
doConvert( args[0], args[1], Double.parseDouble( args[2] ), Double.parseDouble( args[3] ), Double.parseDouble( args[4] ),
|
||||
Double.parseDouble( args[5] ), Integer.parseInt( args[6] ), Integer.parseInt( args[7] ) );
|
||||
}
|
||||
else if ( args.length == 4 )
|
||||
{
|
||||
int lon0 = Integer.parseInt( args[0] );
|
||||
int lat0 = Integer.parseInt( args[1] );
|
||||
String inputFile = "traffic/E" + lon0 + "_N" + lat0 + ".trf";
|
||||
for ( int lon = lon0; lon < lon0 + 5; lon++ )
|
||||
for ( int lat = lat0; lat < lat0 + 5; lat++ )
|
||||
{
|
||||
String imageFile = "traffic_pics/E" + lon + "_N" + lat + ".png";
|
||||
System.out.println( "file=" + inputFile + " image=" + imageFile );
|
||||
doConvert( inputFile, imageFile, lon, lat, lon + 1, lat + 1, Integer.parseInt( args[2] ), Integer.parseInt( args[3] ) );
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public static void doConvert( String inputFile, String imageFile, double lon0, double lat0, double lon1, double lat1, int cols, int rows )
|
||||
throws Exception
|
||||
{
|
||||
OsmTrafficMap trafficMap = new OsmTrafficMap();
|
||||
minLon = (int) ( lon0 * 1000000 + 180000000 );
|
||||
maxLon = (int) ( lon1 * 1000000 + 180000000 );
|
||||
minLat = (int) ( lat0 * 1000000 + 90000000 );
|
||||
maxLat = (int) ( lat1 * 1000000 + 90000000 );
|
||||
ncols = cols;
|
||||
nrows = rows;
|
||||
|
||||
long[] keys = trafficMap.load( new File( inputFile ), minLon, minLat, maxLon, maxLat, true );
|
||||
|
||||
pixels = new int[cols * rows];
|
||||
|
||||
int[] tclasses = new int[]
|
||||
{ 1, 2, 3, 4, 5, 6, 7, -1 };
|
||||
for ( int tclass : tclasses )
|
||||
{
|
||||
for ( long key : keys )
|
||||
{
|
||||
OsmTrafficMap.OsmTrafficElement e = trafficMap.getElement( key );
|
||||
while (e != null)
|
||||
{
|
||||
long key2 = e.node2;
|
||||
e = e.next;
|
||||
int trafficClass = trafficMap.getTrafficClass( key, key2 );
|
||||
if ( trafficClass != tclass )
|
||||
continue;
|
||||
|
||||
int[] from = getImagePosition( key );
|
||||
int[] to = getImagePosition( key2 );
|
||||
|
||||
int rgb = 0;
|
||||
if ( trafficClass == -1 ) rgb = 0x0000ff; // blue
|
||||
else if ( trafficClass == 1 ) rgb = 0x404040; // dark grey
|
||||
else if ( trafficClass == 2 ) rgb = 0xa0a0a0; // light grey
|
||||
else if ( trafficClass == 3 ) rgb = 0x00ff00; // green
|
||||
else if ( trafficClass == 4 ) rgb = 0xf4e500; // yellow
|
||||
else if ( trafficClass == 5 ) rgb = 0xf18e1c; // orange
|
||||
else if ( trafficClass == 6 ) rgb = 0xe32322; // red
|
||||
else if ( trafficClass == 7 ) rgb = 0xc0327d; // pink
|
||||
if ( rgb != 0 )
|
||||
{
|
||||
drawLine( from, to, rgb );
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
byte[] png = new Raster2Png().pngEncode( cols, rows, pixels );
|
||||
|
||||
System.out.println( "got png of size: " + png.length );
|
||||
DataOutputStream dos = new DataOutputStream( new BufferedOutputStream( new FileOutputStream( imageFile ) ) );
|
||||
dos.write( png );
|
||||
dos.close();
|
||||
}
|
||||
|
||||
private static void drawLine( int[] from, int[] to, int rgb )
|
||||
{
|
||||
int ix = from[0];
|
||||
int iy = from[1];
|
||||
int ixx = to[0];
|
||||
int iyy = to[1];
|
||||
|
||||
int sx = ixx > ix ? 1 : -1;
|
||||
int sy = iyy > iy ? 1 : -1;
|
||||
|
||||
int dx = ( ixx - ix ) * sx;
|
||||
int dy = ( iyy - iy ) * sy;
|
||||
|
||||
int sum = 0;
|
||||
|
||||
for ( ;; )
|
||||
{
|
||||
drawPixel( ix, iy, rgb );
|
||||
if ( ix == ixx && iy == iyy )
|
||||
break;
|
||||
|
||||
if ( Math.abs( sum + dx ) < Math.abs( sum - dy ) )
|
||||
{
|
||||
iy += sy;
|
||||
sum += dx;
|
||||
}
|
||||
else
|
||||
{
|
||||
ix += sx;
|
||||
sum -= dy;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static void drawPixel( int ix, int iy, int rgb )
|
||||
{
|
||||
if ( ix >= 0 && ix < ncols && iy >= 0 && iy < nrows )
|
||||
{
|
||||
pixels[( nrows - 1 - iy ) * ncols + ix] = rgb;
|
||||
}
|
||||
}
|
||||
|
||||
private static int[] getImagePosition( long key )
|
||||
{
|
||||
int ilon = (int) ( key >> 32 );
|
||||
int ilat = (int) ( key & 0xffffffff );
|
||||
double lonDelta = maxLon - minLon;
|
||||
double latDelta = maxLat - minLat;
|
||||
int[] res = new int[2];
|
||||
res[0] = (int) ( ( ( ilon - minLon ) / lonDelta ) * ncols );
|
||||
res[1] = (int) ( ( ( ilat - minLat ) / latDelta ) * nrows );
|
||||
return res;
|
||||
}
|
||||
}
|
|
@ -1,170 +0,0 @@
|
|||
package btools.util;
|
||||
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.util.zip.CRC32;
|
||||
import java.util.zip.Deflater;
|
||||
import java.util.zip.DeflaterOutputStream;
|
||||
|
||||
public class Raster2Png extends ByteDataWriter
|
||||
{
|
||||
/** Constants for filter (NONE) */
|
||||
public static final int FILTER_NONE = 0;
|
||||
|
||||
/** IHDR tag. */
|
||||
protected static final byte IHDR[] = { 73, 72, 68, 82 };
|
||||
|
||||
/** IDAT tag. */
|
||||
protected static final byte IDAT[] = { 73, 68, 65, 84 };
|
||||
|
||||
/** IEND tag. */
|
||||
protected static final byte IEND[] = { 73, 69, 78, 68 };
|
||||
|
||||
/** geometry */
|
||||
protected int width, height;
|
||||
|
||||
protected int[] imagePixels;
|
||||
|
||||
/** CRC. */
|
||||
protected CRC32 crc = new CRC32();
|
||||
|
||||
public Raster2Png()
|
||||
{
|
||||
super( null );
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts a pixel array to it's PNG equivalent
|
||||
*/
|
||||
public byte[] pngEncode( int width, int height, int[] imagePixels ) throws IOException
|
||||
{
|
||||
this.width = width;
|
||||
this.height = height;
|
||||
this.imagePixels = imagePixels;
|
||||
|
||||
// user a buffer large enough to hold the png
|
||||
ab = new byte[( ( width + 1 ) * height * 3 ) + 200];
|
||||
|
||||
byte[] pngIdBytes =
|
||||
{ -119, 80, 78, 71, 13, 10, 26, 10 };
|
||||
write( pngIdBytes );
|
||||
writeHeader();
|
||||
writeImageData();
|
||||
return toByteArray();
|
||||
}
|
||||
|
||||
/**
|
||||
* Write a PNG "IHDR" chunk into the pngBytes array.
|
||||
*/
|
||||
protected void writeHeader()
|
||||
{
|
||||
writeInt( 13 );
|
||||
int startPos = aboffset;
|
||||
write( IHDR );
|
||||
writeInt( width );
|
||||
writeInt( height );
|
||||
writeByte( 8 ); // bit depth
|
||||
writeByte( 2 ); // direct model
|
||||
writeByte( 0 ); // compression method
|
||||
writeByte( 0 ); // filter method
|
||||
writeByte( 0 ); // no interlace
|
||||
crc.reset();
|
||||
crc.update( ab, startPos, aboffset - startPos );
|
||||
writeInt( (int) crc.getValue() );
|
||||
}
|
||||
|
||||
/**
|
||||
* Write the image data into the pngBytes array. This will write one or more
|
||||
* PNG "IDAT" chunks. In order to conserve memory, this method grabs as many
|
||||
* rows as will fit into 32K bytes, or the whole image; whichever is less.
|
||||
*/
|
||||
protected void writeImageData() throws IOException
|
||||
{
|
||||
int rowsLeft = height; // number of rows remaining to write
|
||||
int startRow = 0; // starting row to process this time through
|
||||
int nRows; // how many rows to grab at a time
|
||||
|
||||
byte[] scanLines; // the scan lines to be compressed
|
||||
int scanPos; // where we are in the scan lines
|
||||
|
||||
byte[] compressedLines; // the resultant compressed lines
|
||||
int nCompressed; // how big is the compressed area?
|
||||
|
||||
int bytesPerPixel = 3;
|
||||
|
||||
Deflater scrunch = new Deflater( 6 );
|
||||
ByteArrayOutputStream outBytes = new ByteArrayOutputStream( 1024 );
|
||||
|
||||
DeflaterOutputStream compBytes = new DeflaterOutputStream( outBytes, scrunch );
|
||||
while (rowsLeft > 0)
|
||||
{
|
||||
nRows = Math.min( 32767 / ( width * ( bytesPerPixel + 1 ) ), rowsLeft );
|
||||
nRows = Math.max( nRows, 1 );
|
||||
|
||||
int[] pixels = new int[width * nRows];
|
||||
|
||||
getPixels( startRow, nRows, pixels );
|
||||
|
||||
/*
|
||||
* Create a data chunk. scanLines adds "nRows" for the filter bytes.
|
||||
*/
|
||||
scanLines = new byte[width * nRows * bytesPerPixel + nRows];
|
||||
|
||||
scanPos = 0;
|
||||
for ( int i = 0; i < width * nRows; i++ )
|
||||
{
|
||||
if ( i % width == 0 )
|
||||
{
|
||||
scanLines[scanPos++] = (byte) FILTER_NONE;
|
||||
}
|
||||
scanLines[scanPos++] = (byte) ( ( pixels[i] >> 16 ) & 0xff );
|
||||
scanLines[scanPos++] = (byte) ( ( pixels[i] >> 8 ) & 0xff );
|
||||
scanLines[scanPos++] = (byte) ( ( pixels[i] ) & 0xff );
|
||||
}
|
||||
|
||||
/*
|
||||
* Write these lines to the output area
|
||||
*/
|
||||
compBytes.write( scanLines, 0, scanPos );
|
||||
|
||||
startRow += nRows;
|
||||
rowsLeft -= nRows;
|
||||
}
|
||||
compBytes.close();
|
||||
|
||||
/*
|
||||
* Write the compressed bytes
|
||||
*/
|
||||
compressedLines = outBytes.toByteArray();
|
||||
nCompressed = compressedLines.length;
|
||||
|
||||
crc.reset();
|
||||
writeInt( nCompressed );
|
||||
write( IDAT );
|
||||
crc.update( IDAT );
|
||||
write( compressedLines );
|
||||
crc.update( compressedLines, 0, nCompressed );
|
||||
|
||||
writeInt( (int) crc.getValue() );
|
||||
scrunch.finish();
|
||||
|
||||
// Write a PNG "IEND" chunk into the pngBytes array.
|
||||
writeInt( 0 );
|
||||
write( IEND );
|
||||
crc.reset();
|
||||
crc.update( IEND );
|
||||
writeInt( (int) crc.getValue() );
|
||||
}
|
||||
|
||||
private void getPixels( int startRow, int nRows, int[] pixels )
|
||||
{
|
||||
for ( int i = 0; i < nRows; i++ )
|
||||
{
|
||||
int ir = i + startRow;
|
||||
for ( int ic = 0; ic < width; ic++ )
|
||||
{
|
||||
pixels[i * width + ic] = imagePixels[ir * width + ic];
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue