summaryrefslogtreecommitdiff
path: root/img/bmpreader.pas
blob: 71d7ee73e0b3614d12f64d3ec2e9bceca580167e (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
{
    fpImg  -  Free Pascal Imaging Library
    Copyright (C) 2000 by
      Areca Systems GmbH / Sebastian Guenther, sg@freepascal.org

    Image reader for BMP files

    See the file COPYING, included in this distribution,
    for details about the copyright.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
}


unit BMPReader;

{$IFDEF Debug}
{$ASSERTIONS On}
{$ENDIF}

interface

uses Classes, GFXBase, ImageIO;

type
  DWORD = LongWord;
  LONG = LongInt;

  TBitmapFileHeader = packed record
    bfType: WORD;
    bfSize: DWORD;
    bfReserved1: WORD;
    bfReserved2: WORD;
    bfOffBits: DWORD;
  end;

  TBitmapInfoHeader = packed record
    biSize: DWORD;
    biWidth: LONG;
    biHeight: LONG;
    biPlanes: WORD;
    biBitCount: WORD;
    biCompression: DWORD;
    biSizeImage: DWORD;
    biXPelsPerMeter: LONG;
    biYPelsPerMeter: LONG;
    biClrUsed: DWORD;
    biClrImportant: DWORD;
  end;

  PRGBQuad = ^TRGBQuad;
  TRGBQuad = packed record
    rgbBlue, rgbGreen, rgbRed, rgbReserved: BYTE;
  end;


  TBMPReader = class(TImageReader)
  protected
    FFileHeader: TBitmapFileHeader;
    FInfoHeader: TBitmapInfoHeader;
    FBMPPalette: PRGBQuad;
    FFileStride: LongWord;

    HeaderBytesRead, PalBytesRead: Integer;
    ScanlinesLeft: Integer;
    ThisSegmentHeight: Integer;
    ScanlinesLeftInSegment: Integer;
    ScanlineBytesDone: LongWord;
    CurScanline: Pointer;

    procedure DoProcessHeaderData(AStream: TStream); override;
    function DoGetImageSegmentStartY(ASegmentHeight: Integer): Integer;
      override;
    procedure InitImageReading; override;
    procedure InitSegmentReading;
    procedure DoProcessImageData(AStream: TStream); override;
  public
    destructor Destroy; override;
    property FileHeader: TBitmapFileHeader read FFileHeader;
    property InfoHeader: TBitmapInfoHeader read FInfoHeader;
    property BMPPalette: PRGBQuad read FBMPPalette;
    property FileStride: LongWord read FFileStride;
  end;


implementation


destructor TBMPReader.Destroy;
begin
  if Assigned(Palette) then
    FreeMem(FPalette);
  if Assigned(BMPPalette) then
    FreeMem(FBMPPalette);
  inherited Destroy;
end;

procedure TBMPReader.DoProcessHeaderData(AStream: TStream);
var
  DataOffset: LongWord;
  HaveRead, BytesToSkip, i: Integer;
  IsFirstRead: Boolean;
  SkipBuffer: array[0..1023] of Byte;
begin
  if HeaderBytesRead < SizeOf(FileHeader) then
  begin
    HaveRead := AStream.Read(PChar(@FileHeader)[HeaderBytesRead],
      SizeOf(FileHeader) - HeaderBytesRead);
    if HaveRead = 0 then
      raise EImgOutOfData.Create;
    Inc(HeaderBytesRead, HaveRead);
    IsFirstRead := False;
  end else
    IsFirstRead := True;

  if HeaderBytesRead < SizeOf(FileHeader) + SizeOf(InfoHeader) then
  begin
    HaveRead := AStream.Read(
      PChar(@InfoHeader)[HeaderBytesRead - SizeOf(FileHeader)],
      SizeOf(FileHeader) + SizeOf(InfoHeader) - HeaderBytesRead);
    if HaveRead = 0 then
      if IsFirstRead then
        raise EImgOutOfData.Create
      else
        exit;
    IsFirstRead := False;
    Inc(HeaderBytesRead, HaveRead);
  end;

  if HeaderBytesRead = SizeOf(FileHeader) + SizeOf(InfoHeader) then
  begin
    case InfoHeader.biBitCount of
      1: FPaletteSize := 2;
      4: FPaletteSize := 16;
      8: FPaletteSize := 256;
    end;
    if PaletteSize > 0 then
    begin
      GetMem(FBMPPalette, PaletteSize * SizeOf(TRGBQuad));
      GetMem(FPalette, PaletteSize * SizeOf(TGfxColor));
    end;
  end;

  if HeaderBytesRead >= SizeOf(FileHeader) + SizeOf(InfoHeader) then
  begin
    DataOffset := FileHeader.bfOffBits;
    if HeaderBytesRead < DataOffset then
    begin
      BytesToSkip := DataOffset - HeaderBytesRead;
      if BytesToSkip > SizeOf(SkipBuffer) then
        BytesToSkip := SizeOf(SkipBuffer);
      HaveRead := AStream.Read(SkipBuffer, BytesToSkip);
      if HaveRead = 0 then
	if IsFirstRead then
	  raise EImgOutOfData.Create
	else
	  exit;
      IsFirstRead := False;
      Inc(HeaderBytesRead, HaveRead);
      if PalBytesRead < PaletteSize * SizeOf(TGfxPixel) then
      begin
        Move(SkipBuffer, PByte(FBMPPalette)[PalBytesRead], HaveRead);
        Inc(PalBytesRead, HaveRead);
      end;
    end;

    if HeaderBytesRead = DataOffset then
    begin
      FWidth := InfoHeader.biWidth;
      FHeight := InfoHeader.biHeight;

      if PaletteSize > 0 then
        for i := 0 to PaletteSize - 1 do
	begin
	  Palette[i].Red := BMPPalette[i].rgbRed * 257;
	  Palette[i].Green := BMPPalette[i].rgbGreen * 257;
	  Palette[i].Blue := BMPPalette[i].rgbBlue * 257;
	  Palette[i].Alpha := 0;
	end;

      case InfoHeader.biBitCount of
	1:
          begin
	    FFileStride := ((Width + 31) shr 3) and not 3;
	    FPixelFormat.FormatType := ftMono;
	  end;
	4:
          begin
	    FFileStride := ((Width + 7) shr 1) and not 3;
	    FPixelFormat.FormatType := ftPal4;
	  end;
	8:
          begin
	    FFileStride := (Width + 3) and not 3;
	    FPixelFormat.FormatType := ftPal8;
	  end;
        24:
          begin
            FFileStride := (Width * 3 + 3) and not 3;
  	    FPixelFormat := PixelFormatBGR24;
          end;
        else
          raise EImgUnsupportedPixelFormat.Create;
      end;
    end;

    HeaderFinished;
  end;
end;

function TBMPReader.DoGetImageSegmentStartY(ASegmentHeight: Integer): Integer;
begin
  Result := ScanlinesLeft - ASegmentHeight;
  if Result < 0 then
    Result := 0;
end;

procedure TBMPReader.InitImageReading;
begin
  ScanlinesLeft := Height;
  InitSegmentReading;
end;

procedure TBMPReader.InitSegmentReading;
begin
  ThisSegmentHeight := ScanlinesLeft;
  if ThisSegmentHeight > SegmentHeight then
    ThisSegmentHeight := SegmentHeight;
  ScanlinesLeftInSegment := ThisSegmentHeight;
  ScanlineBytesDone := 0;
  CurScanline := SegmentData + (ThisSegmentHeight - 1) * SegmentStride;
end;

procedure TBMPReader.DoProcessImageData(AStream: TStream);

  procedure ScanlineDone;
  begin
    Dec(ScanlinesLeftInSegment);
    Dec(ScanlinesLeft);

    if ScanlinesLeftInSegment = 0 then
    begin
      SegmentFinished(ScanlinesLeft, ThisSegmentHeight);
      if ScanlinesLeft = 0 then
        ImageFinished
      else
        InitSegmentReading;
    end else
      Dec(CurScanline, SegmentStride);
  end;

var
  ReadMayFail: Boolean;
  ToRead, HaveRead: Integer;
begin
  if ScanlineBytesDone > 0 then
  begin
    ToRead := SegmentStride;
    if ToRead > FileStride then
      ToRead := FileStride;
    Dec(ToRead, ScanlineBytesDone);
    HaveRead := AStream.Read(PChar(CurScanline)[ScanlineBytesDone], ToRead);
    if HaveRead = 0 then
      raise EImgOutOfData.Create;
    if HaveRead = ToRead then
    begin
      ScanlineBytesDone := 0;
      ScanlineDone
    end else
    begin
      Inc(ScanlineBytesDone, HaveRead);
      exit;
    end;
    ReadMayFail := True;
  end else
    ReadMayFail := False;

  while ScanlinesLeft > 0 do
  begin
    ToRead := SegmentStride;
    if ToRead > FileStride then
      ToRead := FileStride;

    HaveRead := AStream.Read(CurScanline^, ToRead);

    if HaveRead = 0 then
      if ReadMayFail then
        exit
      else
        raise EImgOutOfData.Create;

    if HaveRead < ToRead then
    begin
      ScanlineBytesDone := HaveRead;
      break;
    end;

    // Handle the ordinary case: a full scanline has been read
    if ToRead < FileStride then
      AStream.Position := AStream.Position + FileStride - ToRead;
    ReadMayFail := True;
    ScanlineDone;
  end;
end;

end.