Пример #1
0
        public LuaArgs write(LuaArgs args)
        {
            int x = args.GetInt(0);
            int y = args.GetInt(1);

            if (args.IsNumber(2))
            {
                var n = args.GetByte(2);
                CheckWritable();
                if (x >= 0 && x < Image.Width &&
                    y >= 0 && y < Image.Height)
                {
                    Image[x, y] = n;
                }
                return(LuaArgs.Empty);
            }
            else
            {
                var bytes = args.GetByteString(2);
                if (x < 0 || x >= Image.Width ||
                    y < 0 || y >= Image.Height ||
                    (x + y * Image.Width + bytes.Length) > Image.Width * Image.Height)
                {
                    throw new LuaError("Write must start and end within the image bounds");
                }
                CheckWritable();
                Image.Write(bytes, 0, bytes.Length, x, y);
                return(LuaArgs.Empty);
            }
        }
Пример #2
0
        public LuaArgs loadTGA(LuaArgs args)
        {
            try
            {
                // Load image
                Image   image;
                Palette palette;
                if (args.IsString(0))
                {
                    // Load from a string
                    var bytes = args.GetByteString(0);
                    using (var stream = new MemoryStream(bytes))
                    {
                        image = TGAImage.Decode(stream, m_computer.Memory, out palette);
                    }
                }
                else
                {
                    // Load from a stream
                    var file = args.GetObject <LuaFile>(0);
                    CheckFileMode(file, "tga", LuaFileOpenMode.Read, LuaFileContentType.Binary);
                    image = TGAImage.Decode(file.InnerStream, m_computer.Memory, out palette);
                }

                // Return image
                return(new LuaArgs(
                           new LuaImage(image, m_computer.Memory),
                           new LuaPalette(palette, m_computer.Memory)
                           ));
            }
            catch (OutOfMemoryException)
            {
                throw new LuaError("not enough memory");
            }
            catch (IOException e)
            {
                throw new LuaError(e.Message);
            }
        }
Пример #3
0
 public LuaArgs write(LuaArgs args)
 {
     if (args.IsNumber(1))
     {
         int start = args.GetInt(0);
         var b     = args.GetByte(1);
         if (start >= 0 && start < Buffer.Length)
         {
             Buffer[start] = b;
         }
         return(LuaArgs.Empty);
     }
     else
     {
         int start = args.GetInt(0);
         var bytes = args.GetByteString(1);
         if (start < 0 || start + bytes.Length > Buffer.Length)
         {
             throw new LuaError("Write must start and end within the buffer");
         }
         Buffer.Write(start, bytes);
         return(LuaArgs.Empty);
     }
 }