summaryrefslogtreecommitdiff
path: root/src/corelib/gfx_imgfmt_bmp.pas
blob: 46e5f0a8f222c6f29f9fd49560d9d3ae6b112d91 (plain)
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
{ 
  BMP format image parser
}

unit gfx_imgfmt_bmp;

{$mode objfpc}{$H+}

interface

uses
  Classes,
  SysUtils,
  fpgfx,
  gfxbase{, fpcanvas};

procedure ReadImage_BMP(img: TfpgImage; bmp: Pointer; bmpsize: longword);
function  LoadImage_BMP(const AFileName: String): TfpgImage;
function  CreateImage_BMP(bmp: Pointer; bmpsize: longword): TfpgImage;

implementation

function CreateImage_BMP(bmp: Pointer; bmpsize: longword): TfpgImage;
begin
  Result := TfpgImage.Create;
  ReadImage_BMP(Result, bmp, bmpsize);
end;

function LoadImage_BMP(const AFileName: String): TfpgImage;
var
  AFile: file of char;
  AImageData: Pointer;
  AImageDataSize: integer;
begin
  Result := nil;
  if not FileExists(AFileName) then
    Exit; //==>

  AssignFile(AFile, AFileName);
  FileMode := fmOpenRead; // read-only
  Reset(AFile);
  AImageDataSize := FileSize(AFile);
  AImageData := nil;
  GetMem(AImageData, AImageDataSize);
  try
    BlockRead(AFile, AImageData^, AImageDataSize);
    Result := TfpgImage.Create;
    ReadImage_BMP(Result, AImageData, AImageDataSize);
  finally
    CloseFile(AFile);
    FreeMem(AImageData);
  end;
end;


type
  // Windows BMP format description:
  // Below is the exact order how how information is stored in a BMP file.

  TBMPHeaderRec = packed record
    signature: word;
    filesize: longword;
    reserved: longword;
    dataoffset: longword;
  end;
  PBMPHeaderRec = ^TBMPHeaderRec;

  TBMPInfoHeaderRec = packed record
    headersize: longword; // = 40
    Width: longword;
    Height: longword;
    planes: word;
    bitcount: word;
    compression: longword;
    imagesize: longword; // bytes in the image data (after the color table)
    XpixelsPerM: longword;
    YpixelsPerM: longword;
    ColorsUsed: longword;
    ColorsImportant: longword;
  end;
  PBMPInfoHeaderRec = ^TBMPInfoHeaderRec;

  // Then follows the Color Table if bitcount <= 8

  TBMPColorTableRec = packed record
    red: byte;
    green: byte;
    blue: byte;
    reserved: byte;
  end;

 // Then follows the image data
 // Every line padded to 32 bits
 // The lines stored bottom-up


procedure ReadImage_BMP(img: TfpgImage; bmp: Pointer; bmpsize: longword);
var
  bh: PBMPHeaderRec;
  ih: PBMPInfoHeaderRec;
  p: PByte;
  ppal: plongword;
  pcol: Plongword;
  palsize: integer;
  pdata: PByte;
  b: byte;
  bit: byte;
  bcnt: byte;
  linecnt: integer;
  pixelcnt: integer;
  pdest: Plongword;
  depth: integer;

  function GetPalColor(cindex: longword): longword;
  var
    pc: Plongword;
  begin
    pc     := ppal;
    Inc(pc, cindex);
    Result := pc^;
  end;

begin
  if img = nil then
    Exit; //==>

  img.FreeImage;

  p         := bmp;
  PByte(bh) := p;
  ppal      := nil;
  if bh^.filesize <> bmpsize then
    Exit; //==>

  pdata := bmp;
  Inc(pdata, bh^.dataoffset);
  Inc(p, SizeOf(TBMPHeaderRec));
  PByte(ih) := p;
  depth := ih^.bitcount;

  if depth > 1 then
    img.AllocateImage(32, ih^.Width, ih^.Height)// color image
  else
  begin
    img.AllocateImage(1, ih^.Width, ih^.Height);
    img.AllocateMask;
  end;

  //Writeln('width: ',img.width,' height: ',img.height,' depth: ',depth);
  //Writeln('compression: ',ih^.compression);

  Inc(p, SizeOf(TBMPInfoHeaderRec));

  if ih^.bitcount <= 8 then
  begin
    // reading color palette
    case ih^.bitcount of
      1: palsize := 2;
      4: palsize := 16;
      else
        palsize  := 256;
    end;

    GetMem(ppal, palsize * SizeOf(longword));

    pcol     := ppal;
    pixelcnt := 0;
    while (p) < (pdata) do
    begin
      pcol^ := Plongword(p)^;
      //Writeln('color: ',HexStr(pcol^,8));
      Inc(pcol);
      Inc(Plongword(p));
      Inc(pixelcnt);
    end;
    //writeln(pixelcnt,' colors loaded.');
  end;

  pdest := img.ImageData;
  Inc(pdest, img.Width * (img.Height - 1));  // bottom-up line order
  p := bmp;
  Inc(p, bh^.dataoffset);

  // reading the data...
  case ih^.bitcount of
    1:
    begin
      // direct line transfer
      //writeln('reading 1-bit color bitmap');
      linecnt := 0;
      bcnt := img.Width div 32;
      if (img.Width and $1F) > 0 then
        Inc(bcnt);

      pdest := img.ImageData;
      Inc(pdest, bcnt * (img.Height - 1));  // bottom-up line order
      repeat
        move(p^, pdest^, bcnt * 4);
        Inc(p, bcnt * 4);
        Dec(pdest, bcnt);
        Inc(linecnt);
      until linecnt >= img.Height;

      //Writeln(linecnt,' lines loaded.');
      move(img.ImageData^, img.MaskData^, img.ImageDataSize);
      img.Invert;
    end;

    4:
    begin
      //writeln('reading 4-bit color');
      linecnt := 0;
      repeat
        // parse one line..
        bit      := 0;
        pixelcnt := 0;
        bcnt     := 0;
        repeat
          if bit = 0 then
            b := (p^ shr 4) and $0F
          else
          begin
            b := p^ and $0F;
            Inc(p);
            Inc(bcnt);
          end;

          pdest^ := GetPalColor(b);
          Inc(pdest);
          Inc(pixelcnt);
          bit := bit xor 1;
        until pixelcnt >= img.Width;

        while (bcnt mod 4) <> 0 do
        begin
          Inc(bcnt);
          Inc(p);
        end;

        Inc(linecnt);
        Dec(pdest, img.Width * 2);  // go to next line
      until linecnt >= img.Height;
    end;

    8:
    begin
      //writeln('reading 8-bit color');
      linecnt := 0;
      repeat
        // parse one line..
        pixelcnt := 0;
        repeat
          pdest^ := GetPalColor(p^);
          Inc(p);
          Inc(pdest);
          Inc(pixelcnt);
        until pixelcnt >= img.Width;

        while (pixelcnt mod 4) <> 0 do
        begin
          Inc(pixelcnt);
          Inc(p);
        end;

        Inc(linecnt);
        Dec(pdest, img.Width * 2);  // go to next line
      until linecnt >= img.Height;
    end;

    24:
    begin
      //writeln('reading truecolor');
      linecnt := 0;
      repeat
        // parse one line..
        pixelcnt := 0;
        repeat
          pdest^ := p^;
          Inc(p);
          pdest^ := pdest^ or (longword(p^) shl 8);
          Inc(p);
          pdest^ := pdest^ or (longword(p^) shl 16);
          Inc(p);
          Inc(pdest);
          Inc(pixelcnt);
        until pixelcnt >= img.Width;

        pixelcnt := img.Width * 3;
        while (pixelcnt mod 4) <> 0 do
        begin
          Inc(pixelcnt);
          Inc(p);
        end;

        Inc(linecnt);
        Dec(pdest, img.Width * 2);  // go to next line
      until linecnt >= img.Height;
    end;
    else
      writeln('Unsupported BMP format!');
  end;

  if ppal <> nil then
    FreeMem(ppal);

  img.UpdateImage;
end;

end.