lisgo

package module
v0.0.0-...-3f6d7a2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 3, 2023 License: LGPL-3.0 Imports: 15 Imported by: 0

README

lisgo

Go bindings for libinsane scanning library (OpenPaperwork project).

lisgo library is potentially cross-platform, but tested only on Windows yet.

How to compile on Windows

Instructions for compiling 32-bit version.

  1. Install MSYS2 and everything for compiling 32-bit programs. Run it in MINGW32 shell. This is an excerpt from libinsane documentation.

    pacman -Syuu
    pacman -S \
        make \
        mingw-w64-i686-cunit \
        mingw-w64-i686-doxygen \
        mingw-w64-i686-gcc \
        mingw-w64-i686-gobject-introspection \
        mingw-w64-i686-meson \
        mingw-w64-i686-python3-gobject \
        mingw-w64-i686-vala
    

    If you plan on compiling your program as a 64bits executable, you just have to replace all the i686 by x86_64 and run it in MINGW64 shell.

  2. Download libinsane sources

    wget -c https://gitlab.gnome.org/World/OpenPaperwork/libinsane/-/archive/1.0.10/libinsane-1.0.10.tar.gz -O - | tar -xz
    mv libinsane-* libinsane
    
  3. To compile libinsane as a static library, change line #88 in subprojects\libinsane\src\meson.build from libinsane = library to libinsane = static_library

    (cd libinsane && sed -i 's/libinsane = library/libinsane = static_library/g' subprojects/libinsane/src/meson.build)
    
  4. Build libinsane

    (cd libinsane && make PREFIX=/mingw64)
    
  5. Set CGO options

    export LIBINSANE_DIR=`cygpath -aw libinsane`
    export CGO_CFLAGS="-I${LIBINSANE_DIR}/subprojects/libinsane/include"
    export CGO_LDFLAGS="-L${LIBINSANE_DIR}/build/subprojects/libinsane/src/ -static -linsane -lpthread -lsystre -lintl -ltre -liconv -lregex -lole32 -loleaut32 -luuid"
    
  6. Build

    go get .
    go build -o bin/lisgo.exe cmd/lisgo/main.go
    

How to compile on Ubuntu

  1. Install dependencies
    sudo apt get update
    sudo apt install build-essential libinsane-dev libsane-dev
    
  2. Set CGO options
    export CGO_LDFLAGS="-linsane"
    
  3. Build
    go get .
    go build -o bin/lisgo cmd/lisgo/main.go
    

lisgo.exe command-line utility

This project includes lisgo.exe command-line utility. It illustrates using of the library. Please refer to cmd\lisgo\lisgo.go for examples. lisgo.exe could be compiled as 32-bit or 64-bit program. Usually 32-bit is preferred way of using it, because most of Twain drivers are 32-bit only.

Currently lisgo.exe can:

  • List available scanners.
lisgo32.exe print-scanners

Qt: Untested Windows version 10.0 detected!
---------------------------------------------------------------------
Device Id: twain:TWAIN Working Group:TWAIN2 FreeImage Software Scanner

Vendor: TWAIN Working Group
Model: TWAIN2 FreeImage Software Scanner
Qt: Untested Windows version 10.0 detected!
Qt: Untested Windows version 10.0 detected!
Paper source: flatbed
Paper source: feeder
---------------------------------------------------------------------
Device Id: twain:Brother Industries, Ltd.:TW-Brother MFC-L3770CDW LAN

Vendor: Brother Industries, Ltd.
Model: TW-Brother MFC-L3770CDW LAN
   ⨯ libinsane error           error=../subprojects/libinsane/src/bases/twain/twain.c:L1551(twain_simple_set_value): Brother Industries, Ltd.:TW-Brother MFC-L3770CDW LAN->simple_set_value(supported_sizes): Failed to get value: 0x60000002, LibInsane internal error: Unknown error reported by backend (please report !)
   ⨯ libinsane error           error=../subprojects/libinsane/src/workarounds/cache.c:L229(cache_set_value): supported_sizes->set_value() failed: 0x60000002, LibInsane internal error: Unknown error reported by backend (please report !)
