Example #1
0
        /// <summary>
        /// Returns bitmap object.
        /// </summary>
        /// <param name="tl"></param>
        /// <param name="path"></param>
        /// <param name="name"></param>
        /// <returns></returns>
        public Bitmap GetTexture(TextureLayout tl, string path = "", string name = "")
        {
            try
            {
                Tim x = GetTimTexture(tl);

                if (x.region.Width <= 0 || x.region.Height <= 0)
                {
                    Helpers.Panic(this, "negative or null size");
                    return(new Bitmap(1, 1));
                }

                using (MemoryStream stream = new MemoryStream(x.SaveBMPToStream(CtrClutToBmpPalette(x.clutdata))))
                {
                    Bitmap oldBmp = (Bitmap)Bitmap.FromStream(stream);
                    Bitmap newBmp = new Bitmap(oldBmp);

                    if (!textures.ContainsKey(tl.Tag()))
                    {
                        textures.Add(tl.Tag(), newBmp);
                    }

                    return(newBmp);
                }
            }
            catch (Exception ex)
            {
                Helpers.Panic(this, "GetTexture fails: " + " " + ex.Message + "\r\n" + ex.ToString() + "\r\n");
                return(null);
            }
        }
Example #2
0
        /// <summary>
        /// Cuts a Tim subtexture from current Tim, based on TextureLayout data.
        /// </summary>
        /// <param name="tl">TextureLayout object.</param>
        /// <returns>Tim object.</returns>
        public Tim GetTimTexture(TextureLayout tl)
        {
            int bpp = 4;

            if (tl.f1 > 0 && tl.f2 > 0 && tl.f3 > 0)
            {
                bpp = 8;
            }

            //Directory.CreateDirectory(path);

            //int width = (tl.width / 4) * 2;
            int width  = (int)(tl.width * (bpp / 8.0f));
            int height = tl.height;

            Console.WriteLine(width + "x" + height);

            ushort[] buf = new ushort[(width / 2) * height];

            //Console.WriteLine(width + "x" + height);

            int ptr = tl.Position * 2; // tl.PageY * 1024 * (1024 * 2 / 16) + tl.frame.Y * 1024 + tl.PageX * (1024 * 2 / 16) + tl.frame.X;

            for (int i = 0; i < height; i++)
            {
                Buffer.BlockCopy(
                    this.data, ptr,
                    buf, i * width,
                    width);

                ptr += CtrVrm.Width * 2;
            }


            Tim x = new Tim(tl.frame);

            x.data = buf;

            x.region = new Rectangle(tl.RealX, tl.RealY, tl.width / 4, tl.height);

            x.clutregion = new Rectangle(tl.PalX * 16, tl.PalY, 16, 1);
            x.clutdata   = GetCtrClut(tl);
            x.clutsize   = (uint)(x.clutregion.Width * 2 + 12);
            x.flags      = 8; //4 bit + pal = 8

            /*
             * Rectangle r = x.clutregion;
             *
             * if (r.Width % 4 != 0)
             *  r.Width += 16;
             *
             * Tim x2 = new Tim(r);
             * x2.DrawTim(x);
             */

            //Console.WriteLine(x.clutdata.Length);

            return(x);
        }
Example #3
0
        /// <summary>
        /// Returns PS1 palette (CLUT) for corresponding texture layout.
        /// </summary>
        /// <param name="tl">Texture layout data.</param>
        public ushort[] GetCtrClut(TextureLayout tl)
        {
            ushort[] buf = new ushort[16];

            int ptr = tl.PalPosition * 2;

            Buffer.BlockCopy(
                this.data, ptr,
                buf, 0,
                16 * 2);

            return(buf);
        }
Example #4
0
        public void GetTexture(TextureLayout tl, string path, string name = "")
        {
            try
            {
                Directory.CreateDirectory(path);

                int width  = (tl.width / 4) * 2;
                int height = tl.height;

                ushort[] buf = new ushort[(width / 2) * height];

                //Console.WriteLine(width + "x" + height);

                int ptr = tl.Position * 2; // tl.PageY * 1024 * (1024 * 2 / 16) + tl.frame.Y * 1024 + tl.PageX * (1024 * 2 / 16) + tl.frame.X;

                for (int i = 0; i < height; i++)
                {
                    Buffer.BlockCopy(
                        this.data, ptr,
                        buf, i * width,
                        width);

                    ptr += CtrVrm.Width * 2;
                }


                Tim x = new Tim(tl.frame);

                x.data = buf;

                x.region = new Rectangle(tl.RealX, tl.RealY, tl.width / 4, tl.height);

                x.clutregion = new Rectangle(tl.PalX * 16, tl.PalY, 16, 1);
                x.clutdata   = GetCtrClut(tl);
                x.clutsize   = (uint)(x.clutregion.Width * 2 + 12);
                x.flags      = 8; //4 bit + pal = 8

                /*
                 * Rectangle r = x.clutregion;
                 *
                 * if (r.Width % 4 != 0)
                 *  r.Width += 16;
                 *
                 * Tim x2 = new Tim(r);
                 * x2.DrawTim(x);
                 */

                //Console.WriteLine(x.clutdata.Length);

                if (x.region.Width > 0 && x.region.Height > 0)
                {
                    string n = path + "\\" + (name == "" ? tl.Tag() : name);

                    //if (!File.Exists(n + ".tim"))
                    x.Write(n + ".tim");

                    //if (!File.Exists(n + ".bmp"))
                    x.SaveBMP(n + ".bmp", CtrClutToBmpPalette(x.clutdata));

                    //if (!File.Exists(n + ".png"))
                    using (Bitmap oldBmp = new Bitmap(n + ".bmp"))
                        using (Bitmap newBmp = new Bitmap(oldBmp))
                        {
                            newBmp.Save(n + ".png", System.Drawing.Imaging.ImageFormat.Png);
                        }
                }
                else
                {
                    throw new Exception("negative or null size");
                    Console.WriteLine("failed!" + tl.ToString());
                    Console.ReadKey();
                }
            }
            catch (Exception ex)
            {
                Helpers.Panic(this, "GetTexture fails: " + path + " " + ex.Message + "\r\n" + ex.ToString() + "\r\n");
            }
        }