private void buildPriorityCanvas(int priorityNr)
        {
            int minX = 160;
              int maxX = 0;
              int minY = 168;
              int maxY = 0;
              int width = 0;
              int height = 0;
              bool colorUsed = false;

              // first pass, detect width and height
              for (int y=0; y<168; y++)
              {
            for (int x=0; x<160; x++)
            {
              bool drawPixel = (priorityCanvas.GetPixel(x, y) == priorityNr);
              if (drawPixel)
              {
            colorUsed = true;
            minX = Math.Min(minX, x);
            maxX = Math.Max(maxX, x);
            minY = Math.Min(minY, y);
            maxY = Math.Max(maxY, y);
              }
            }
              }

              // break if the color is actually not used (could happen for color 4)
              if (!colorUsed)
              {
            prioritiesUsed[priorityNr] = false;
            return;
              }
              width = maxX - minX + 1;
              height = maxY - minY + 1;

              Canvas canvas = new Canvas(width * zoomX, height * zoomY);
              for (int i=0; i<16; i++)
              {
            canvas.SetColor(ColorPalette[i]);
              }
              canvas.FillBackground(4);
              canvas.SetTransparentColor(4);
              for (int y=0; y<height; y++)
              {
            for (int x=0; x<width; x++)
            {
              bool drawPixel = (priorityCanvas.GetPixel(x + minX, y + minY) == priorityNr);
              if (drawPixel)
              {
            int color = pictureCanvas.GetPixel(x + minX, y + minY);

            // draw pixels in zoomed fashion
            for (int zY = y * zoomY; zY < (y + 1) * zoomY; zY++)
            {
              for (int zX = x * zoomX; zX < (x + 1) * zoomX; zX++)
              {
                canvas.SetPixel(zX, zY, color);
              }
            }
              }
            }
              }
              priorityCanvases.Push(canvas);

              // add layers information to goaDoc
              XmlElement layerNode = goaDoc.CreateElement("layer");
              layerNode.SetAttribute("priority", "" + priorityNr);
              layerNode.SetAttribute("width", "" + width);
              layerNode.SetAttribute("height", "" + height);
              layerNode.SetAttribute("left", "" + minX);
              layerNode.SetAttribute("top", "" + minY);
              layersNode.AppendChild(layerNode);
        }
        private void renderCanvas()
        {
            XmlElement viewNode = doc.CreateElement("view");
              viewNode.SetAttribute("id", "" + id);
              doc.AppendChild(viewNode);

              if (Description != "")
              {
            XmlElement descEl = doc.CreateElement("description");
            viewNode.AppendChild(descEl);
            descEl.AppendChild(doc.CreateTextNode(Description));
              }

              canvas = new Canvas(viewWidth * zoomX, viewHeight * zoomY);
              for (int i = 0; i < colorPalette.Length; i++)
              {
            canvas.SetColor(colorPalette[i]);
              }

              int celY = 0;
              Loop loop;

              for (int loopNr = 0; loopNr < loopCount; loopNr++)
              {
            int celX = 0;

            loop = loops[loopNr];
            int celCount = loop.cels.Length;

            XmlElement loopNode = doc.CreateElement("loop");
            viewNode.AppendChild(loopNode);

            for (int celNr = 0; celNr < celCount; celNr++)
            {
              Cel cel = loop.cels[celNr];

              XmlElement celNode = doc.CreateElement("cel");
              loopNode.AppendChild(celNode);
              celNode.SetAttribute("width", "" + cel.width);
              celNode.SetAttribute("height", "" + cel.height);

              for (int y = 0; y < cel.height; y++)
              {
            for (int x = 0; x < cel.width; x++)
            {
              int colorIndex = cel.data[x, y];

              //canvas.SetPixel(celX + x, celY + y, colorIndex);
              int vX = celX + x;
              int vY = celY + y;

              // draw pixels in zoomed fashion
              for (int zY = vY * zoomY; zY < (vY + 1) * zoomY; zY++)
              {
                for (int zX = vX * zoomX; zX < (vX + 1) * zoomX; zX++)
                {
                  canvas.SetPixel(zX, zY, colorIndex);
                }
              }
            }
              }
              celX += cel.width;
            }
            celY += loop.height;
              }

              canvas.SetTransparentColor(16);
        }
        public void InitValues()
        {
            pictureCanvas               = new Canvas(160, 168);
              priorityCanvas              = new Canvas(160, 168);
              controlCanvas               = new Canvas(160, 168);
              pictureDoc                  = new XmlDocument();
              goaDoc                      = new XmlDocument();
              prioritiesUsed              = new bool[16];
              priorityCanvases            = new Stack();
              pictureColor                = 15;
              priorityColor               = 4;
              pictureDrawingEnabled       = false;
              priorityDrawingEnabled      = false;
              isGoaCommand                = false;

              for (int i=0; i<16; i++)
              {
            pictureCanvas.SetColor(ColorPalette[i]);
            priorityCanvas.SetColor(ColorPalette[i]);
            controlCanvas.SetColor(ColorPalette[i]);
            prioritiesUsed[i] = false;
              }

              prioritiesUsed[4] = true;

              pictureCanvas.FillBackground(15);
              priorityCanvas.FillBackground(4);
              controlCanvas.FillBackground(4);
        }