Skip to content

Commit

Permalink
pull in latest changes from upstream
Browse files Browse the repository at this point in the history
  • Loading branch information
majorsilence committed Oct 27, 2024
1 parent 2367ca9 commit 0db8a02
Show file tree
Hide file tree
Showing 9 changed files with 5,109 additions and 4,650 deletions.
143 changes: 78 additions & 65 deletions TownSuite.TwainScanner/DibToImage.cs
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
/* Ýòîò ôàéë ÿâëÿåòñÿ ÷àñòüþ áèáëèîòåêè Saraff.Twain.NET
* © SARAFF SOFTWARE (Êèðíîæèöêèé Àíäðåé), 2011.
* Saraff.Twain.NET - ñâîáîäíàÿ ïðîãðàììà: âû ìîæåòå ïåðåðàñïðîñòðàíÿòü åå è/èëè
* èçìåíÿòü åå íà óñëîâèÿõ Ìåíüøåé Ñòàíäàðòíîé îáùåñòâåííîé ëèöåíçèè GNU â òîì âèäå,
* â êàêîì îíà áûëà îïóáëèêîâàíà Ôîíäîì ñâîáîäíîãî ïðîãðàììíîãî îáåñïå÷åíèÿ;
* ëèáî âåðñèè 3 ëèöåíçèè, ëèáî (ïî âàøåìó âûáîðó) ëþáîé áîëåå ïîçäíåé
* âåðñèè.
* Saraff.Twain.NET ðàñïðîñòðàíÿåòñÿ â íàäåæäå, ÷òî îíà áóäåò ïîëåçíîé,
* íî ÁÅÇÎ ÂÑßÊÈÕ ÃÀÐÀÍÒÈÉ; äàæå áåç íåÿâíîé ãàðàíòèè ÒÎÂÀÐÍÎÃÎ ÂÈÄÀ
* èëè ÏÐÈÃÎÄÍÎÑÒÈ ÄËß ÎÏÐÅÄÅËÅÍÍÛÕ ÖÅËÅÉ. Ïîäðîáíåå ñì. â Ìåíüøåé Ñòàíäàðòíîé
* îáùåñòâåííîé ëèöåíçèè GNU.
* Âû äîëæíû áûëè ïîëó÷èòü êîïèþ Ìåíüøåé Ñòàíäàðòíîé îáùåñòâåííîé ëèöåíçèè GNU
* âìåñòå ñ ýòîé ïðîãðàììîé. Åñëè ýòî íå òàê, ñì.
/* ���� ���� �������� ������ ���������� Saraff.Twain.NET
* SARAFF SOFTWARE (����������� ������), 2011.
* Saraff.Twain.NET - ��������� ���������: �� ������ ������������������ �� �/���
* �������� �� �� �������� ������� ����������� ������������ �������� GNU � ��� ����,
* � ����� ��� ���� ������������ ������ ���������� ������������ �����������;
* ���� ������ 3 ��������, ���� (�� ������ ������) ����� ����� �������
* ������.
* Saraff.Twain.NET ���������������� � �������, ��� ��� ����� ��������,
* �� ���� ������ ��������; ���� ��� ������� �������� ��������� ����
* ��� ����������� ��� ������������ �����. ��������� ��. � ������� �����������
* ������������ �������� GNU.
* �� ������ ���� �������� ����� ������� ����������� ������������ �������� GNU
* ������ � ���� ����������. ���� ��� �� ���, ��.
* <http://www.gnu.org/licenses/>.)
*
* This file is part of Saraff.Twain.NET.
* © SARAFF SOFTWARE (Kirnazhytski Andrei), 2011.
* SARAFF SOFTWARE (Kirnazhytski Andrei), 2011.
* Saraff.Twain.NET is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
Expand All @@ -35,57 +35,90 @@
using System.IO;
using System.Reflection;
using System.Collections.Generic;
using TownSuite.TwainScanner;


namespace TownSuite.TwainScanner
{

internal sealed class DibToImage {
private const int BufferSize=256*1024; //256K
internal sealed class DibToImage : _ImageHandler
{

public static Stream WithStream(IntPtr dibPtr,IStreamProvider provider) {
Stream _stream=provider!=null?provider.GetStream():new MemoryStream();
BinaryWriter _writer=new BinaryWriter(_stream);

BITMAPINFOHEADER _bmi=(BITMAPINFOHEADER)Marshal.PtrToStructure(dibPtr,typeof(BITMAPINFOHEADER));

int _extra=0;
if(_bmi.biCompression==0) {
int _bytesPerRow=((_bmi.biWidth*_bmi.biBitCount)>>3);
_extra=Math.Max(_bmi.biHeight*(_bytesPerRow+((_bytesPerRow&0x3)!=0?4-_bytesPerRow&0x3:0))-_bmi.biSizeImage,0);
}

int _dibSize=_bmi.biSize+_bmi.biSizeImage+_extra+(_bmi.ClrUsed<<2);
/// <summary>
/// Convert a block of unmanaged memory to stream.
/// </summary>
/// <param name="ptr">The pointer to block of unmanaged memory.</param>
/// <param name="stream"></param>
protected override void PtrToStreamCore(IntPtr ptr, Stream stream)
{
BinaryWriter _writer = new BinaryWriter(stream);

#region BITMAPFILEHEADER

BITMAPINFOHEADER _header = this.Header;

_writer.Write((ushort)0x4d42);
_writer.Write(14+_dibSize);
_writer.Write(14 + this.GetSize());
_writer.Write(0);
_writer.Write(14+_bmi.biSize+(_bmi.ClrUsed<<2));
_writer.Write(14 + _header.biSize + (_header.ClrUsed << 2));

#endregion

#region BITMAPINFO and pixel data

byte[] _buffer = new byte[DibToImage.BufferSize];
for(int _offset = 0, _len = 0; _offset<_dibSize; _offset+=_len) {
_len=Math.Min(DibToImage.BufferSize,_dibSize-_offset);
Marshal.Copy((IntPtr)(dibPtr.ToInt64()+_offset),_buffer,0,_len);
_writer.Write(_buffer,0,_len);
}
base.PtrToStreamCore(ptr, stream);

#endregion

return _stream;
}

public static Stream WithStream(IntPtr dibPtr) {
return DibToImage.WithStream(dibPtr,null);
/// <summary>
/// Gets the size of a image data.
/// </summary>
/// <returns>
/// Size of a image data.
/// </returns>
protected override int GetSize()
{
if (!this.HandlerState.ContainsKey("DIBSIZE"))
{
BITMAPINFOHEADER _header = this.Header;

int _extra = 0;
if (_header.biCompression == 0)
{
int _bytesPerRow = ((_header.biWidth * _header.biBitCount) >> 3);
_extra = Math.Max(_header.biHeight * (_bytesPerRow + ((_bytesPerRow & 0x3) != 0 ? 4 - _bytesPerRow & 0x3 : 0)) - _header.biSizeImage, 0);
}

this.HandlerState.Add("DIBSIZE", _header.biSize + _header.biSizeImage + _extra + (_header.ClrUsed << 2));
}
return (int)this.HandlerState["DIBSIZE"];
}

/// <summary>
/// Gets the size of the buffer.
/// </summary>
/// <value>
/// The size of the buffer.
/// </value>
protected override int BufferSize => 256 * 1024; //256K

private BITMAPINFOHEADER Header
{
get
{
if (!this.HandlerState.ContainsKey("BITMAPINFOHEADER"))
{
this.HandlerState.Add("BITMAPINFOHEADER", Marshal.PtrToStructure(this.ImagePointer, typeof(BITMAPINFOHEADER)));
}
return this.HandlerState["BITMAPINFOHEADER"] as BITMAPINFOHEADER;
}
}

[StructLayout(LayoutKind.Sequential,Pack=2)]
private class BITMAPINFOHEADER {
[StructLayout(LayoutKind.Sequential, Pack = 2)]
private class BITMAPINFOHEADER
{
public int biSize;
public int biWidth;
public int biHeight;
Expand All @@ -98,29 +131,9 @@ private class BITMAPINFOHEADER {
public int biClrUsed;
public int biClrImportant;

public int ClrUsed {
get {
return this.IsRequiredCreateColorTable ? 1<<this.biBitCount : this.biClrUsed;
}
}
public int ClrUsed => this.IsRequiredCreateColorTable ? 1 << this.biBitCount : this.biClrUsed;

public bool IsRequiredCreateColorTable {
get {
return this.biClrUsed==0&&this.biBitCount<=8;
}
}
public bool IsRequiredCreateColorTable => this.biClrUsed == 0 && this.biBitCount <= 8;
}
}

/// <summary>
/// Provides instances of the <see cref="System.IO.Stream"/> for data writing.
/// </summary>
public interface IStreamProvider {

/// <summary>
/// Gets the stream.
/// </summary>
/// <returns>The stream.</returns>
Stream GetStream();
}
}
}
5 changes: 4 additions & 1 deletion TownSuite.TwainScanner/MainFrame.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1064,7 +1064,10 @@ private void SourceListBox_SelectedValueChanged(object sender, EventArgs e)
private void SourceTwianListBox_SelectedValueChanged(object sender, EventArgs e)
{
//this._twain.SetDefaultSource(sourceTwianListBox.SelectedIndex);
this._twain.SourceIndex = sourceTwianListBox.SelectedIndex;
if (sourceTwianListBox.SelectedIndex >= 0)
{
this._twain.SourceIndex = sourceTwianListBox.SelectedIndex;
}
}

private void MainFrame_FormClosing(object sender, FormClosingEventArgs e)
Expand Down
30 changes: 30 additions & 0 deletions TownSuite.TwainScanner/Pict.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Runtime.InteropServices;
using System.Text;
using TownSuite.TwainScanner;

namespace TownSuite.TwainScanner
{

internal sealed class Pict : _ImageHandler
{

/// <summary>
/// Gets the size of a image data.
/// </summary>
/// <returns>
/// Size of a image data.
/// </returns>
protected override int GetSize() => throw new NotImplementedException();

/// <summary>
/// Gets the size of the buffer.
/// </summary>
/// <value>
/// The size of the buffer.
/// </value>
protected override int BufferSize => 256 * 1024; //256K
}
}
Loading

0 comments on commit 0db8a02

Please sign in to comment.