Paper source: feeder
Paper source: flatbed
  • Print specific scanner and paper source options.
lisgo32.exe print-options -d "twain:Brother Industries, Ltd.:TW-Brother MFC-L3770CDW LAN" -s feeder

------- transfer_count ------
transfer_count (transfer_count;transfer_count)
Caps: 8 [LisCapSwSelect]
Type: 1 (Integer)
Units: 0 (None)
Constraint: List (-1)
Value: -1

------- compression ------
compression (compression;compression)
Caps: 8 [LisCapSwSelect]
Type: 3 (String)
Units: 0 (None)
Constraint: List (none)
Value: none

------- mode ------
mode (mode;mode)
Caps: 8 [LisCapSwSelect]
Type: 3 (String)
Units: 0 (None)
Constraint: List (LineArt,Gray,Color)
Value: Color

------- units ------
units (units;units)
Caps: 8 [LisCapSwSelect]
Type: 3 (String)
Units: 0 (None)
Constraint: List (inches,centimeters,pixels)
Value: inches
...
  • Scan to pdf, jpeg and png format. Default format is pdf, resulting file name is result.pdf.
lisgo32.exe scan -d "twain:Brother Industries, Ltd.:TW-Brother MFC-L3770CDW LAN" -s feeder

You can use -o flag to set scanner options. Flag -o can be specified more than once to set several options. For example, for duplex gray-scale scanning, you can issue this command:

