コード例 #1
0
        public void Save(string uri, int lod)
        {
            lod = lod.Clamp(0, NumLods - 1);
            HdrBuffer image = images [lod];

            image.Save(uri);
        }
コード例 #2
0
        public static HdrBuffer Load(string uri)
        {
            BitmapImage bi = new BitmapImage(new Uri(uri, UriKind.RelativeOrAbsolute));

            //PixelFormat fmt = bi.Format;	// FIXIT: handle multiple formats
            int [,] pixels = new int [bi.PixelHeight, bi.PixelWidth];

            unsafe
            {
                fixed(int *p = pixels)
                {
                    IntPtr ip = new IntPtr(p);

                    bi.CopyPixels(Int32Rect.Empty, ip, bi.PixelWidth * bi.PixelHeight * 4, bi.PixelWidth * 4);
                }
            }

            HdrBuffer image = new HdrBuffer(bi.PixelWidth, bi.PixelHeight);

            for (int y = 0; y < bi.PixelHeight; y++)
            {
                for (int x = 0; x < bi.PixelWidth; x++)
                {
                    int intC = pixels [y, x];

                    double3 c = new double3(
                        ( double )((intC & 0x00ff0000) >> 16) / 255,
                        ( double )((intC & 0x0000ff00) >> 8) / 255,
                        ( double )(intC & 0x000000ff) / 255
                        );

                    image.Values [x, y] = c;
                }
            }

            return(image);
        }
コード例 #3
0
        public override void LoadMipmaps()
        {
            if (mipsLoaded)
            {
                return;
            }

            int       numLods   = ( int )Math.Log(Math.Min(Width, Height), 2) + 1;
            HdrBuffer baseImage = images [0];

            images     = new HdrBuffer [numLods];
            images [0] = baseImage;

            string cacheDir = @"..\..\Cache";

            if (!Directory.Exists(cacheDir))
            {
                cacheDir = @"Cache";
            }

            string mipDir = string.Format(@"{0}\Mipmaps\{1}", cacheDir, Name);

            if (Directory.Exists(mipDir))
            {
                for (int lod = 1; lod < NumLods; lod++)
                {
                    string mipFileName = string.Format(@"{0}\{1}.bmp", mipDir, lod);
                    bool   loaded      = false;

                    if (File.Exists(mipFileName))
                    {
                        loaded = true;

                        try {
                            Load(mipFileName, lod);
                        } catch (Exception ex) {
                            Console.WriteLine("Error while loading texture \"{0}\" miplevel {1}: {2}",
                                              Name, lod, ex.Message);

                            loaded = false;
                        }
                    }

                    if (!loaded)
                    {
                        BuildMipmap(lod);
                        Save(mipFileName, lod);
                    }
                }
            }
            else
            {
                Directory.CreateDirectory(mipDir);

                for (int lod = 1; lod < NumLods; lod++)
                {
                    string mipFileName = string.Format(@"{0}\{1}.bmp", mipDir, lod);

                    BuildMipmap(lod);
                    Save(mipFileName, lod);
                }
            }

            mipsLoaded = true;
        }
コード例 #4
0
 public void Load(string uri, int lod)
 {
     images [lod] = HdrBuffer.Load(uri);
 }
コード例 #5
0
        private void BuildMipmap(int lod)
        {
            int hiLod = lod - 1;
            int w     = images [hiLod].Size.width,
                h     = images [hiLod].Size.height;

            double dx = 0, dy = 0;

            if (w % 2 != 0)
            {
                w /= 2;
                dx = 1.0 / w;
            }
            else
            {
                w /= 2;
            }

            if (h % 2 != 0)
            {
                h /= 2;
                dy = 1.0 / h;
            }
            else
            {
                h /= 2;
            }

            double wQuotInv = 1 / (2 + dx),
                   hQuotInv = 1 / (2 + dy);

            HdrBuffer hiMip  = images [hiLod];
            HdrBuffer lowMip = new HdrBuffer(w, h);

            double ky0 = 1, ky2 = dy;

            for (int ly = 0, hy = 0;
                 ly < h;
                 ly++, hy += 2, ky0 -= dy, ky2 += dy)
            {
                double kx0 = 1, kx2 = dx;

                for (int lx = 0, hx = 0;
                     lx < w;
                     lx++, hx += 2, kx0 -= dx, kx2 += dx)
                {
                    int hx1 = hx + 1, hx2 = hx + 2,
                        hy1 = hy + 1, hy2 = hy + 2;
                    double3 c00 = hiMip.Values [hx, hy] * kx0;
                    double3 c10 = hiMip.Values [hx1, hy];
                    double3 c20 = kx2 != 0 ? hiMip.Values [hx2, hy] * kx2 : double3.Zero;

                    double3 c01 = hiMip.Values [hx, hy1] * kx0;
                    double3 c11 = hiMip.Values [hx1, hy1];
                    double3 c21 = kx2 != 0 ? hiMip.Values [hx2, hy1] * kx2 : double3.Zero;

                    double3 c02 = ky2 != 0 ? hiMip.Values [hx, hy2] * kx0 : double3.Zero;
                    double3 c12 = ky2 != 0 ? hiMip.Values [hx1, hy2] : double3.Zero;
                    double3 c22 = ky2 != 0 && kx2 != 0 ? hiMip.Values [hx2, hy2] * kx2 : double3.Zero;

                    lowMip.Values [lx, ly] = (
                        (c00 + c10 + c20) * ky0 +
                        (c01 + c11 + c21) +
                        (c02 + c12 + c22) * ky2
                        ) * wQuotInv * hQuotInv;
                }
            }

            //for ( int ly = 0, hy = 0 ; ly < h ; ly++, hy += 2 ) {
            //    for ( int lx = 0, hx = 0 ; lx < w ; lx++, hx += 2 ) {
            //        int hx1 = hx + 1;
            //        int hy1 = hy + 1;

            //        lowMip.Values [lx, ly] = (
            //            hiMip.Values [hx, hy ] + hiMip.Values [hx1, hy ] +
            //            hiMip.Values [hx, hy1] + hiMip.Values [hx1, hy1]
            //            ) * 0.25;
            //    }
            //}

            images [lod] = lowMip;
        }