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

Subversion Repositories wf3d

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

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 4 specular
//=======================================================================
2
// Project Monophony
3
//   Wire-Frame 3D Graphics Accelerator IP Core
4
//
5
// File:
6
//   bitmap.c
7
//
8
// Abstract:
9
//   ascii to bitmap converter
10
//
11
// Author:
12 9 specular
//   Kenji Ishimaru (info.info.wf3d@gmail.com)
13 4 specular
//
14
//======================================================================
15
//
16
// Copyright (c) 2015, Kenji Ishimaru
17
// All rights reserved.
18
//
19
// Redistribution and use in source and binary forms, with or without
20
// modification, are permitted provided that the following conditions are met:
21
//
22
//  -Redistributions of source code must retain the above copyright notice,
23
//   this list of conditions and the following disclaimer.
24
//  -Redistributions in binary form must reproduce the above copyright notice,
25
//   this list of conditions and the following disclaimer in the documentation
26
//   and/or other materials provided with the distribution.
27
//
28
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
29
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
30
// THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
31
// PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
32
// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
33
// EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
34
// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
35
// OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
36
// WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
37
// OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
38
// EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
39
//
40
// Revision History
41
 
42 2 specular
#include<stdio.h>
43
#include<stdlib.h>
44
#include<string.h>
45
#include<math.h>
46
#include"bitmap.h"
47
 
48
 
49
Image *read_txt(char *filename, int w, int h)
50
{
51
  int i, j;
52
  unsigned int color;
53
  FILE *fp;
54
  Image *img;
55
 
56
  if((fp = fopen(filename, "rb")) == NULL){
57
    fprintf(stderr, "Error: %s not found.", filename);
58
    return NULL;
59
  }
60
  // File format ABGR
61
  printf("w=%d, h=%d\n",w,h);
62
 
63
  if((img = create_image(w, h)) == NULL) return NULL;
64
  for (i=0;i<h;i++) {
65
    for (j=0;j<w;j++) {
66
      fscanf(fp, "%08x", &color);
67
      img->dat[w*i+j].r = color & 0xff;
68
      img->dat[w*i+j].g = (color >> 8) & 0xff;
69
      img->dat[w*i+j].b = (color >> 16) & 0xff;
70
    }
71
  }
72
 
73
  fclose(fp);
74
 
75
  return img;
76
}
77
 
78
int write_bmp(char *filename, Image *img)
79
{
80
  int i, j;
81
  FILE *fp;
82
  int real_width;
83
  unsigned char *bmp_line_data;
84
  unsigned char header_buf[HEADERSIZE];
85
  unsigned int file_size;
86
  unsigned int offset_to_data;
87
  unsigned long info_header_size;
88
  unsigned int planes;
89
  unsigned int color;
90
  unsigned long compress;
91
  unsigned long data_size;
92
  long xppm;
93
  long yppm;
94
 
95
  if((fp = fopen(filename, "wb")) == NULL){
96
    fprintf(stderr, "Error: %s could not open.", filename);
97
    return 1;
98
  }
99
 
100
  real_width = img->width*3 + img->width%4;
101
 
102
  // create header
103
  file_size = img->height * real_width + HEADERSIZE;
104
  offset_to_data = HEADERSIZE;
105
  info_header_size = INFOHEADERSIZE;
106
  planes = 1;
107
  color = 24;
108
  compress = 0;
109
  data_size = img->height * real_width;
110
  xppm = 1;
111
  yppm = 1;
112
 
113
  header_buf[0] = 'B';
114
  header_buf[1] = 'M';
115
  memcpy(header_buf + 2, &file_size, sizeof(file_size));
116
  header_buf[6] = 0;
117
  header_buf[7] = 0;
118
  header_buf[8] = 0;
119
  header_buf[9] = 0;
120
  memcpy(header_buf + 10, &offset_to_data, sizeof(file_size));
121
  header_buf[11] = 0;
122
  header_buf[12] = 0;
123
  header_buf[13] = 0;
124
 
125
  memcpy(header_buf + 14, &info_header_size, sizeof(info_header_size));
126
  header_buf[15] = 0;
127
  header_buf[16] = 0;
128
  header_buf[17] = 0;
129
  memcpy(header_buf + 18, &img->width, sizeof(img->width));
130
  memcpy(header_buf + 22, &img->height, sizeof(img->height));
131
  memcpy(header_buf + 26, &planes, sizeof(planes));
132
  memcpy(header_buf + 28, &color, sizeof(color));
133
  memcpy(header_buf + 30, &compress, sizeof(compress));
134
  memcpy(header_buf + 34, &data_size, sizeof(data_size));
135
  memcpy(header_buf + 38, &xppm, sizeof(xppm));
136
  memcpy(header_buf + 42, &yppm, sizeof(yppm));
137
  header_buf[46] = 0;
138
  header_buf[47] = 0;
139
  header_buf[48] = 0;
140
  header_buf[49] = 0;
141
  header_buf[50] = 0;
142
  header_buf[51] = 0;
143
  header_buf[52] = 0;
144
  header_buf[53] = 0;
145
 
146
  // write header
147
  fwrite(header_buf, sizeof(unsigned char), HEADERSIZE, fp);
148
 
149
  if((bmp_line_data = (unsigned char *)malloc(sizeof(unsigned char)*real_width)) == NULL){
150
    fprintf(stderr, "Error: Allocation error.\n");
151
    fclose(fp);
152
    return 1;
153
  }
154
 
155
  // write RGB
156
  for(i=0; i<img->height; i++){
157
    for(j=0; j<img->width; j++){
158
      bmp_line_data[j*3] = img->dat[(img->height - i - 1)*img->width + j].b;
159
      bmp_line_data[j*3 + 1] = img->dat[(img->height - i - 1)*img->width + j].g;
160
      bmp_line_data[j*3 + 2] = img->dat[(img->height - i - 1)*img->width + j].r;
161
    }
162
    // 4-bite align
163
    for(j=img->width*3; j<real_width; j++){
164
      bmp_line_data[j] = 0;
165
    }
166
    fwrite(bmp_line_data, sizeof(unsigned char), real_width, fp);
167
  }
168
  free(bmp_line_data);
169
 
170
  fclose(fp);
171
  return 0;
172
}
173
 
174
Image *create_image(int width, int height)
175
{
176
  Image *img;
177
 
178
  if((img = (Image *)malloc(sizeof(Image))) == NULL){
179
    fprintf(stderr, "Allocation error\n");
180
    return NULL;
181
  }
182
 
183
  if((img->dat = (rgb*)malloc(sizeof(rgb)*width*height)) == NULL){
184
    fprintf(stderr, "Allocation error\n");
185
      free(img);
186
      return NULL;
187
  }
188
 
189
  img->width = width;
190
  img->height = height;
191
 
192
  return img;
193
}
194
 
195
void delete_image(Image *img)
196
{
197
  free(img->dat);
198
  free(img);
199
}
200
 

powered by: WebSVN 2.1.0

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