示例#1
0
 public static void CompressDxt1(byte *rgb, int top, int left, int bottom, int right, int stride, byte *blocks, int option)
 {
     if (useSse2)
     {
         option |= 2;
     }
     ZunTzuLib.CompressDxt1(rgb, top, left, bottom, right, stride, blocks, option);
 }
示例#2
0
文件: DXTileSet.cs 项目: jimu/ZunTzu
        private static unsafe int loadNextTexture(IntPtr imageLoader, Texture texture, out uint mipmapLevel, out uint x, out uint y)
        {
            byte *textureBits = (byte *)texture.LockRectangle(0, LockFlags.None).InternalData.ToPointer();

            return(ZunTzuLib.LoadNextTile(imageLoader, (IntPtr)textureBits, out mipmapLevel, out x, out y));
        }
示例#3
0
文件: DXTileSet.cs 项目: jimu/ZunTzu
        /// <summary>Must be called in a loop for the tile set to be fully loaded.</summary>
        /// <returns>Progress between 0 and 1.</returns>
        public IEnumerable <float> LoadIncrementally()
        {
            if (imageFile.Archive == null ||
                (!imageFile.FileName.EndsWith(".jpg", StringComparison.OrdinalIgnoreCase) && !imageFile.FileName.EndsWith(".png", StringComparison.OrdinalIgnoreCase)) ||
                (maskFile != null && !maskFile.FileName.EndsWith(".jpg", StringComparison.OrdinalIgnoreCase) && !maskFile.FileName.EndsWith(".png", StringComparison.OrdinalIgnoreCase)))
            {
                goto fallBack;
            }

            // optimized native code simd-ed multithreaded pipeline
            {
                int  error;
                uint skippedMipMapLevels = (uint)Math.Max(0, (int)graphics.Properties.MapsAndCountersDetailLevel - (int)detailLevel);

                IntPtr imageLoader = ZunTzuLib.CreateImageLoader(imageFile.Archive.FileName, imageFile.FileName, (maskFile != null ? maskFile.FileName : ""), skippedMipMapLevels, 1);
                try {
                    uint width;
                    uint height;
                    if (0 != (error = ZunTzuLib.GetImageDimensions(imageLoader, out width, out height)))
                    {
                        //throw new ApplicationException(string.Format("Error while loading image: code {0}", error));
                        goto fallBack;
                    }

                    size = new SizeF((float)width, (float)height);
                    for (int i = (int)detailLevel; i > 0; --i)
                    {
                        size = new SizeF(size.Width * 2, size.Height * 2);
                    }

                    int mipMapLevelCount =
                        Math.Max(
                            3,                                  // at least 3 mip-maps
                            Math.Max(
                                (int)Math.Ceiling(Math.Log(width / 254, 2) + 1),
                                (int)Math.Ceiling(Math.Log(height / 254, 2) + 1)));
                    tiles = new DXTile[mipMapLevelCount + (int)detailLevel][, ];

                    uint tileCount = 0;
                    for (int mipMapLevel = (int)detailLevel; mipMapLevel < mipMapLevelCount + (int)detailLevel; ++mipMapLevel)
                    {
                        if (mipMapLevel >= (int)graphics.Properties.MapsAndCountersDetailLevel)
                        {
                            uint columnCount = (width + 253) / 254;
                            uint rowCount    = (height + 253) / 254;
                            tiles[mipMapLevel] = new DXTile[columnCount, rowCount];
                            tileCount         += columnCount * rowCount;
                        }
                        width  = (width + 1) / 2;
                        height = (height + 1) / 2;
                    }

                    uint mipmapLevel;                           // regardless of detailLevel (i.e. first mipmap is always zero)
                    uint x;
                    uint y;
                    for (uint i = 0; i < tileCount; ++i)
                    {
                        Texture texture = new Texture(graphics.Device, 256, 256, 1, 0, (maskFile == null ? Format.Dxt1 : Format.Dxt5), Pool.Managed);
                        if (0 != (error = loadNextTexture(imageLoader, texture, out mipmapLevel, out x, out y)))
                        {
                            texture.UnlockRectangle(0);
                            //throw new ApplicationException(string.Format("Error while loading image: code {0}", error));
                            goto fallBack;
                        }
                        texture.UnlockRectangle(0);

                        DXTile tile = new DXTile(this);
                        tiles[mipmapLevel + (int)detailLevel][x, y] = tile;
                        tile.Initialize(maskFile == null ? Format.Dxt1 : Format.Dxt5, texture);

                        yield return((float)(i + 1) / (float)tileCount);
                    }
                } finally {
                    ZunTzuLib.FreeImageLoader(imageLoader);
                }
                yield break;
            }

fallBack:
            {
                BitmapResource image = null;
                try {
                    if (maskFile != null)
                    {
                        image = new BitmapResource(imageFile, PixelFormat.Format32bppArgb);
                        BitmapResource mask = null;
                        try {
                            mask = new BitmapResource(maskFile, PixelFormat.Format24bppRgb);
                            applyMask(image, mask);
                        } finally {
                            if (mask != null)
                            {
                                mask.Dispose();
                            }
                        }
                    }
                    else
                    {
                        image = new BitmapResource(imageFile, PixelFormat.Format24bppRgb);
                    }

                    // TODO: find the correct progress for loading the bitmaps
                    yield return(0.0f);

                    size = new SizeF((float)image.BitmapData.Width, (float)image.BitmapData.Height);
                    for (int i = (int)detailLevel; i > 0; --i)
                    {
                        size = new SizeF(size.Width * 2, size.Height * 2);
                    }

                    foreach (float progress in createMipMappedTilesIncrements(image))
                    {
                        yield return(progress);
                    }
                } finally {
                    if (image != null)
                    {
                        image.Dispose();
                    }
                }
            }
        }