Ejemplo n.º 1
0
        public static IDMap Build( Lightmap map )
        {
            // Should be a zero zoom lightmap
            if ( map.Zoom != 0 ) throw new InvalidZoomFactorException( map.Zoom );

            // We build the map in 2048/2048 blocks
            IDMap result = new IDMap();
            IShader32 shader = new ImportIDShader();
            bool vd = map.VolatileDecompression;
            try {
                map.VolatileDecompression = true;
                for ( int y=0; y<Lightmap.BaseHeight; y+=BuildBlockSize ) {
                    int height = (y+BuildBlockSize > Lightmap.BaseHeight) ? (Lightmap.BaseHeight-y) : BuildBlockSize;

                    for ( int x=0; x<Lightmap.BaseWidth; x+=BuildBlockSize ) {
                        int width = (x+BuildBlockSize > Lightmap.BaseWidth) ? (Lightmap.BaseWidth-x) : BuildBlockSize;

                        // Get the bitmap from the lightmap
                        // And import it into ourselves
                        result.ImportBitmapBuffer( x, y, width, height, map.DecodeImage( new Rectangle( x, y, width, height ), shader ) );
                    }
                }
            }
            finally {
                map.VolatileDecompression = vd;
            }

            return result;
        }
Ejemplo n.º 2
0
        public void Shrink( out Lightmap result1, out Lightmap result2 )
        {
            result1 = new Lightmap( zoom+1, provinces, adjacent, idmap );
            result2 = new Lightmap( zoom+2, provinces, adjacent, idmap );

            Size size = this.CoordMap.ActualToZoomedBlocks( Lightmap.BaseSize );
            //Size size1 = result1.CoordMap.ActualToZoomedBlocks( Lightmap.BaseSize );
            //Size size2 = result2.CoordMap.ActualToZoomedBlocks( Lightmap.BaseSize );
            int stride = CoordMap.ActualToZoomedBlocks( Lightmap.BaseWidth );
            MapBlock[,] matrix = new MapBlock[4,4];
            for ( int blockindex1 = 0, blockindex2 = 0, y=0; y<size.Height; y+=2 ) {
                for ( int x=0; x<size.Width; x+=2 ) {
                    // Get 4 blocks
                    int index = y*stride+x;

                    result1.blocks[blockindex1++] = MapBlock.Combine(
                        GetDecompressedBlock( index+stride+1 ),		// bottomright
                        GetDecompressedBlock( index+stride ),		// bottomleft
                        GetDecompressedBlock( index+1 ),			// topright
                        GetDecompressedBlock( index )   			// topleft
                        ).Compress( (x << BlockFactor) << zoom, (y << BlockFactor) << zoom, zoom+1, provinces, adjacent, idmap );

                    if ( (x % 4 == 0) && (y % 4 == 0) ) {
                        matrix[3,3] = GetDecompressedBlock( index+(stride*3)+3 );
                        matrix[2,3] = GetDecompressedBlock( index+(stride*3)+2 );
                        matrix[1,3] = GetDecompressedBlock( index+(stride*3)+1 );
                        matrix[0,3] = GetDecompressedBlock( index+(stride*3) );
                        matrix[3,2] = GetDecompressedBlock( index+(stride*2)+3 );
                        matrix[2,2] = GetDecompressedBlock( index+(stride*2)+2 );
                        matrix[1,2] = GetDecompressedBlock( index+(stride*2)+1 );
                        matrix[0,2] = GetDecompressedBlock( index+(stride*2) );
                        matrix[3,1] = GetDecompressedBlock( index+stride+3 );
                        matrix[2,1] = GetDecompressedBlock( index+stride+2 );
                        matrix[1,1] = GetDecompressedBlock( index+stride+1 );
                        matrix[0,1] = GetDecompressedBlock( index+stride );
                        matrix[3,0] = GetDecompressedBlock( index+3 );
                        matrix[2,0] = GetDecompressedBlock( index+2 );
                        matrix[1,0] = GetDecompressedBlock( index+1 );
                        matrix[0,0] = GetDecompressedBlock( index );

                        result2.blocks[blockindex2++] = MapBlock.Combine( matrix )
                            .Compress( (x << BlockFactor) << (zoom), (y << BlockFactor) << (zoom), zoom+2, provinces, adjacent, idmap );
                    }
                }
            }
        }
Ejemplo n.º 3
0
        public Lightmap Shrink()
        {
            Lightmap result = new Lightmap( zoom+1, provinces, adjacent, idmap );

            Size size = result.CoordMap.ActualToZoomedBlocks( Lightmap.BaseSize );
            #if true
            int stride = CoordMap.ActualToZoomedBlocks( Lightmap.BaseWidth );
            for ( int blockindex = 0, y=0; y<size.Height; ++y ) {
                for ( int x=0; x<size.Width; ++x ) {
                    // Get 4 blocks
                    int index = (y<<1)*stride+(x<<1);

                    result.blocks[blockindex++] = MapBlock.Combine(
                        GetDecompressedBlock( index+stride+1 ),		// bottomright
                        GetDecompressedBlock( index+stride ),		// bottomleft
                        GetDecompressedBlock( index+1 ),			// topright
                        GetDecompressedBlock( index )   			// topleft
                    ).Compress( (x << BlockFactor) << (zoom), (y << BlockFactor) << (zoom), zoom+1, provinces, adjacent, idmap );
                }
            }
            #else
            // Turn on volatile decompression, so that we don't hog memory...
            bool remembercompression = volatiledecompression;
            volatiledecompression = true;

            for ( int y=size.Height-1; y>=0; --y ) {
                for ( int x=size.Width-1; x>=0; --x ) {
                    result.EncodeImage( DecodeImage( new Rectangle(  x<<(BlockFactor+1), y<<(BlockFactor+1), BlockSize << 1, BlockSize << 1 ) ).Shrink() );
                    //RawImage raw = DecodeImage( new Rectangle( x<<(BlockFactor+1), y<<(BlockFactor+1), BlockSize << 1, BlockSize << 1 ) );
                    //raw = raw.Shrink();
                    //result.EncodeImage( raw );
                }
                GC.Collect();
            }

            // Put previous decompression mode back
            volatiledecompression = remembercompression;
            #endif

            return result;
        }
Ejemplo n.º 4
0
 public static Lightmap CreateEmpty( int zoom, ProvinceList provinces, AdjacencyTable adjacent, IDMap idmap )
 {
     Lightmap result = new Lightmap( zoom, provinces, adjacent, idmap );
     for ( int i=0; i<result.blocks.Length; ++i ) result.blocks[i] = new MapBlock();
     return result;
 }
Ejemplo n.º 5
0
        public static Lightmap CreateEmpty( int zoom )
        {
            Lightmap result = new Lightmap( zoom, null, null, null );

            CompressedBlock block = new MapBlock().Compress( 0, 0, zoom, null, null, null );
            for ( int i=0; i<result.blocks.Length; ++i ) result.blocks[i] = block;
            return result;
        }
