wl_lib_0.1.3_1c94dc7c/src/wl-bitmap_io.adb

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
with Ada.Unchecked_Deallocation;

package body WL.Bitmap_IO is

   use WL.Binary_IO;

   procedure Free is
     new Ada.Unchecked_Deallocation (Bitmap_Data,
                                     Bitmap_Data_Access);

   procedure Free is
     new Ada.Unchecked_Deallocation (Bitmap_Color_Index_Data,
                                     Bitmap_Color_Index_Access);

   procedure Free is
     new Ada.Unchecked_Deallocation (Colormap_Type,
                                     Colormap_Access);

   Reversed : Boolean := False;

   type Bitmap_Magic is new String (1 .. 2);
   for Bitmap_Magic'Size use 16;

   type Bitmap_Header is
      record
         File_Size   : Word_32;
         Creator_1   : Word_16;
         Creator_2   : Word_16;
         Data_Start  : Word_32;
      end record;

   for Bitmap_Header'Size use 96;

   type Bitmap_Information_Header is
      record
         Header_Size       : Word_32;
         Width             : Word_32;
         Height            : Word_32;
         Num_Planes        : Word_16;
         Bits_Per_Pixel    : Word_16;
         Compression       : Word_32;
         Image_Size        : Word_32;
         Horizontal_Res    : Word_32;
         Vertical_Res      : Word_32;
         Colormap_Size    : Word_32;
         Important_Colors : Word_32;
      end record;

   for Bitmap_Information_Header'Size use 40 * 8;

   -----------------------
   -- Adjust_Brightness --
   -----------------------

   function Adjust_Brightness
     (Color     : Color_Type;
      Factor     : Float)
      return Color_Type
   is
      R : constant Float :=
            Float'Min
              (Float (Color.R) * Factor, 255.0);
      G : constant Float :=
            Float'Min
              (Float (Color.G) * Factor, 255.0);
      B : constant Float :=
            Float'Min
              (Float (Color.B) * Factor, 255.0);
   begin
      return (R => Color_Element (R),
              G => Color_Element (G),
              B => Color_Element (B),
              Alpha => 255);
   end Adjust_Brightness;

   -----------
   -- Close --
   -----------

   procedure Close (Bitmap : in out Bitmap_Type) is
   begin
      if Bitmap.Data /= null then
         Free (Bitmap.Data);
      end if;
      if Bitmap.Indices /= null then
         Free (Bitmap.Indices);
      end if;
      if Bitmap.Colormap /= null then
         Free (Bitmap.Colormap);
      end if;
   end Close;

   ------------
   -- Color --
   ------------

   function Color (Item : Bitmap_Type;
                    X, Y : Natural)
                   return Color_Type
   is
   begin
      if Has_Colormap (Item) then
         return Colormap_Color (Item, Color_Index (Item, X, Y));
      else
         return Item.Data (X, Y);
      end if;
   end Color;

   ------------------
   -- Color_Index --
   ------------------

   function Color_Index (Item : Bitmap_Type;
                          X, Y : Natural)
                         return Color_Element
   is
   begin
      return Item.Indices (X, Y);
   end Color_Index;

   ----------------------
   -- Colormap_Color --
   ----------------------

   function Colormap_Color (Item  : Bitmap_Type;
                              Index : Color_Element)
                             return Color_Type
   is
   begin
      return Item.Colormap (Index);
   end Colormap_Color;

   -----------
   -- Depth --
   -----------

   function Depth (Item : Bitmap_Type) return Natural is
   begin
      return Item.Depth;
   end Depth;

   -------------------
   -- Has_Colormap --
   -------------------

   function Has_Colormap (Item : Bitmap_Type) return Boolean is
   begin
      return Item.Colormap /= null;
   end Has_Colormap;

   ------------
   -- Height --
   ------------

   function Height (Item : Bitmap_Type) return Natural is
   begin
      return Item.Height;
   end Height;

   --------------------------
   -- Linear_Interpolation --
   --------------------------

   function Linear_Interpolation
     (Start_Color  : Color_Type;
      Finish_Color : Color_Type;
      Start_Value   : Integer;
      Finish_Value  : Integer;
      Value         : Integer)
      return Color_Type
   is
      function Interpolate (Start, Finish : Color_Element)
                            return Color_Element;

      -----------------
      -- Interpolate --
      -----------------

      function Interpolate (Start, Finish : Color_Element)
                            return Color_Element
      is
      begin
         return Color_Element ((Integer (Finish) - Integer (Start)) *
                                (Value - Start_Value)
                                / (Finish_Value - Start_Value)
                               + Integer (Start));
      end Interpolate;

   begin
      if Start_Value = Finish_Value then
         return Start_Color;
      else
         return (Interpolate (Start_Color.B, Finish_Color.B),
                 Interpolate (Start_Color.G, Finish_Color.G),
                 Interpolate (Start_Color.R, Finish_Color.R),
                 Interpolate (Start_Color.Alpha, Finish_Color.Alpha));
      end if;
   end Linear_Interpolation;

   ----------------
   -- New_Bitmap --
   ----------------

   function New_Bitmap (Width, Height : Natural) return Bitmap_Type is
      Result : constant Bitmap_Type :=
        (Width        => Width,
         Height       => Height,
         Depth        => 32,
         Data         => new Bitmap_Data (0 .. Width - 1, 0 .. Height - 1),
         Indices      => null,
         Colormap    => null);
   begin
      Result.Data.all := (others => (others => (0, 0, 0, 1)));
      return Result;
   end New_Bitmap;

   ----------
   -- Read --
   ----------

   procedure Read (Bitmap    : out Bitmap_Type;
                   File_Name :     String)
   is
      File           : File_Type;
   begin
      Open (File, In_File, File_Name);
      Read (Bitmap, File);
      Close (File);
   exception
      when others =>
         Close (File);
         raise;
   end Read;

   ----------
   -- Read --
   ----------

   procedure Read (Bitmap  : out Bitmap_Type;
                   File    : in out WL.Binary_IO.File_Type)
   is
      Magic          : Bitmap_Magic;
      Header         : Bitmap_Header;
      Info_Header    : Bitmap_Information_Header;
      Row_Size       : Word_32;
      BPP            : Word_32;
      Used           : array (Color_Element) of Natural;
   begin
      Copy (File, 0, 2, Magic'Address);
      if Magic /= "BM" then
         raise Constraint_Error with
           "bad magic number: " & String (Magic);
      end if;

      Copy (File, 2, Header'Size / 8, Header'Address);
      Copy (File, 14, Info_Header'Size / 8, Info_Header'Address);

      BPP := Word_32 (Info_Header.Bits_Per_Pixel);
      Bitmap.Depth := Natural (BPP);

      Bitmap.Width := Natural (Info_Header.Width);
      Bitmap.Height := Natural (Info_Header.Height);

      if BPP <= 8 then
         Bitmap.Indices :=
           new Bitmap_Color_Index_Data (0 .. Bitmap.Width - 1,
                                         0 .. Bitmap.Height - 1);
         Bitmap.Colormap := new Colormap_Type;
         Copy (File, 14 + Info_Header.Header_Size,
               2**Natural (BPP) * 4,
               Bitmap.Colormap.all'Address);
         Used := (others => 0);
      else

         Bitmap.Data := new Bitmap_Data (0 .. Bitmap.Width - 1,
                                         0 .. Bitmap.Height - 1);
      end if;

      Row_Size := Info_Header.Width * BPP / 8;
      if Row_Size mod 4 /= 0 then
         Row_Size := Row_Size + 4 - Row_Size mod 4;
      end if;

      for Y in 0 .. Bitmap.Height - 1 loop
         for X in 0 .. Bitmap.Width - 1 loop
            declare
               Row_Offset   : constant Word_32 :=
                                Header.Data_Start
                                  + Word_32 (Y) * Row_Size;
               Col_Offset   : constant Word_32 :=
                                Word_32 (X) * BPP / 8;
               Bit_Offset   : constant Natural :=
                                X * Natural (BPP) mod 8;
               Color       : Color_Type := (0, 0, 0, 0);
            begin
               if BPP >= 24 then
                  Copy (File, Row_Offset + Col_Offset, BPP / 8,
                        Color'Address);
                  if Reversed then
                     Bitmap.Data (X, Bitmap.Height - Y - 1) := Color;
                  else
                     Bitmap.Data (X, Y) := Color;
                  end if;
               else
                  declare
                     W8    : Word_8;
                     Index : Color_Element;
                  begin
                     Read (File, W8, Row_Offset + Col_Offset);
                     if BPP < 8 then
                        W8 := W8 / (2 ** Bit_Offset)
                        mod (2 ** Natural (BPP));
                     end if;
                     Index := Color_Element (W8);
                     Used (Index) := Used (Index) + 1;
                     if Reversed then
                        Bitmap.Indices (X, Bitmap.Height - Y - 1) := Index;
                     else
                        Bitmap.Indices (X, Y) := Index;
                     end if;
                  end;
               end if;
            end;
         end loop;
      end loop;

   end Read;

   ----------------
   -- Set_Color --
   ----------------

   procedure Set_Color (Item   : Bitmap_Type;
                         X, Y   : Natural;
                         Color : Color_Type)
   is
   begin
      Item.Data (X, Y) := Color;
   end Set_Color;

   -----------------------
   -- Set_Vertical_Flip --
   -----------------------

   procedure Set_Vertical_Flip (Value : Boolean := True) is
   begin
      Reversed := Value;
   end Set_Vertical_Flip;

   -----------
   -- Width --
   -----------

   function Width (Item : Bitmap_Type) return Natural is
   begin
      return Item.Width;
   end Width;

   -----------
   -- Write --
   -----------

   procedure Write (Bitmap    :    Bitmap_Type;
                    File_Name :    String)
   is
      File           : Binary_IO.File_Type;
      Magic          : constant Bitmap_Magic := "BM";
      Header         : Bitmap_Header;
      Info_Header    : Bitmap_Information_Header;
   begin
      Binary_IO.Create (File, Binary_IO.Out_File, File_Name);
      Binary_IO.Write (File, 2, Magic'Address);
      Header.File_Size := 16#36# +
        32 * Word_32 (Bitmap.Width) * Word_32 (Bitmap.Height);
      Header.Creator_1 := 16#424C#;
      Header.Creator_2 := 16#414F#;
      Header.Data_Start := 16#00000036#;
      Binary_IO.Write (File, Header'Size / 8, Header'Address);

      Info_Header.Header_Size := 16#0000_0028#;
      Info_Header.Width       := Word_32 (Bitmap.Width);
      Info_Header.Height      := Word_32 (Bitmap.Height);
      Info_Header.Num_Planes  := 1;
      Info_Header.Bits_Per_Pixel := 32;
      Info_Header.Compression    := 0;
      Info_Header.Image_Size     :=
        32 * Word_32 (Bitmap.Width) * Word_32 (Bitmap.Height);
      Info_Header.Horizontal_Res := 2835;
      Info_Header.Vertical_Res   := 2835;
      Info_Header.Colormap_Size := 0;
      Info_Header.Important_Colors := 0;

      Write (File, Info_Header'Size / 8, Info_Header'Address);

      for Y in 0 .. Bitmap.Height - 1 loop
         for X in 0 .. Bitmap.Width - 1 loop
            declare
               Color : constant Color_Type :=
                          (if Bitmap.Depth >= 24
                           then Bitmap.Data (X, Y)
                           else Bitmap.Colormap (Bitmap.Indices (X, Y)));
            begin
               Write (File, Word_8 (Color.B));
               Write (File, Word_8 (Color.G));
               Write (File, Word_8 (Color.R));
               Write (File, Word_8 (Color.Alpha));
            end;
         end loop;
      end loop;

      Close (File);

   end Write;

end WL.Bitmap_IO;