import image in backend

import image in backend

Messagepar Stephane Maillard » 01 Août 2005 à 12:22

[syntax="ls"]Const SIG_CD_IMAGESEGMENT = 124%
Const SIG_CD_IMAGEHEADER = 125%
Const SIG_CD_GRAPHIC = 153%
Const SIG_CD_BEGIN = 221% + &H0600% ' includes length
Const SIG_CD_END = 222% + &H0600% ' includes length

Const CDIMAGETYPE_GIF= 1%
Const CDIMAGETYPE_JPEG = 2%
Const CDIMAGETYPE_BMP = 3% ' Notes 6

Const MAX_SEG = &H2800%
Const MAX_ITEM = &HA000&

Const wAPIModule = "NNOTES" ' Windows/32
Const wUIModule = "NNOTESWS"

Declare Private Function NSFItemAppend Lib wAPIModule Alias "NSFItemAppend" _
( Byval hNT As Long, Byval F As Integer, Byval N As String, Byval nN As Integer _
, Byval T As Integer, Byval V As Long, Byval nV As Long) As Integer
Declare Sub NEMDisplayError Lib wUIModule Alias "NEMDisplayError" _
( Byval E As Long)
Declare Private Function OSMemoryAllocate Lib wAPIModule Alias "OSMemoryAllocate" _
( Byval T As Integer, Byval S As Long, hM As Long) As Integer
Declare Private Function OSMemoryLock Lib wAPIModule Alias "OSMemoryLock" _
( Byval hM As Long) As Long
Declare Private Function OSMemoryUnlock Lib wAPIModule Alias "OSMemoryUnlock" _
( Byval hM As Long) As Long
Declare Private Function OSMemoryFree Lib wAPIModule Alias "OSMemoryFree" _
( Byval hM As Long) As Integer
Declare Private Sub Poke Lib wAPIModule Alias "Cmovmem" _
( S As Any, Byval P As Long, Byval N As Long)


Type SegmentData
Data(MAX_SEG / 4 - 1) As Long
End Type


Class ItemBuffer

Private address As Long
Private hM As Long
Private hNT As Long
Private itemname As String
Private seg As SegmentData
Private pointer As Long

Public Sub New(hNT As Long, itemname As String)
Me.hNT = hNT
Me.itemname = itemname
OSMemoryAllocate 0, MAX_ITEM + MAX_SEG + 256, hM
address = OSMemoryLock(hM)
pointer = address
End Sub

Public Sub Delete
If hM = 0 Then Exit Sub
OSMemoryUnlock hM
OSMemoryFree hM
hM = 0
End Sub

Public Sub Put(V As Variant)
Select Case Typename(V)
Case "INTEGER" : n& = 2
Case "LONG" : n& = 4
Case Else : Exit Sub
End Select
Poke V, pointer, n&
pointer = pointer + n&
End Sub

Public Sub Read(f As Integer, n As Integer)
On Error 62 Resume Next ' allow input past EoF
Get #f%, , seg
On Error Goto 0
Poke seg, pointer, n
pointer = pointer + n
End Sub

Public Sub Record(V As Variant)
If pointer - address > MAX_ITEM Then Save
Me.Put V
End Sub

Public Sub Save
s% = NSFItemAppend( hNT, 0, itemname, Len(itemname), 1, address, pointer - address)
If Not s% = 0 Then NEMDisplayError s%
pointer = address
End Sub
End Class


Sub Invert(n As Integer)
h$ = Right$("000" & Hex$(n), 4)
n = Cint("&H" & Right$(h$, 2) & Left$(h$, 2))
End Sub


Sub CreateImageItem(doc As NotesDocument, itemname As String, imagefile As String)
Dim hNT As Long
hNT = doc.Handle
If hNT = 0 Then Error 1000, "No document handle"

f% = Freefile()
Open imagefile For Binary Access Read As #f%
fsize& = Lof(f%)
If fsize& = 0 Then Error 1000, "Can't open file " & imagefile
segs& = -Int(-fsize& / MAX_SEG) ' round up

Get #f%, , v%
Select Case v%
Case &H4947 : ftype% = CDIMAGETYPE_GIF
Get #f%, 7, imgx%
Get #f%, 9, imgy%
Case &HD8FF : ftype% = CDIMAGETYPE_JPEG
p& = 3
n% = 0
While Not (t% = &HC0FF Or t% = &HC2FF) ' SOFn
p& = p& + n%
Get #f%, p&, t% ' marker type
p& = p& + 2
Get #f%, p&, n% ' length
Invert n%
Wend
If t% = &HC0FF Or t% = &HC2FF Then
Get #f%, p& + 3, imgy%
Invert imgy%
Get #f%, p& + 5, imgx%
Invert imgx%
Else
Error 1000, "Can't understand JPEG format"
End If
Case &H4D42 : ftype% = CDIMAGETYPE_BMP ' Notes 6
Get #f%, 19, imgx%
Get #f%, 23, imgy%
Case Else : Error 1000, "Unrecognized image format"
End Select
Seek #f%, 1

Dim buf As New ItemBuffer(hNT, itemname)
With buf
.Record SIG_CD_BEGIN
.Put 1% ' version
.Put SIG_CD_GRAPHIC

.Record SIG_CD_GRAPHIC
.Put 28& ' length
.Put 0& ' dest
.Put 0& ' crop
.Put 0& ' off1
.Put 0& ' off2
.Put 0% ' resized
.Put 1% ' version
.Put 0% ' junk

.Record SIG_CD_IMAGEHEADER
.Put 28& ' length
.Put ftype%
.Put imgx%
.Put imgy%
.Put fsize&
.Put segs&
.Put 0& ' flags
.Put 0& ' junk

For i& = 1 To segs&
If i& = segs& Then dsize% = fsize& Mod MAX_SEG Else dsize% = MAX_SEG
ssize% = dsize% + (dsize% And 1) ' round up
.Record SIG_CD_IMAGESEGMENT
.Put Clng(ssize% + 10) ' length
.Put dsize%
.Put ssize%
.Read f%, ssize%
Next

.Record SIG_CD_END
.Put 1% ' version
.Put SIG_CD_GRAPHIC

.Save
End With
Close #f%
End Sub[/syntax]
Cordialement

Stéphane Maillard
Avatar de l’utilisateur
Stephane Maillard
Lord of DominoArea
Lord of DominoArea
 
Message(s) : 8695
Inscrit(e) le : 16 Déc 2004 à 01:10
Localisation : Bretagne

Retour vers API