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

Subversion Repositories wf3d

[/] [wf3d/] [trunk/] [bin/] [a2bmp/] [bitmap.c] - Blame information for rev 2

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

Line No. Rev Author Line
1 2 specular
#include<stdio.h>
2
#include<stdlib.h>
3
#include<string.h>
4
#include<math.h>
5
#include"bitmap.h"
6
 
7
 
8
Image *read_txt(char *filename, int w, int h)
9
{
10
  int i, j;
11
  unsigned int color;
12
  FILE *fp;
13
  Image *img;
14
 
15
  if((fp = fopen(filename, "rb")) == NULL){
16
    fprintf(stderr, "Error: %s not found.", filename);
17
    return NULL;
18
  }
19
  // File format ABGR
20
  printf("w=%d, h=%d\n",w,h);
21
 
22
  if((img = create_image(w, h)) == NULL) return NULL;
23
  for (i=0;i<h;i++) {
24
    for (j=0;j<w;j++) {
25
      fscanf(fp, "%08x", &color);
26
      img->dat[w*i+j].r = color & 0xff;
27
      img->dat[w*i+j].g = (color >> 8) & 0xff;
28
      img->dat[w*i+j].b = (color >> 16) & 0xff;
29
    }
30
  }
31
 
32
  fclose(fp);
33
 
34
  return img;
35
}
36
 
37
int write_bmp(char *filename, Image *img)
38
{
39
  int i, j;
40
  FILE *fp;
41
  int real_width;
42
  unsigned char *bmp_line_data;
43
  unsigned char header_buf[HEADERSIZE];
44
  unsigned int file_size;
45
  unsigned int offset_to_data;
46
  unsigned long info_header_size;
47
  unsigned int planes;
48
  unsigned int color;
49
  unsigned long compress;
50
  unsigned long data_size;
51
  long xppm;
52
  long yppm;
53
 
54
  if((fp = fopen(filename, "wb")) == NULL){
55
    fprintf(stderr, "Error: %s could not open.", filename);
56
    return 1;
57
  }
58
 
59
  real_width = img->width*3 + img->width%4;
60
 
61
  // create header
62
  file_size = img->height * real_width + HEADERSIZE;
63
  offset_to_data = HEADERSIZE;
64
  info_header_size = INFOHEADERSIZE;
65
  planes = 1;
66
  color = 24;
67
  compress = 0;
68
  data_size = img->height * real_width;
69
  xppm = 1;
70
  yppm = 1;
71
 
72
  header_buf[0] = 'B';
73
  header_buf[1] = 'M';
74
  memcpy(header_buf + 2, &file_size, sizeof(file_size));
75
  header_buf[6] = 0;
76
  header_buf[7] = 0;
77
  header_buf[8] = 0;
78
  header_buf[9] = 0;
79
  memcpy(header_buf + 10, &offset_to_data, sizeof(file_size));
80
  header_buf[11] = 0;
81
  header_buf[12] = 0;
82
  header_buf[13] = 0;
83
 
84
  memcpy(header_buf + 14, &info_header_size, sizeof(info_header_size));
85
  header_buf[15] = 0;
86
  header_buf[16] = 0;
87
  header_buf[17] = 0;
88
  memcpy(header_buf + 18, &img->width, sizeof(img->width));
89
  memcpy(header_buf + 22, &img->height, sizeof(img->height));
90
  memcpy(header_buf + 26, &planes, sizeof(planes));
91
  memcpy(header_buf + 28, &color, sizeof(color));
92
  memcpy(header_buf + 30, &compress, sizeof(compress));
93
  memcpy(header_buf + 34, &data_size, sizeof(data_size));
94
  memcpy(header_buf + 38, &xppm, sizeof(xppm));
95
  memcpy(header_buf + 42, &yppm, sizeof(yppm));
96
  header_buf[46] = 0;
97
  header_buf[47] = 0;
98
  header_buf[48] = 0;
99
  header_buf[49] = 0;
100
  header_buf[50] = 0;
101
  header_buf[51] = 0;
102
  header_buf[52] = 0;
103
  header_buf[53] = 0;
104
 
105
  // write header
106
  fwrite(header_buf, sizeof(unsigned char), HEADERSIZE, fp);
107
 
108
  if((bmp_line_data = (unsigned char *)malloc(sizeof(unsigned char)*real_width)) == NULL){
109
    fprintf(stderr, "Error: Allocation error.\n");
110
    fclose(fp);
111
    return 1;
112
  }
113
 
114
  // write RGB
115
  for(i=0; i<img->height; i++){
116
    for(j=0; j<img->width; j++){
117
      bmp_line_data[j*3] = img->dat[(img->height - i - 1)*img->width + j].b;
118
      bmp_line_data[j*3 + 1] = img->dat[(img->height - i - 1)*img->width + j].g;
119
      bmp_line_data[j*3 + 2] = img->dat[(img->height - i - 1)*img->width + j].r;
120
    }
121
    // 4-bite align
122
    for(j=img->width*3; j<real_width; j++){
123
      bmp_line_data[j] = 0;
124
    }
125
    fwrite(bmp_line_data, sizeof(unsigned char), real_width, fp);
126
  }
127
  free(bmp_line_data);
128
 
129
  fclose(fp);
130
  return 0;
131
}
132
 
133
Image *create_image(int width, int height)
134
{
135
  Image *img;
136
 
137
  if((img = (Image *)malloc(sizeof(Image))) == NULL){
138
    fprintf(stderr, "Allocation error\n");
139
    return NULL;
140
  }
141
 
142
  if((img->dat = (rgb*)malloc(sizeof(rgb)*width*height)) == NULL){
143
    fprintf(stderr, "Allocation error\n");
144
      free(img);
145
      return NULL;
146
  }
147
 
148
  img->width = width;
149
  img->height = height;
150
 
151
  return img;
152
}
153
 
154
void delete_image(Image *img)
155
{
156
  free(img->dat);
157
  free(img);
158
}
159
 

powered by: WebSVN 2.1.0

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