sane-project-website/old-archive/1998-11/0080.html

202 wiersze
6.5 KiB
HTML

<!-- received="Tue Nov 10 01:14:40 1998 PST" -->
<!-- sent="Tue, 10 Nov 1998 10:13:23 +0100 (MET)" -->
<!-- name="Stefan Illy" -->
<!-- email="stefan.illy@itp.fzk.de" -->
<!-- subject="Output filters for xscanimage: a simple attempt" -->
<!-- id="199811100913.KAA25020@itpgyro1.fzk.de" -->
<!-- inreplyto="" -->
<title>sane-devel: Output filters for xscanimage: a simple attempt</title>
<h1>Output filters for xscanimage: a simple attempt</h1>
<b>Stefan Illy</b> (<a href="mailto:stefan.illy@itp.fzk.de"><i>stefan.illy@itp.fzk.de</i></a>)<br>
<i>Tue, 10 Nov 1998 10:13:23 +0100 (MET)</i>
<p>
<ul>
<li> <b>Messages sorted by:</b> <a href="date.html#80">[ date ]</a><a href="index.html#80">[ thread ]</a><a href="subject.html#80">[ subject ]</a><a href="author.html#80">[ author ]</a>
<!-- next="start" -->
<li> <b>Next message:</b> <a href="0081.html">Matti Koskimies: "Re: Sym53c416 driver problem"</a>
<li> <b>Previous message:</b> <a href="0079.html">Andre Couture: "Re: Scanner"</a>
<!-- nextthread="start" -->
<!-- reply="end" -->
</ul>
<!-- body="start" -->
Hello,<br>
<p>
I wondered why there is no possibility to use other output formats than<br>
PNM in xscanimage. So I decided to implement output filters for other<br>
formats in a simple way without changing too much of xscanimage.<br>
<p>
This was done by replacing the `fopen' and `fclose' commands in<br>
xscanimage.c by functions called `pfopen' and `pfclose'. Both functions<br>
are implemented in `pfopen.c' appended below. Please look at the<br>
comments in this file for more details, it should be not too complicated.<br>
<p>
The conversion can be done by `netpbm' filters, ImageMagick's `convert'<br>
command or `cjpeg'. Compression using `gzip' is also possible.<br>
<p>
If you want to test it, replace `fopen' and `fclose' in xscanimage.c<br>
by `pfopen' and `pfclose', respectively. Don't forget to provide<br>
prototypes for both functions. Provide pfopen.c in the `frontend' path<br>
and add an entry for pfopen.o in the corresponding Makefile.<br>
<p>
I tested it both with the 0.74 and the pre-1.00 version and had no<br>
problems (on Linux, kernel 2.0.35, libc5).<br>
<p>
<p>
Please tell me what you think about this attempt, if it worked for you<br>
and what could be done in a better way.<br>
<p>
Stefan<br>
<p>
----8&lt;-------8&lt;-------8&lt;-------8&lt;-------8&lt;-------8&lt;-------8&lt;-------8&lt;---<br>
/* <br>
* pfopen.c: A simple way to provide output filters for xscanimage.<br>
*<br>
* Author: Stefan Illy &lt;<a href="mailto:stefan.illy@itp.fzk.de">stefan.illy@itp.fzk.de</a>&gt;<br>
*<br>
*/<br>
<p>
#include &lt;stdio.h&gt;<br>
#include &lt;string.h&gt;<br>
#include &lt;sys/stat.h&gt;<br>
#include &lt;unistd.h&gt;<br>
<p>
#define STRLEN 256<br>
<p>
<p>
/*<br>
* List of output filters.<br>
* This should be provided in a configuration file (e.g. `filters.conf')<br>
*/<br>
char* filter_list[] = {<br>
".eps: convert pnm:- eps2:-",<br>
".eps.gz: convert pnm:- eps2:- | gzip",<br>
".ps: convert pnm:- ps2:-",<br>
".ps.gz: convert pnm:- ps2:- | gzip",<br>
".jpg: cjpeg -quality 75",<br>
".png: pnmtopng",<br>
".pnm.gz: gzip",<br>
NULL};<br>
<p>
<p>
/*<br>
* Returns one if `str' ends with `expr', otherwise zero.<br>
*/<br>
int<br>
strends(const char* str, const char* expr)<br>
{<br>
char* pos;<br>
if ((pos = strstr(str, expr)) == NULL) return 0;<br>
if (strlen(str) - strlen(expr) == (int)(pos - str)) return 1; else return 0;<br>
}<br>
<p>
<p>
/*<br>
* pfopen: replacement for fopen in `xscanimage.c'.<br>
* <br>
* If `path' starts with the pipe symbol `|' or the extension of the<br>
* file name is in `filter_list', popen will be used instead of<br>
* fopen.<br>
* <br>
*/<br>
FILE*<br>
pfopen(const char* path, char* mode)<br>
{<br>
FILE* stream;<br>
char filter[STRLEN] = "";<br>
<p>
if (path[0] == '|') /* Output filter directly specified. */<br>
{<br>
strcpy(filter, &amp;path[1]);<br>
}<br>
else /* Search filter_list for extension. */<br>
{<br>
int i;<br>
for (i = 0; filter_list[i] != NULL; i++)<br>
{<br>
char* pos;<br>
if ((pos = strchr(filter_list[i], ':')) != NULL)<br>
{<br>
char ext[STRLEN] = "";<br>
strncpy(ext, filter_list[i], (int)(pos - filter_list[i]));<br>
if (strends(path, ext))<br>
{<br>
strcpy(filter, pos + 1);<br>
strcat(filter, " &gt; ");<br>
strcat(filter, path);<br>
break;<br>
}<br>
}<br>
}<br>
}<br>
<p>
if (strcmp(filter, "") == 0)<br>
{<br>
fprintf(stderr, "pfopen: using fopen(%s).\n", path);<br>
stream = fopen(path, mode);<br>
}<br>
else<br>
{<br>
fprintf(stderr, "pfopen: using popen(%s).\n", filter);<br>
stream = popen(filter, mode);<br>
}<br>
<p>
return stream;<br>
}<br>
<p>
<p>
/*<br>
* pfclose: replacement for fclose in `xscanimage.c'.<br>
*<br>
* Uses fstat to determine if `stream' belongs to a regular file.<br>
* If this is not the case, pclose will be used instead of<br>
* fclose.<br>
*<br>
* Maybe there are better (and more secure) methods to do this.<br>
*/<br>
int<br>
pfclose(FILE* stream)<br>
{<br>
struct stat statinfo;<br>
<p>
if (fstat(fileno(stream), &amp;statinfo) != 0)<br>
{<br>
fprintf(stderr, "pfclose: fstat was not successful, using fclose.\n");<br>
return fclose(stream);<br>
}<br>
<p>
if (S_ISREG(statinfo.st_mode))<br>
{<br>
fprintf(stderr, "pfclose: S_ISREG==true, using fclose.\n");<br>
return fclose(stream);<br>
}<br>
else<br>
{<br>
fprintf(stderr, "pfclose: S_ISREG==false, using pclose.\n");<br>
return pclose(stream);<br>
}<br>
}<br>
----8&lt;-------8&lt;-------8&lt;-------8&lt;-------8&lt;-------8&lt;-------8&lt;-------8&lt;---<br>
<p>
<pre>
--
Stefan Illy | Institut fuer Technische Physik (ITP)
e-mail: <a href="mailto:stefan.illy@itp.fzk.de">stefan.illy@itp.fzk.de</a> | Forschungszentrum Karlsruhe GmbH
phone: (+49) 07247/82-4165 | Hermann-von-Helmholtz-Platz 1
fax: (+49) 07247/82-2849 | D-76344 Eggenstein-Leopoldshafen, Germany
<p>
<p>
<pre>
--
Source code, list archive, and docs: <a href="http://www.mostang.com/sane/">http://www.mostang.com/sane/</a>
To unsubscribe: echo unsubscribe sane-devel | mail <a href="mailto:majordomo@mostang.com">majordomo@mostang.com</a>
</pre>
<!-- body="end" -->
<p>
<ul>
<!-- next="start" -->
<li> <b>Next message:</b> <a href="0081.html">Matti Koskimies: "Re: Sym53c416 driver problem"</a>
<li> <b>Previous message:</b> <a href="0079.html">Andre Couture: "Re: Scanner"</a>
<!-- nextthread="start" -->
<!-- reply="end" -->
</ul>