OpenCores
URL https://opencores.org/ocsvn/or1k_old/or1k_old/trunk

Subversion Repositories or1k_old

[/] [or1k_old/] [trunk/] [mw/] [src/] [mwin/] [bmp/] [makebmp.c] - Blame information for rev 1765

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 673 markom
/*
2
 * Copyright (c) 1999 Greg Haerr <greg@censoft.com>
3
 *
4
 * Framebuffer data to .bmp file converter
5
 *
6
 * 10/4/1999 g haerr
7
 */
8
#include <stdio.h>
9
#include <unistd.h>
10
 
11
#define BI_RGB          0L
12
#define BI_RLE8         1L
13
#define BI_RLE4         2L
14
#define BI_BITFIELDS    3L
15
 
16
typedef unsigned char   BYTE;
17
typedef unsigned short  WORD;
18
typedef unsigned long   DWORD;
19
typedef long            LONG;
20
 
21
#pragma pack(1)
22
/* windows style*/
23
typedef struct {
24
        /* BITMAPFILEHEADER*/
25
        BYTE    bfType[2];
26
        DWORD   bfSize;
27
        WORD    bfReserved1;
28
        WORD    bfReserved2;
29
        DWORD   bfOffBits;
30
        /* BITMAPINFOHEADER*/
31
        DWORD   BiSize;
32
        LONG    BiWidth;
33
        LONG    BiHeight;
34
        WORD    BiPlanes;
35
        WORD    BiBitCount;
36
        DWORD   BiCompression;
37
        DWORD   BiSizeImage;
38
        LONG    BiXpelsPerMeter;
39
        LONG    BiYpelsPerMeter;
40
        DWORD   BiClrUsed;
41
        DWORD   BiClrImportant;
42
} BMPHEAD;
43
#pragma pack()
44
 
45
int             MakeBMP(FILE *ifp,FILE *ofp);
46
 
47
int
48
main(int ac,char **av)
49
{
50
        FILE    *ifp;
51
        FILE    *ofp;
52
 
53
        if(ac < 3) {
54
                fprintf(stderr, "Usage: makebmp <infile> <outfile>\n");
55
                exit(1);
56
        }
57
        ifp = fopen(av[1], "rb");
58
        if(!ifp) {
59
                fprintf(stderr, "Can't open file: %s\n", av[1]);
60
                exit(1);
61
        }
62
        ofp = fopen(av[2], "wb");
63
        if(!ofp) {
64
                fprintf(stderr, "Can't create file: %s\n", av[2]);
65
                exit(1);
66
        }
67
        if(!MakeBMP(ifp, ofp)) {
68
                fprintf(stderr, "Conversion failed: %s\n", av[2]);
69
                fclose(ofp);
70
                unlink(av[2]);
71
                exit(1);
72
        }
73
        fclose(ifp);
74
        fclose(ofp);
75
        return 0;
76
}
77
 
78
/* create a bmp file*/
79
int
80
MakeBMP(FILE *ifp, FILE *ofp)
81
{
82
        BMPHEAD bmp;
83
        int     i, j;
84
        int     cx, cy, extra, bitdepth, ncolors;
85
 
86
        cx = 640;
87
        cy = 480;
88
        extra = (cx + 3) & 3;
89
        bitdepth = 8;
90
        if(bitdepth <= 8)
91
                ncolors = 1<<bitdepth;
92
        else ncolors = 0;
93
 
94
        memset(&bmp, 0, sizeof(bmp));
95
        bmp.bfType[0] = 'B';
96
        bmp.bfType[1] = 'M';
97
        bmp.bfSize = sizeof(bmp) + ncolors*4 + (long)cx*cy;
98
        bmp.bfOffBits = sizeof(bmp) + ncolors*4;
99
        bmp.BiSize = 40;
100
        bmp.BiWidth = cx;
101
        bmp.BiHeight = cy;
102
        bmp.BiPlanes = 1;
103
        bmp.BiBitCount = bitdepth;
104
        bmp.BiCompression = BI_RGB;
105
        //bmp.BiSizeImage = ??;
106
        bmp.BiClrUsed = ncolors;
107
        bmp.BiClrImportant = ncolors;
108
 
109
        /* write header*/
110
        fwrite(&bmp, sizeof(bmp), 1, ofp);
111
 
112
        /* write palette*/
113
        if(bitdepth <= 8) {
114
                for(i=0; i<ncolors; i++) {
115
                        unsigned char r, g, b;
116
                        r = getc(ifp);
117
                        g = getc(ifp);
118
                        b = getc(ifp);
119
                        putc(b, ofp);
120
                        putc(g, ofp);
121
                        putc(r, ofp);
122
                        putc(0, ofp);
123
                }
124
        }
125
 
126
        /* write image data, upside down ;)*/
127
        for(i=cy-1; i>0; --i) {
128
                long base = sizeof(bmp) + ncolors*4 + (long)i*cx;
129
                fseek(ofp, base, SEEK_SET);
130
                for(j=0; j<cx; ++j)
131
                        putc(getc(ifp), ofp);
132
                for(j=0; j<extra; ++j)
133
                        putc(0, ofp);            /* DWORD pad each line*/
134
        }
135
        return 1;
136
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.