Ejemplo n.º 1
0
        /// <summary>
        /// Drawing function for rasterization
        /// </summary>
        /// <param name="filename"></param>
        /// <param name="frame"></param>
        public static void DrawFrame(string filename, Frame frame)
        {
            SxzPoint boundingBox = new SxzPoint();
            //Why did I write this code?
            //if (boundingBox.X == 0 || boundingBox.Y == 0)
            //{
            //    return;
            //}

            frame.EnsureDimensions(boundingBox);

            bool[,] drawable = new bool[boundingBox.X, boundingBox.Y];
            Color transparent = Color.Transparent;

            using (Bitmap drawableSurface = new Bitmap(boundingBox.X, boundingBox.Y, PixelFormat.Format32bppArgb))
            {
                drawableSurface.MakeTransparent(transparent);
                using (Graphics graphics = Graphics.FromImage(drawableSurface))
                {
                    graphics.Clear(transparent);
                    //debug color of green for now
                    SolidBrush brush = new SolidBrush(Color.Green);
                    //graphics.FillRectangle(brush, 0, 0, boundingBox.X, boundingBox.Y);

                    foreach (Chunk chunk in frame.Chunks)
                    {
                        if (chunk.IsPalette())
                        {
                            continue;
                        }

                        if (chunk.IsBackground())
                        {
                            brush.Color = GetColor(chunk.GetColor(0, 0));
                            graphics.FillRectangle(brush, 0, 0, boundingBox.X, boundingBox.Y);
                            continue;
                        }

                        SxzPoint dimensions = chunk.GetDimensions();
                        SxzPoint origin = chunk.Origin;
                        for (int y = origin.Y; y < origin.Y + dimensions.Y; y++)
                        {
                            for (int x = origin.X; x < origin.X + dimensions.X; x++)
                            {
                                SxzColor sxzColor = chunk.GetColor(x, y);

                                if (sxzColor == null)
                                {
                                    //For chunks with bitplanes, this means don't drawn on this pixel even though it is within
                                    //the region of the chunk
                                    continue;
                                }

                                if (chunk.IsTransparent())
                                {
                                    drawableSurface.SetPixel(x, y, transparent);
                                    continue;
                                }

                                Color color = GetColor(sxzColor);
                                if (color == null)
                                {
                                    continue;
                                }

                                drawable[x, y] = true;
                                brush.Color = color;
                                graphics.FillRectangle(brush, x, y, 1, 1);
                            }
                        }
                    }
                }

                Image outputImage = (Image)drawableSurface;
                outputImage.Save(filename, ImageFormat.Png);
            }
        }