forked from Yeolar/cjpeg
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrdbmp.c
76 lines (69 loc) · 2.22 KB
/
rdbmp.c
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
/* Copyright (C) 2009 - Yu, Le <[email protected]>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* 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. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
* 02111-1307, USA.
*
*/
/**
* @file rdbmp.c
* @brief routine for reading BMP file.
* @author Yu, Le <[email protected]>
* @version 1.0.0
* @date 2009-11-23 11:02:02
*/
#include "cjpeg.h"
UINT32
extract_uint(const UINT8 *dataptr, UINT32 start, UINT32 len)
{
UINT32 uint = 0;
if (len <= 0 || len > 4)
return uint;
if (len > 0)
uint += dataptr[start];
if (len > 1)
uint += dataptr[start+1] * 1 << 8;
if (len > 2)
uint += dataptr[start+2] * 1 << 16;
if (len > 3)
uint += dataptr[start+3] * 1 << 24;
return uint;
}
int
get_file_size(FILE *bmp_fp)
{
fpos_t fpos;
int len;
fgetpos(bmp_fp, &fpos);
fseek(bmp_fp, 0, SEEK_END);
len = ftell(bmp_fp);
fsetpos(bmp_fp, &fpos);
return len;
}
void
read_bmp(FILE *bmp_fp, bmp_info *binfo)
{
size_t len = BMP_HEAD_LEN;
UINT8 bmp_head[len];
if (fread(bmp_head, sizeof (UINT8), len, bmp_fp) != len)
err_exit(FILE_READ_ERR);
binfo->size = extract_uint(bmp_head, 2, 4);
binfo->offset = extract_uint(bmp_head, 10, 4);
binfo->width = extract_uint(bmp_head, 18, 4);
binfo->height = extract_uint(bmp_head, 22, 4);
binfo->bitppx = extract_uint(bmp_head, 28, 2);
binfo->datasize = extract_uint(bmp_head, 34, 4);
if (binfo->datasize == 0) /* data size not included in some BMP */
binfo->datasize = get_file_size(bmp_fp) - binfo->offset;
}