Ejemplo n.º 6
0
        private static void DoExport( Lightmap map, Rectangle region, PngLevel pngLevel, IDMap idmap, string sourcefile, string target )
        {
            Console.WriteLine( "Decoding image..." );
            RawImage rawimg = map.DecodeImage( region );
            int[] shadebuffer, idbuffer, borderbuffer;
            new IOShader( Boot.DefaultConvertor ).MultiShade32( rawimg, out shadebuffer, out idbuffer, out borderbuffer );

            // We don't use the lightmap version as a source: this can lead to errors.
            // Use the idmap as source. For lightmap2 and lightmap3, this needs to be scaled down.
            // -- NOTE: if this is changed, don't forget to turn on the id converting in the IOShader.Multishade above!
            ushort[] tmpbuffer = null;

            if ( map.Zoom == 0 ) {
                tmpbuffer = idmap.ExportBitmapBuffer( rawimg.Bounds );
            }
            else {
                Console.WriteLine( "Scaling idmap..." );
                tmpbuffer = MapToolsLib.Utils.ScaleIDBuffer( idmap.ExportBitmapGrid( map.CoordMap.ZoomedToActual( rawimg.Bounds ) ), map.Zoom );
            }

            // Convert it to the map format
            for ( int i=0; i<idbuffer.Length; ++i ) {
                if ( tmpbuffer[i] >= Province.Count ) tmpbuffer[i] = Province.TerraIncognitaID;
                idbuffer[i] = Boot.DefaultConvertor.ConvertID( tmpbuffer[i] );
            }

            if ( pngLevel != PngLevel.None ) {
                string basetarget = Path.ChangeExtension( Path.GetFileNameWithoutExtension( target ) + "-###", Path.GetExtension( target ) );

                if ((pngLevel & PngLevel.Shading) > 0) {
                    target = basetarget.Replace("###", Boot.ShadingLayerName);
                    Console.WriteLine("Exporting to \"{0}\"...", Path.GetFileName(target));
                    Visualiser.CreateImage32(shadebuffer, rawimg.Size).Save(target, ImageFormat.Png);
                }

                if ((pngLevel & PngLevel.IDs) > 0) {
                    target = basetarget.Replace("###", Boot.IDLayerName);
                    Console.WriteLine("Exporting to \"{0}\"...", Path.GetFileName(target));
                    Visualiser.CreateImage32(idbuffer, rawimg.Size).Save(target, ImageFormat.Png);
                }

                if ((pngLevel & PngLevel.Borders) > 0) {
                    target = basetarget.Replace("###", Boot.BorderLayerName);
                    Console.WriteLine("Exporting to \"{0}\"...", Path.GetFileName(target));
                    Visualiser.CreateImage32(borderbuffer, rawimg.Size).Save(target, ImageFormat.Png);
                }
            }
            else {
                Console.WriteLine( "Exporting to \"{0}\"...", Path.GetFileName( target ) );

                PSD.File psd = new PSD.File( rawimg.Size );

                Bitmap img = new MapInfo( map.Zoom, rawimg.Location, rawimg.Size, sourcefile, Path.GetFileName( target ), Boot.DefaultConvertor ).AsBitmap();
                PSD.Layer l = psd.Layers.Add( new PSD.Layer( Boot.MapInfoLayerName, 0, 0, img ) );
                l.Opacity = 255;
                l.Visible = false;
                l.ProtectTransparancy = true;

                img = Visualiser.CreateImage32( shadebuffer, rawimg.Size );
                l = psd.Layers.Add( new PSD.Layer( Boot.ShadingLayerName, 0, 0, img ) );
                l.Opacity = 254;
                l.Visible = true;
                l.ProtectTransparancy = false;

                img = Visualiser.CreateImage32( idbuffer, rawimg.Size );

                l = psd.Layers.Add( new PSD.Layer( Boot.IDLayerName, 0, 0, img ) );
                l.Opacity = 254;
                l.Mode = PSD.LayerMode.Overlay;
                l.Visible = true;
                l.ProtectTransparancy = false;

                img = Visualiser.CreateImage32( borderbuffer, rawimg.Size, true );
                l = psd.Layers.Add( new PSD.Layer( Boot.BorderLayerName, 0, 0, img ) );
                l.Opacity = 254;
                l.Visible = true;
                l.ProtectTransparancy = false;

                // -- Write the psd
                FileStream stream = null;
                try {
                    stream = new FileStream( target, FileMode.Create, FileAccess.Write, FileShare.None );
                    psd.WriteTo( new BinaryWriter( stream ) );
                }
                finally {
                    if ( stream != null ) stream.Close();
                }
            }
        }