-- $Date: 2004/03/04 05:05:42 $
-- $Revision: 1.6 $
-- $Author: jcrocholl $

with Messages; use Messages;
with Integer_Strings; use Integer_Strings;

package body Glyph_Images is

   procedure Bounds_To_Pixels
     (Bounds : in Rectangle-- Convert this rectangle.
      Zoom   : in Vector;    -- Zoom factors.
      SX, SY : out Integer;  -- Raw integer size.
      Size   : out Vector;   -- Size of image in pixels.
      Center : out Vector)   -- Center pixel coordinates.
   is
      TL : Real_Vectors.Vector := Get_Top_Left(Bounds);
      BR : Real_Vectors.Vector := Get_Bottom_Right(Bounds);
      CX : Integer := 1 + Greater_Or_Equal(-Zoom.X * TL.X - 0.5);
      CY : Integer := 1 + Greater_Or_Equal(-Zoom.Y * TL.Y - 0.5);
   begin
      SX := CX + Greater_Or_Equal(Zoom.X * BR.X - 0.5);
      SY := CY + Greater_Or_Equal(Zoom.Y * BR.Y - 0.5);
      Center := (Real(CX), Real(CY));
      -- Debug("center " & To_String(Center));
      Size := (Real(SX), Real(SY));
      -- Debug("size " & To_String(Size));
   end Bounds_To_Pixels;

   function Bounds_To_Image
     (Bounds       : in Real_Vectors.Rectangle-- Convert this rectangle.
      Staff_Height : in Real;                   -- General zoom factor.
      Aspect_Ratio : in Real := 1.0)            -- Horizontal extra zoom factor.
     return Glyph_Image_Access                  -- The resulting glyph image.
   is
      SX, SY : Integer;
      Size   : Vector;             -- Size of image in pixels.
      Center : Vector;             -- Center pixel coordinates.
      Result : Glyph_Image_Access-- Result image instance.
      Zoom   : Real := Staff_Height / 400.0;
   begin
      Bounds_To_Pixels(Bounds, (Zoom * Aspect_Ratio, Zoom), SX, SY, Size, Center);
      Result := new Glyph_Image(SX, SY);
      Result.Staff_Height := Staff_Height;
      Result.Aspect_Ratio := Aspect_Ratio;
      Result.Center := Center;
      return Result;
   end Bounds_To_Image;

   procedure Black_Box
     (This   : access Glyph_Image;
      X1, X2 : in Real;
      Y1, Y2 : in Real) is
   begin
      if Y1 > Y2 then
         Black_Box(This, X1, X2, Y2, Y1);
         return;
      end if;

      if X1 > X2 then
         Black_Box(This, X2, X1, Y1, Y2);
         return;
      end if;

      for Integer_Y in
        Integer(This.Center.Y + Y1 * This.Staff_Height / 400.0) ..
        Integer(This.Center.Y + Y2 * This.Staff_Height / 400.0)
      loop
         for Integer_X in
           Integer(This.Center.X + X1 * This.Staff_Height / 400.0) ..
           Integer(This.Center.X + X2 * This.Staff_Height / 400.0)
         loop
            -- Debug("vl " & To_String(Integer_Y) & " " & To_String(Integer_X));
            This.Pixels(Integer_Y, Integer_X) := 0;
         end loop;
      end loop;
   end Black_Box;

end Glyph_Images;