lisgo32.exe scan -o mode=Gray -o duplex_enabled=true -d "twain:Brother Industries, Ltd.:TW-Brother MFC-L3770CDW LAN" -s feeder`
  • Print help page for available commands.
lisgo32.exe scan

Scan using specified scanner and paper source. Output file will have name like 'page1.png, page2.jpg or result.pdf' depending on -f option value.

Options:
  -d string
        id of the scanner, mandatory
  -f string
        output file format [jpg|png|pdf] (default "pdf")
  -o value
        try to set specified option before scan.
        Format:
        -o name=value :  set option with [name] to [value]
        -o name= : pass empty string as value of the option
        This flag can appear multiple times: -o name1=value1 -o name2=value2
  -s string
        paper source, mandatory
  -v    show debug messages

Documentation

Index

Constants

View Source
const (
	LisLogLvlMin     uint32 = 0
	LisLogLvlDebug   uint32 = LisLogLvlMin
	LisLogLvlInfo    uint32 = 1
	LisLogLvlWarning uint32 = 2
	LisLogLvlError   uint32 = 3
	LisLogLvlMax     uint32 = LisLogLvlError
)

Log levels from libinsane

View Source
const (
	LisTypeBool = iota
	LisTypeInteger
	LisTypeDouble
	LisTypeString
	LisTypeImageFormat
)

lis_value_type enum

View Source
const (
	LisConstraintNone = iota
	LisConstraintRange
	LisConstraintList
)

lis_constraint_type enum

View Source
const (
	LisCapEmulated  = 1 << 0 // emulated option (either by LibInsane or backend)
	LisCapAutomatic = 1 << 1 // can be set automatically by the driver
	LisCapHwSelect  = 1 << 2 // can be set by a hardware switch
	LisCapSwSelect  = 1 << 3 //  read/write ; can be set by software
	LisCapInactive  = 1 << 4 //requires another option to be set to a specific value to become active
)

capabilitites bit mask flags

View Source
const (

	/* Raw image, 24bits per pixel.
	*
	* For each pixel:
	* - 8bits for red,
	* - 8bits for green,
	* - 8bits for blue.
	*
	* No header, just pixels. */
	LisImgFormatRawRGB24 = iota
	LisImgFormatGrayScale8
	LisImgFormatBW1
	LisImgFormatBmp
	LisImgFormatCiff
	LisImgFormatExif
	LisImgFormatFlashPix
	LisImgFormatGif
	LisImgFormatJpeg
	LisImgFormatPng
	LisImgFormatIco
	LisImgFormatJpeg2k
	LisImgFormatJpeg2kx
	LisImgFormatmMemoryBmp // Windows BMP without header
	LisImgFormatPhotoCD
	LisImgFormatPict
	LisImgFormatTiff
)

* Image format. * * Used to defined the type of content that will be returned by \ref lis_scan_session.scan_read() .

View Source
const (
	LisUnitNone = iota
	LisUnitPixel
	LisUnitBit
	LisUnitMM
	LisUnitDPI
	LisUnitPercent
	LisUnitMicrosecond
)

enum lis_unit

View Source
const (
	LisOk uint32 = 0
)

Lis errors

View Source
const (
	ScanSessionCBufferSize = 1024 * 1024 //1MB
)

mostly buffer sizes here

Variables

This section is empty.

Functions

func New

func New() (*lisgo, error)

New creates new instances of lis_api using lis_safe_bet

func SetLisLogLevel

func SetLisLogLevel(lvl uint32)

SetLogLevel sets min log level

Types

type BmpHeader

type BmpHeader struct {
	Magic        uint16
	FileSize     uint32
	Unused       uint32
	OffsetToData uint32
	HeaderSize   uint32
	//Width and Height are the width and height of the image in pixels, respectively. If Height is a positive number,
	//then the image is a "bottom-up" bitmap with the origin in the lower-left corner. If Height is a negative number,
	//then the image is a "top-down" bitmap with the origin in the upper-left corner. Width does not include any scan-line boundary padding.
	Width                uint32
	Height               int32
	NbColorPlanes        uint16
	NbBitsPerPixel       uint16
	Compression          uint32
	PixelDataSize        uint32
	HorizontalResolution uint32
	VerticalResolution   uint32
	NbColorsInPalette    uint32
	ImportantColors      uint32
}

BmpHeader represents a BMP version 3 header (54 bytes total)

func ReadBMPHeader

func ReadBMPHeader(r io.Reader) (*BmpHeader, []byte, error)

type CErrorProxy

type CErrorProxy struct {
	// contains filtered or unexported fields
}

CErrorProxy wraps errors in C code, the idea is to reuse pre-allocated buffer when calling C function proxies

func NewErrProxy

func NewErrProxy() *CErrorProxy

NewErrProxy allocates internal *char buffer

func (*CErrorProxy) Clear

func (e *CErrorProxy) Clear()

Clear prepares CError for future use

func (*CErrorProxy) ErrNum

func (e *CErrorProxy) ErrNum() uint32

ErrNum returns number of error which we've got from C-code

func (*CErrorProxy) Error

func (e *CErrorProxy) Error() string

func (*CErrorProxy) FreeBuffer

func (e *CErrorProxy) FreeBuffer()

FreeBuffer releases allocated memory

func (*CErrorProxy) GetProxy

func (e *CErrorProxy) GetProxy() *C.struct_error_proxy

GetProxy returns address of internal struct error_proxy

type ImageBmpBw

type ImageBmpBw struct {
	// contains filtered or unexported fields
}

ImageBmpBw represents a black-N-white image with 1 bit per pixel

func NewBmpBwImage

func NewBmpBwImage(data []byte, header *BmpHeader) *ImageBmpBw

NewBmpBwImage constructs new object

func (*ImageBmpBw) At

func (i *ImageBmpBw) At(x, y int) color.Color

At returns the color of the pixel at (x, y).

func (*ImageBmpBw) Bounds

func (i *ImageBmpBw) Bounds() image.Rectangle

Bounds returns the domain for which At can return non-zero color. The bounds do not necessarily contain the point (0, 0).

func (*ImageBmpBw) ColorIndexAt

func (i *ImageBmpBw) ColorIndexAt(x, y int) uint8

ColorIndexAt returns the color index of the pixel at (x, y).

func (*ImageBmpBw) ColorModel

func (i *ImageBmpBw) ColorModel() color.Model

ColorModel returns the Image's color model.

type ImageBmpGray

type ImageBmpGray struct {
	// contains filtered or unexported fields
}

func NewBmpGrayImage

func NewBmpGrayImage(data []byte, header *BmpHeader) *ImageBmpGray

func (*ImageBmpGray) At

func (i *ImageBmpGray) At(x, y int) color.Color

At returns the color of the pixel at (x, y).

func (*ImageBmpGray) Bounds

func (i *ImageBmpGray) Bounds() image.Rectangle

Bounds returns the domain for which At can return non-zero color. The bounds do not necessarily contain the point (0, 0).

func (*ImageBmpGray) ColorModel

func (i *ImageBmpGray) ColorModel() color.Model

ColorModel returns the Image's color model.

type LisValue

type LisValue struct {
	ValType     uint32
	BoolValue   bool
	IntValue    int
	DoubleValue float64
	StringValue string
	ImgFormat   C.enum_lis_img_format
}

LisValue is value of option

func NewValue

func NewValue(val *C.union_lis_value, typ C.enum_lis_value_type) *LisValue

NewValue constructs GO LisValue struct from lis_value C-struct

func (*LisValue) String

func (v *LisValue) String() string

Print prints LisValue depending on type

type OptionConstraint

type OptionConstraint struct {
	/*
		LIS_CONSTRAINT_NONE = 0,  No constraint
		LIS_CONSTRAINT_RANGE = 1, Range of values (integers only)
		LIS_CONSTRAINT_LIST = 2, The structure 'possible' contains a list of values
	*/
	ConstraintType int
	//If LIS_CONSTRAINT_RANGE.
	PossibleRange *ValueRange
	//If  LIS_CONSTRAINT_LIST.
	PossibleList ValueList
}

OptionConstraint describe restrictions defining the possible values for this option.

func NewConstraint

func NewConstraint(valType C.enum_lis_value_type, conType C.int, conPossible unsafe.Pointer) *OptionConstraint

NewConstraint costructs Go ValueConstraint struct from C-structs

type OptionDescriptor

type OptionDescriptor struct {
	//Name option name / identifier (ex: "source", "resolution", etc).
	Name string
	//Title Human readable title (usually in English).
	Title string
	//Desc Human readable description (usually in English).
	Desc string
	/* Capabilities is the option capabilities.
	*
	* Bit fields:
	* -  LIS_CAP_EMULATED
	* -  LIS_CAP_AUTOMATIC
	* -  LIS_CAP_HW_SELECT
	* -  LIS_CAP_SW_SELECT
	* -  LIS_CAP_INACTIVE */
	Capabilities int
	// Type of this option.
	ValueType C.enum_lis_value_type
	// Unit of this value. Only useful for integers and float.
	ValueUnit  C.enum_lis_unit
	Constraint *OptionConstraint
	// contains filtered or unexported fields
}

OptionDescriptor is a definition of an scan option

func (*OptionDescriptor) GetValue

func (o *OptionDescriptor) GetValue() (*LisValue, error)

GetValue obtains value of an option

func (*OptionDescriptor) IsReadable

func (o *OptionDescriptor) IsReadable() bool

IsReadable indicates that option can be read

func (*OptionDescriptor) IsWritable

func (o *OptionDescriptor) IsWritable() bool

IsWritable indicates that option can be written

func (*OptionDescriptor) String

func (o *OptionDescriptor) String() string

Print option using fmt

type PageReader

type PageReader struct {
	Width   int
	Height  int
	Format  uint32
	Session *ScanSession
	// contains filtered or unexported fields
}

PageReader represents a single page received from scanner

func NewPageReader

func NewPageReader(session *ScanSession, param *ScanParameters) *PageReader

NewPageReader converts data buffer to image object

func (*PageReader) GetImage

func (sb *PageReader) GetImage() (image.Image, error)

func (*PageReader) Read

func (sb *PageReader) Read(p []byte) (int, error)

Read portion of data from scanner into a buffer until the page is over. Optimal size of the buffer is ScanSessionCBufferSize.

func (*PageReader) WriteToFile

func (sb *PageReader) WriteToFile(name string, format string) error

func (*PageReader) WriteToJpeg

func (sb *PageReader) WriteToJpeg(name string) error

WriteToJpeg writes image to file

func (*PageReader) WriteToPng

func (sb *PageReader) WriteToPng(name string) error

WriteToPng writes image to file

type PaperSource

type PaperSource struct {
	Name string
	Kind C.enum_lis_item_type
	// contains filtered or unexported fields
}

PaperSource represents a source of paper for scan, i.e flatbed or automatic feeder

func (*PaperSource) IterateOptions

func (s *PaperSource) IterateOptions(f func(*OptionDescriptor) bool) error

IterateOptions iterates thru options of the paper source

func (*PaperSource) ScanStart

func (s *PaperSource) ScanStart() (*ScanSession, error)

ScanStart creates scanning session

func (*PaperSource) SetOption

func (s *PaperSource) SetOption(name string, val string) error

SetOption accepts string representation of value, converts it to the actual type of the option and sets it.

type ScanParameters

type ScanParameters struct {
	// contains filtered or unexported fields
}

ScanParameters holds the current scan session's parameters

func (*ScanParameters) Height

func (sp *ScanParameters) Height() int

Height of the image in pixels. warning This value is *not* guaranteed to be true when scanning.

func (*ScanParameters) ImageFormat

func (sp *ScanParameters) ImageFormat() uint32

ImageFormat is image format. This value is guaranteed to be true when scanning.

func (*ScanParameters) ImageFormatStr

func (sp *ScanParameters) ImageFormatStr() string

ImageFormatStr returns name of the image format

func (*ScanParameters) ImageSize

func (sp *ScanParameters) ImageSize() uint

ImageSize is estimated image size in bytes. Can be used to pre-allocate memory. This value is *not* guaranteed to be true when scanning.

func (*ScanParameters) String

func (sp *ScanParameters) String() string

func (*ScanParameters) Width

func (sp *ScanParameters) Width() int

Width of the image in pixels. This value is guaranteed to be true when scanning

type ScanSession

type ScanSession struct {
	// contains filtered or unexported fields
}

ScanSession is just a scan session

func (*ScanSession) Cancel

func (s *ScanSession) Cancel()

func (*ScanSession) Close

func (s *ScanSession) Close()

Close frees all allocated resources

func (*ScanSession) EndOfFeed

func (s *ScanSession) EndOfFeed() bool

EndOfFeed indicates that there are no more to read from scanner

func (*ScanSession) EndOfPage

func (s *ScanSession) EndOfPage() bool

EndOfPage indicates that the current page is over

func (*ScanSession) GetScanParameters

func (s *ScanSession) GetScanParameters() (*ScanParameters, error)

GetScanParameters returns scanning session parameters

func (*ScanSession) ScanRead

func (s *ScanSession) ScanRead() ([]byte, uint64, error)

ScanRead reads data from scanner

type Scanner

type Scanner struct {
	DeviceID string
	Vendor   string
	Model    string
	Type     string
	// contains filtered or unexported fields
}

Scanner is a descriptor of scanner

func (*Scanner) Close

func (d *Scanner) Close()

Close should be called after Open to release associated resources

func (*Scanner) GetPaperSource

func (d *Scanner) GetPaperSource(name string) (*PaperSource, error)

GetPaperSource returns paper source with specified name if any. Otherwise it returns nil.

func (*Scanner) IterateSources

func (d *Scanner) IterateSources(f func(*PaperSource) bool) error

IterateSources iterates thru paper sources

func (*Scanner) Open

func (d *Scanner) Open() error

Open calls lis->get_device. Should be called before any of GetSourceByName, IterateOptions are called.

type ValueList

type ValueList []*LisValue

ValueList is an array of values

type ValueRange

type ValueRange struct {
	MinValue *LisValue
	MaxValue *LisValue
	Interval *LisValue
}

ValueRange define constraints applied to value

Directories

Path Synopsis
cmd

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL