1 /****************************************************************
2  * ainput.c
3  *
4  * Copyright (C) 1995-2002 Klaus Ehrenfried
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
19  *
20  *************************************************************************/
21 
22 #include <stdio.h>
23 #include <sys/types.h>
24 #include <sys/stat.h>
25 #include <memory.h>
26 #include <stdlib.h>
27 
28 #include "apro.h"
29 #define EMPTY_TYPE   -1
30 #define FBM_TYPE     1000
31 #define UNKNOWN_TYPE 0
32 const char fbm_magic[]="bitmap";
33 
34 static FILE *open_xx_file(char *file_name);
35 
36 static char *command;
37 static int max_cmd_len = -1;
38 static int popen_flag;
39 
40 /****************************************************************
41  * type_detector
42  ****************************************************************/
43 
detect_type(FILE * fp)44 static int detect_type(FILE *fp)
45 {
46   int c, i;
47 
48   c=getc(fp);
49   if (c == 'P')
50     {
51       c=getc(fp);
52       switch (c)
53 	{
54 	case '1': return(1);
55 	case '2': return(2);
56 	case '3': return(3);
57 	case '4': return(4);
58 	case '5': return(5);
59 	case '6': return(6);
60 	default: break;
61 	}
62     }
63   else if (c == '%')
64     {
65       for (i=0; i < 7; i++)
66 	{
67 	  c = getc(fp);
68 	  if (c != fbm_magic[i]) return(UNKNOWN_TYPE);
69 	}
70       return(FBM_TYPE);
71     }
72   else if (c == EOF)
73     {
74       fprintf(stderr,"Empty data stream\n");
75       return(EMPTY_TYPE);
76     }
77 
78   return(UNKNOWN_TYPE);
79 }
80 
81 /****************************************************************
82  * check_exist
83  ****************************************************************/
84 
check_exist(char * file_name)85 int check_exist(char *file_name)
86 {
87   struct stat statbuf;
88 
89   if (stat(file_name, &statbuf) == 0)
90     {
91       return(1);  /* gibt's */
92     }
93 
94   return(0);  /* ist nicht da */
95 }
96 
97 /****************************************************************
98  * free_pms
99  ****************************************************************/
100 
free_pms(PMS * image)101 int free_pms(PMS *image)
102 {
103   if (image->pixels != NULL)
104     {
105       free(image->pixels);
106       image->pixels=NULL;
107     }
108   return(1);
109 }
110 
111 /****************************************************************
112  * read_image
113  ****************************************************************/
114 
read_image(PMS * image,char * file_name)115 int read_image(PMS *image, char *file_name)
116 {
117   FILE *fpin, *fopen();
118   int k, type;
119 
120   k=0;
121   popen_flag = 0;
122 
123   if ((fpin = open_xx_file(file_name)) == NULL)
124     { return(0);}
125 
126   type = detect_type(fpin);
127   if (type == EMPTY_TYPE) return(0);
128   if (type == UNKNOWN_TYPE)
129     {
130       fprintf(stderr,"No ppm/pgm/pbm or fbm format\n");
131       return(0);
132     }
133   if (type == FBM_TYPE)
134     {
135       k=get_fbm_data(image,fpin);
136     }
137   else
138     {
139       k=get_ppm_data(image,fpin,type);
140     }
141 
142   if (popen_flag == 0)
143     { fclose(fpin);}
144   else
145     { pclose(fpin);}
146   return(k);
147 }
148 
149 /****************************************************************
150  * read_raw
151  ****************************************************************/
152 
read_raw(FILE * fp,char * buffer,int len)153 int read_raw(FILE *fp, char *buffer, int len)
154 {
155   int k;
156 
157   if ((k=fread(buffer, 1, len, fp)) != len)
158     {
159       fprintf(stderr,"DATA INCOMPLETE: Can't read all pixels\n");
160       fprintf(stderr,"Expected %d bytes and got only %d\n",len, k);
161     }
162   return(1);
163 }
164 
165 /****************************************************************
166  * open_xx_file
167  ****************************************************************/
168 
open_xx_file(char * file_name)169 static FILE *open_xx_file(char *file_name)
170 {
171   FILE *fopen(), *fp;
172   int cmd_len, type;
173 
174   if ((filter_flag == 0) || (test_magic_flag == 1))
175     {
176       if ((fp = fopen(file_name, "r")) == NULL)
177 	{
178 	  fprintf(stderr,"Can't open file '%s'\n",file_name);
179 	  return(NULL);
180 	}
181       if (filter_flag == 0) return(fp);
182       type = detect_type(fp);
183       if (type == EMPTY_TYPE) return(NULL);
184       if (type == UNKNOWN_TYPE)
185 	{
186 	  fclose(fp);
187 	}
188       else
189 	{
190 	  rewind(fp);
191 	  return(fp);
192 	}
193     }
194 
195   cmd_len = strlen(filter_name);
196   if (cmd_len == 0)
197     {
198       fprintf(stderr,"No filter specified\n");
199       return(NULL);
200     }
201   cmd_len += strlen(file_name);
202   cmd_len += 10; /* for extra chars */
203 
204   if (cmd_len >= max_cmd_len)
205     {
206       cmd_len += 10;
207       if (max_cmd_len > 0) free(command);
208       if ((command = (char *) malloc(cmd_len)) == NULL)
209 	{
210 	  fprintf(stderr,"Can't allocate %d bytes\n",cmd_len);
211 	  return(NULL);
212 	}
213       max_cmd_len = cmd_len;
214     }
215 
216   if (filter_flag == 1)
217     { sprintf(command,"%s < %s",filter_name,file_name);}
218   else
219     { sprintf(command,"%s %s",filter_name,file_name);}
220 
221   if (verbose_flag > 0)
222     fprintf(stdout," .... %s\n",command);
223 
224   if ((fp = popen(command, "r")) == NULL)
225     {
226       fprintf(stderr,"Can't open file via pipe '%s'\n",command);
227       return(NULL);
228     }
229   popen_flag = 1;
230 
231   return(fp);
232 }
233 
234 /**** -- FIN -- *****/
235