LLIPS  rev33
Light Library for Image ProcesS
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros
llips_io.c
Go to the documentation of this file.
1 /********************************************/
8 /* ***************************************************************/
9 /* * Includes */
10 /* ***************************************************************/
11 #include "llips_includes.h"
12 
13 /* ***************************************************************/
14 /* * Global variables */
15 /* ***************************************************************/
16 
17 /* ***************************************************************/
18 /* * Functions */
19 /* ***************************************************************/
20 
21 
22 /********************************************/
30 {
31  img->signature = 0;
32  img->depth = 0;
33  img->wi = 0;
34  img->he = 0;
38 }
39 
40 
41 /********************************************/
49 CPU_CHAR load_img(CPU_CHAR * imgname, t_img * img)
50 {
51  CPU_INT16S i,j,offset;
52  CPU_CHAR ret = ERR_NONE;
53  FILE* fichier = NULL;
54  CPU_CHAR dummy=0;
55 
56  // Open file
57  fichier = fopen((const char *)imgname, "rb");
58  if (fichier != NULL)
59  {
60  //Get signature
61  fseek (fichier,OFFSET_SIGN,SEEK_SET);
62  fread (&(img->signature),OFFSET_SIGN_size,1,fichier);
63  if(img->signature == WIN_BMP)
64  {
65  //Get depth
66  fseek (fichier,OFFSET_IMG_DEPTH,SEEK_SET);
67  fread (&(img->depth),OFFSET_IMG_DEPTH_size,1,fichier);
68  if(img->depth ==DEPTH_24bit)
69  {
70  //Get width
71  fseek (fichier,OFFSET_IMG_WIDTH,SEEK_SET);
72  fread (&(img->wi),OFFSET_IMG_WIDTH_size,1,fichier);
73  if(img->wi <= MAX_WIDTH)
74  {
75  //Get height
76  fseek (fichier,OFFSET_IMG_HEIGHT,SEEK_SET);
77  fread (&(img->he),OFFSET_IMG_HEIGHT_size,1,fichier);
78  if(img->he <= MAX_HEIGHT)
79  {
80  // Here, it is knwon that the BMP is a 24bit Windows BMP
81  // with width and height below 1kpix
82 
83 
84  //Get image offset to deduct header size
85  fseek (fichier,OFFSET_IMG_P_DATA,SEEK_SET);
86  fread (&(img->FileHeader_size),OFFSET_IMG_P_DATA_size,1,fichier);
87 
88 
90  {
91  //copy header ...
92  fseek (fichier,0,SEEK_SET);
93  for(i=0;i<img->FileHeader_size;i++)
94  {
95  fread (&(img->FileHeader[i]),PIXEL_size,1,fichier);
96  }
97  //copy IMG data
98  fseek(fichier,0,SEEK_END);
99  fseek (fichier,OFFSET_IMG_DATA_WIN24bit,SEEK_SET);
100  offset = 0;
101  for(i=0;i< img->he ;i++)
102  {
103  for(j=0 ; j< img->wi ;j++)
104  {
105  fread (&(img->Blue[i][j]),PIXEL_size,1,fichier);
106  fread (&(img->Green[i][j]),PIXEL_size,1,fichier);
107  fread (&(img->Red[i][j]),PIXEL_size,1,fichier);
108  offset ++;
109 
110  }
111  while(offset%4 != 0)
112  {
113  fread (&dummy,PIXEL_size,1,fichier);
114  offset ++;
115  }
116  }
117  }else
118  {
119  ret = ERR_HEADER_TOO_BIG;
120  }
121  }else
122  {
123  ret = ERR_TOO_HEIGHT;
124  }
125  }else
126  {
127  ret = ERR_TOO_WIDTH;
128  }
129  }else
130  {
131  ret = ERR_NOT_24bit;
132  }
133  }else
134  {
135  ret = ERR_NOT_WINBMP;
136  }
137  fclose(fichier);
138  }else
139  {
140  ret = ERR_NOFILE;
141  }
142 
143  return ret;
144 };
145 
146 
147 /********************************************/
155 CPU_CHAR write_img(CPU_CHAR * imgname, t_img * img)
156 {
157  CPU_CHAR ret = ERR_NONE;
158  CPU_INT16S i,j;
159  CPU_INT32U offset;
160  FILE* fichier = NULL;
161  fichier = fopen((const char *)imgname, "wb");
162  if(fichier != NULL)
163  {
164  //Write Header
165  for(i=0;i<img->FileHeader_size;i++)
166  {
167  fputc(img->FileHeader[i], fichier);
168  }
169 
170  //Write color
171  offset = 0;
172  for(i=0;i< img->he ;i++)
173  {
174  for(j=0 ; j< img->wi ;j++)
175  {
176  fputc(img->Blue[i][j], fichier);
177  fputc(img->Green[i][j], fichier);
178  fputc(img->Red[i][j], fichier);
179  offset ++;
180 
181  }
182  while(offset%4 != 0)
183  {
184  fputc(0, fichier);
185  offset ++;
186  }
187  }
188  fclose(fichier);
189  }
190  else
191  {
192  ret = ERR_OPENFILE;
193  }
194  return ret;
195 }
196 
197 
198 /********************************************/
206 CPU_CHAR copy_img( t_img * imgsrc, t_img * imgdest)
207 {
208  CPU_CHAR ret = ERR_NONE;
209  CPU_INT16S i,i_img,j_img;
210 
211 //Write Header
212  for(i=0;i<imgsrc->FileHeader_size;i++)
213  {
214  imgdest->FileHeader[i] = imgsrc->FileHeader[i];
215  }
216  imgdest->signature = imgsrc->signature;
217  imgdest->depth = imgsrc->depth;
218  imgdest->wi = imgsrc->wi;
219  imgdest->he = imgsrc->he;
220  imgdest->FileHeader_size = imgsrc->FileHeader_size;
221 
222 
223  for(i_img=0;i_img< (imgsrc->he ) ;i_img++)
224  {
225 
226  for(j_img=0 ; (j_img< imgsrc->wi );j_img++)
227  {
228  imgdest->Red[i_img][j_img] = imgsrc->Red[i_img][j_img];
229  imgdest->Green[i_img][j_img] = imgsrc->Green[i_img][j_img];
230  imgdest->Blue[i_img][j_img] = imgsrc->Blue[i_img][j_img];
231  }
232  }
233  return ret;
234 }
#define ERR_HEADER_TOO_BIG
Definition: llips_general.h:52
#define DEPTH_24bit
Definition: llips_general.h:28
#define ERR_NOT_WINBMP
Definition: llips_general.h:48
#define MAX_HEIGHT
Definition: llips_cfg.h:15
#define OFFSET_SIGN
Definition: llips_general.h:13
#define OFFSET_SIGN_size
Definition: llips_general.h:19
void CPU_VOID
Definition: llips_type.h:20
CPU_INT16U signature
Definition: llips_type.h:35
#define ERR_NONE
Definition: llips_general.h:46
#define OFFSET_IMG_HEIGHT
Definition: llips_general.h:16
CPU_INT08U FileHeader_size
Definition: llips_type.h:44
#define ERR_TOO_HEIGHT
Definition: llips_general.h:51
#define ERR_TOO_WIDTH
Definition: llips_general.h:50
#define ERR_NOFILE
Definition: llips_general.h:47
#define OFFSET_IMG_DEPTH_size
Definition: llips_general.h:23
#define MAX_HEADER_size
Definition: llips_cfg.h:16
CPU_INT16U depth
Definition: llips_type.h:36
#define PIXEL_size
Definition: llips_general.h:24
#define WIN_BMP
Definition: llips_general.h:26
CPU_CHAR write_img(CPU_CHAR *imgname, t_img *img)
Write an image buffer into a BMP file.
Definition: llips_io.c:155
Global include file for LLIPS.
#define ERR_NOT_24bit
Definition: llips_general.h:49
#define MAX_WIDTH
Definition: llips_cfg.h:14
#define OFFSET_IMG_P_DATA
Definition: llips_general.h:14
CPU_INT32U wi
Definition: llips_type.h:37
unsigned char CPU_CHAR
Definition: llips_type.h:21
CPU_CHAR copy_img(t_img *imgsrc, t_img *imgdest)
Copy source image into destination image.
Definition: llips_io.c:206
#define OFFSET_IMG_DATA_WIN24bit
Definition: llips_general.h:27
CPU_INT08U ** Red
Definition: llips_type.h:41
#define OFFSET_IMG_HEIGHT_size
Definition: llips_general.h:22
CPU_INT08U ** createTableINT08U(CPU_INT16S nbLin, CPU_INT16S nbCol)
Allocate memory for a CPU_INT08U 2D table.
signed short CPU_INT16S
Definition: llips_type.h:26
unsigned long CPU_INT32U
Definition: llips_type.h:27
#define OFFSET_IMG_DEPTH
Definition: llips_general.h:17
CPU_INT08U FileHeader[MAX_HEADER_size]
Definition: llips_type.h:43
CPU_VOID init_img(t_img *img)
Initialize and allocate memory for a potentional load_img()
Definition: llips_io.c:29
#define OFFSET_IMG_P_DATA_size
Definition: llips_general.h:20
CPU_INT08U ** Blue
Definition: llips_type.h:39
CPU_INT08U ** Green
Definition: llips_type.h:40
CPU_CHAR load_img(CPU_CHAR *imgname, t_img *img)
Load a bmp file into an organized memory area.
Definition: llips_io.c:49
#define OFFSET_IMG_WIDTH
Definition: llips_general.h:15
CPU_INT32U he
Definition: llips_type.h:38
#define OFFSET_IMG_WIDTH_size
Definition: llips_general.h:21
#define ERR_OPENFILE
Definition: llips_general.h:53