-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathextractbmp.c
More file actions
104 lines (96 loc) · 2.74 KB
/
extractbmp.c
File metadata and controls
104 lines (96 loc) · 2.74 KB
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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(int argc, char *argv[]) {
FILE *fin,*fout,*iout,*fin1;
typedef struct idx_st {
int start;
int size;
int uncompressed_size;
} idx_type;
typedef struct header_st {
int magic;
int count;
int header_size;
int zero1;
int index_start;
int index_size;
int img_start;
int img_size;
int string_start;
int string_size;
} header_type;
if(argc>2) {
puts("usage: extractbmp [encode]");
exit(1);
}
int encode=0;
if(argc==2 && !strcmp(argv[1],"encode"))
encode=1;
fin=fopen("Image.img","rb");
header_type header;
fread(&header,4,10,fin);
idx_type index[header.count];
char fname[256];
fread(index, sizeof(idx_type), header.count, fin);
printf("%d bitmaps\n", header.count);
int outpos=0;
if(encode) {
iout=fopen("Image1.img","wb");
fwrite(&header,4,10,iout);
fwrite(index, sizeof(idx_type), header.count, iout);
outpos=sizeof(idx_type)*header.count+sizeof(header_type);
}
for(int i=0;i<header.count;i++) {
printf("bmp %d at %d, len %d\n",i, index[i].start, index[i].size);
sprintf(fname,"bmp/b%d.bmp.gz",i);
fout=fopen(fname,"wb");
fseek(fin,index[i].start, SEEK_SET);
for(int j=0;j<index[i].size;j++)
fputc(fgetc(fin),fout);
fclose(fout);
if(encode) {
sprintf(fname,"bmp1/b%d.bmp.gz",i);
fin1=fopen(fname,"rb");
if(!fin1) {
printf("%s not found\n",fname);
exit(1);
}
int l=0;
index[i].start=outpos;
while(!feof(fin1)) {
char c=fgetc(fin1);
if(!feof(fin1)) {
fputc(c,iout);
l++;
}
}
fclose(fin1);
index[i].size=l;
outpos+=l;
sprintf(fname,"bmp1/b%d.bmp",i);
fin1=fopen(fname,"rb");
if(fin1) {
fseek(fin1, 0L, SEEK_END);
index[i].uncompressed_size = ftell(fin1);
fclose(fin1);
}
}
// sprintf(fname,"gzip -d bmp/b%d.bmp.gz",i);
// system(fname);
}
char strings[header.string_size];
fseek(fin,header.string_start,SEEK_SET);
fout=fopen("bmp_names","wb");
fread(strings,1,header.string_size,fin);
fwrite(strings,1,header.string_size,fout);
fclose(fout);
if(encode) {
fwrite(strings,1,header.string_size,iout);
fseek(iout, 0, SEEK_SET);
header.img_size=outpos-index[0].start;
header.string_start=outpos;
fwrite(&header,4,10,iout);
fwrite(index, sizeof(idx_type), header.count, iout);
}
}