📄 sample.c
字号:
//>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>//
// FILE NAME : SAMPLE.C (4A) //
// DESCRIPTION: A sample program used to illustrate how //
// to write code for linking with UNISCAN //
// scanner driver in DOS. //
// COMPILER : Microsoft C/C++ (VC++1.5) //
// AUTHOR : Daohong Kan, Tsinghua Unisplendour Group //
// DATE : Jan-23-1996 //
// VERSION : 1.0 //
//>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>//
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <bios.h>
#include <dos.h>
#include <graph.h>
#include <malloc.h>
#include "sample.h"
#define VGACtlPort 0x3ce // VGA Graph Controller Port
#define VGADataPort 0x3cf
#define SequenCtlPort 0x3c4
#define SequenDataPort 0x3c5
#define VGASEG 0xA000
#define IT_BIN 0 // Color Code
#define IT_GRAY 1
#define IT_COLOR 2
#define GETDEVINFO 1 // Function Code
#define SETPARAMETERS 2
#define STARTSCAN 3
#define STOPSCAN 4
#define GETIMAGESTATUS 5
#define BUF_LEN (1024*20)
struct Dev_Info dev_info;
struct SetDevParam set_dev;
struct ScanParam sScan;
struct GetImageStatus sImage;
struct CommonParam sAbort;
PB_buffer_list buffer_list;
unsigned char img_buf[BUF_LEN], mem_buf[BUF_LEN];
unsigned char dither[4][4] = // Dither pattern
{ { 0, 8, 2, 10 },{ 12, 4, 14, 6 },
{ 3, 11, 1, 9 },{ 15, 7, 13, 5 } };
char model_name[10];
char tmp_file[] = ".\\uniscan.4a$";
float frm[6]={ 0, 0, 0, 0, 0, 0 };
int image_type, scanner_type;
///////////////////////////////////////////////////////////
// Main program, given file name and a port address, //
// is to control the whole scanning process. //
///////////////////////////////////////////////////////////
void main(void)
{
int resolution;
_clearscreen( _GCLEARSCREEN );
_setbkcolor(1);
_settextposition(1,26);
_settextcolor(7);
_outtext(" uniscan controller \n" );
_settextposition(3,1);
_outtext(" scaner type: \n");
if (SCN_INIT() != 0)
{
printf("[Test]: initialization fails!");
exit(0);
}
printf("[Test]: initialization OK!\n");
_settextposition(8,1);
_outtext(" Please enter parameters: \n");
printf("resoulution=");
scanf("%d",&resolution);
if (SCN_SETPARAM(resolution) != 0)
{
printf("[Test]: Set spi driver parameters fails!");
SCN_ABORT();
exit(0);
}
printf("\n[Test]: Set scanning parameters OK!\n");
if (SCN_START() !=0)
{
printf("[Test]: Start scan fails!\n");
SCN_ABORT();
exit(0);
}
_settextposition(20,1);
_outtext("Scanning lines : ");
printf("100%%");
_settextposition(21,1);
printf("[Test]: Start scanning OK!\n");
show_image();
SCN_ABORT();
exit(0);
}
// >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
// Giving the port address of scanner interface card >>
// in order to do the initialization of scanner. >>
// >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
SCN_INIT()
{
int i, j;
if (SCNOpen() < 0)
{
printf("Open driver fail !!\n");
return(-1);
}
memset(dev_info.attention,0,96);
dev_info.Comm.func_code = GETDEVINFO;
SCNCall((char far *) &dev_info);
SCNClose();
printf("card_id=[%s] IO_port=%x\n",dev_info.card_id,dev_info.BUF_SEG);
dev_info.attention[15] = '\0';
dev_info.attention[32] = '\0';
if (strcmp(&dev_info.attention[36], "UNISCAN") == 0)
{
printf("name1=[%s] model=[%s] type=%x source=%x\n",
&dev_info.attention[36], &dev_info.attention[44],
(unsigned char)dev_info.attention[57],
(unsigned char)dev_info.attention[63]);
strcpy(model_name, &dev_info.attention[44]);
}
else
{
printf("name1=[%s] model=[%s] type=%x source=%x\n",
&dev_info.attention[8], &dev_info.attention[16],
(unsigned char)dev_info.attention[57],
(unsigned char)dev_info.attention[63]);
strcpy(model_name, &dev_info.attention[16]);
}
return(0);
}
// >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
// Set scanning parameters >>
// >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
SCN_SETPARAM(int resolution)
{
int i, j, color, type;
if (SCNOpen() < 0) return(-1);
set_dev.length_unit = 0; // 0:inch (unit:papar_length)
set_dev.paper_length = 14; // Max. length 14 inches
set_dev.frame_unit = 1; // 1:pixel ; 0:(inch*3/4)
set_dev.background_unit = 0;
set_dev.background_win_no = 0;
set_dev.contrast = 1;
set_dev.brightness = 1;
set_dev.velocity = 4; // 0->4, fast->slow
set_dev.resolution = resolution;
set_dev.grain_built_in = 0;
set_dev.grain = 0;
set_dev.gamma = 0;
set_dev.gammasize = 0;
set_dev.Comm.func_code = SETPARAMETERS;
set_dev.LD_DPI = 0x00;
printf("color (1:B/W 2:gray 3:color) : ");
scanf("%f",&frm[4]);
switch((int)frm[4])
{
case 1 : set_dev.background_f_code = 'T'; // 'T': B/W
set_dev.filter = 0; // 0:gray or B/W
break;
case 2 : set_dev.background_f_code = 0; // 'T': B/W
set_dev.filter = 0; // 0:gray or B/W
break;
case 3 : set_dev.background_f_code = 0; // 0: gray or color
set_dev.filter = 1; // 1:color
break;
}
printf("scansource (1:flatbed 2:ADF) : ");
scanf("%f",&frm[5]);
if ((int)frm[5])
{
set_dev.scansource = 0x00; // 0x00= Flatbed
printf("enter left-top and right-bottom (inch): "); // unit:inch
scanf("%f %f %f %f",&frm[0],&frm[1],&frm[2],&frm[3]);
}
else
{
if (dev_info.attention[63] !=12)
{
printf("ADF or paper is not ready !! \n\n");
return (-1);
}
set_dev.scansource = 0x02; // 0x02= ADF
frm[0]=frm[1]=0;
frm[2]=8.25; frm[3]=11.2;
}
printf("\nPlease waitting.......");
_settextposition(15,1);
if (frm[2]>8.26) frm[2]=8.26; //right-bottom x,y<8.26 ,11.3
if (frm[3]>11.3) frm[3]=11.3;
if (strcmp(model_name,"4A")==0) set_dev.model = 600;
else if (strcmp(model_name,"4B")==0) set_dev.model = 800;
else if (strcmp(model_name,"4C")==0) set_dev.model = 1200;
else if (strcmp(model_name,"4X")==0) set_dev.model = 600;
for (i = 0; i < 4 ; i++)
set_dev.frame[i] = (int)(frm[i]*set_dev.model);
memset(set_dev.background,0,16 );
memset(set_dev.grain_code,0,256);
memset(set_dev.gammacode ,0,256);
SCNCall((char far *) &set_dev);
SCNClose();
printf("Set parameters return: %d\n", set_dev.Comm.error_code);
return(set_dev.Comm.error_code);
}
// >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
// When selecting OK, thus user can start scanner and >>
// get the digitizied image back. >>
// >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
SCN_START()
{
int lines, i, j, get_lines, MaxValue, Pk_res, IsAZ_LD;
int r_index, g_index, b_index, rLD, gLD, bLD, Qk_R, Qk_G, Qk_B;
FILE *fp;
if (SCNOpen() < 0) return -1;
sImage.Comm.func_code = GETIMAGESTATUS;
SCNCall((char far *) &sImage);
printf("total lines=%d width=%d\n",sImage.line_no,sImage.line_width);
buffer_list.buf_size = BUF_LEN;
buffer_list.buf_line = (int)(BUF_LEN / sImage.line_width);
buffer_list.buf_address = (long)img_buf;
printf("lines per buffer = %d\n", buffer_list.buf_line);
fp = fopen(tmp_file, "wb");
switch ((int)frm[4])
{
case 1 : image_type = IT_BIN;
i = sImage.line_width ;
break;
case 2 : image_type = IT_GRAY ;
i = sImage.line_width ;
break;
default: image_type = IT_COLOR ;
i = sImage.line_width/3 ;
}
fwrite(&image_type, 2, 1, fp);
fwrite(&i, 2, 1, fp);
fwrite(&sImage.line_no, 2, 1, fp);
IsAZ_LD = 0;
if (image_type == IT_COLOR && set_dev.LD_DPI != 0xffff)
{
IsAZ_LD = 1;
Pk_res = set_dev.resolution ;
if ( set_dev.LD_DPI == 0x0000)
{
MaxValue = set_dev.model ;
Qk_R = Qk_G = Qk_B = set_dev.model ;
Pk_res = (Pk_res<=(set_dev.model/2) ? Pk_res*2 : Pk_res ) ;
}
else
{
MaxValue = set_dev.model ;
Qk_R = Qk_G = Qk_B = set_dev.model ;
Pk_res = Pk_res * set_dev.LD_DPI ;
} }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -