Scan2PDF

Share your advanced PureBasic knowledge/code with the community.
coder14
Enthusiast
Enthusiast
Posts: 327
Joined: Tue Jun 21, 2011 10:39 am

Re: Scan2PDF

Post by coder14 »

harkon wrote:

Code: Select all

Procedure.l hkTWAIN_WriteNativeToJPG(hDib.l, sJPGFileName.s, lQuality.l)
  Define lpDib.l
  Define StructSize.l
  Define Width.l 
  Define Height.l
  Define Planes.w
  Define BitCount.w
  Define Compression.l
  Define SizeImage.l
  Define XPix_Meter.l
  Define YPix_Meter.l
  Define ColorsUsed.l
  Define ColorsImportant.l
  Define PalletteSize.l
  Define BMPOffset.l
  Define BMPSize.l
  Define ImageNumber.l
  Define *BMPBuffer

  lpDib=GlobalLock_(hDib)
  
  StructSize=PeekL(lpDib)
  Width=PeekL(lpDib+4)
  Height=PeekL(lpDib+8)
  Planes=PeekW(lpDib+12)
  BitCount=PeekW(lpDib+14)
  Compression=PeekL(lpDib+16)
  SizeImage=PeekL(lpDib+20)
  XPix_Meter=PeekL(lpDib+24)
  YPix_Meter=PeekL(lpDib+28)
  ColorsUsed=PeekL(lpDib+32)
  ColorsImportant=PeekL(lpDib+36)
 
  ;make BMP for CatchImage
  If BitCount<24
    PalletteSize=BitCount*8
  EndIf
  ;BMPFileHeader is
  ;"BM",BMPSize.l,Reserved.w, Reserved.w, Offset.l where bmp data starts
  ; the offset is after the pallette info
  ; pallette info is RGB+1 byte for each color in the pallette
  ; unless it's 24bit then there's no pallette
  BMPOffset=14+StructSize+PalletteSize
  BMPSize=BMPOffset+SizeImage
  
  *BMPBuffer=AllocateMemory(BMPSize)
  PokeS(*BMPBuffer,"BM")
  PokeL(*BMPBuffer+2,BMPSize)
  PokeL(*BMPBuffer+10,BMPOffset)
  CopyMemory(lpDib, *BMPBuffer+14, BMPSize-14)
  
  GlobalUnlock_(hDib)
  
  ImageNumber=CatchImage(#PB_Any,*BMPBuffer,BMPSize)
  
  If sJPGFileName=""
    sJPGFileName.s=SaveFileRequester("Save JPG file",GetHomeDirectory()+"My Documents\","JPG (*.jpg)|*.jpg",0)
  EndIf
  
  ;lQuality is spcified in terms of a number between 1 and 10, 10 being the best quality lowest compression
  ; if not specified and the quality is 0 or invalid, we will default to 7
  If lQuality<1 Or lQuality>10
    lQuality=7
  EndIf  
  ProcedureReturn SaveImage(ImageNumber,sJPGFileName,#PB_ImagePlugin_JPEG, lQuality)
  FreeImage(ImageNumber)
  FreeMemory(*BMPBuffer)
  
EndProcedure

DisableExplicit
I tried this code but the image is converted "flipped" :!:

Image

The DIB is converted correctly with Intel's ijl.dll. Maybe some changes since 2010 :?:
harkon
Enthusiast
Enthusiast
Posts: 217
Joined: Wed Nov 23, 2005 5:48 pm

Re: Scan2PDF

Post by harkon »

Looks like a problem with the offset or bitcount. I ran into this before nut can't remember how I fixed it. Does this happen using all color options or just greycsale?
Missed it by that much!!
HK
coder14
Enthusiast
Enthusiast
Posts: 327
Joined: Tue Jun 21, 2011 10:39 am

Re: Scan2PDF

Post by coder14 »

harkon wrote:Looks like a problem with the offset or bitcount. I ran into this before nut can't remember how I fixed it. Does this happen using all color options or just greycsale?
Hi Harkon! Thanks for your quick reply. Yes! It's okay with pixel 4 but flips on pixel 2. I tried pixel 0, 1, 3 and get strange morphed images squeezed vertically and horizontally. :shock:
User avatar
Fangbeast
PureBasic Protozoa
PureBasic Protozoa
Posts: 4749
Joined: Fri Apr 25, 2003 3:08 pm
Location: Not Sydney!!! (Bad water, no goats)

Re: Scan2PDF

Post by Fangbeast »

Harkon, i've got scanning going for different sections in my inventory program. One deals with scanning images for inventory products (and doesn't go to PDF as it isn't needed) and the other ia attaching documents by scanning to PDF.

This one is a keeper and I thank you heaps for it.
Amateur Radio, D-STAR/VK3HAF
coder14
Enthusiast
Enthusiast
Posts: 327
Joined: Tue Jun 21, 2011 10:39 am

Re: Scan2PDF

Post by coder14 »

@harkon: Can we use this function to convert the twain DIB to bitmap then save it?
http://www.purebasic.fr/english/viewtop ... 13&t=67403
harkon
Enthusiast
Enthusiast
Posts: 217
Joined: Wed Nov 23, 2005 5:48 pm

Re: Scan2PDF

Post by harkon »

coder14 wrote:@harkon: Can we use this function to convert the twain DIB to bitmap then save it?
http://www.purebasic.fr/english/viewtop ... 13&t=67403
I don't see why not. This could prove a really good and more robust solution. I don't have time to flesh this out and rework my app, but I did find some VB code to do this. It's on my dropbox;
https://dl.dropboxusercontent.com/u/653 ... lasses.zip

If you get this working, please post back.
Missed it by that much!!
HK
harkon
Enthusiast
Enthusiast
Posts: 217
Joined: Wed Nov 23, 2005 5:48 pm

Re: Scan2PDF

Post by harkon »

coder14 wrote:
harkon wrote:

Code: Select all

Procedure.l hkTWAIN_WriteNativeToJPG(hDib.l, sJPGFileName.s, lQuality.l)
  Define lpDib.l
  Define StructSize.l
  Define Width.l 
  Define Height.l
  Define Planes.w
  Define BitCount.w
  Define Compression.l
  Define SizeImage.l
  Define XPix_Meter.l
  Define YPix_Meter.l
  Define ColorsUsed.l
  Define ColorsImportant.l
  Define PalletteSize.l
  Define BMPOffset.l
  Define BMPSize.l
  Define ImageNumber.l
  Define *BMPBuffer

  lpDib=GlobalLock_(hDib)
  
  StructSize=PeekL(lpDib)
  Width=PeekL(lpDib+4)
  Height=PeekL(lpDib+8)
  Planes=PeekW(lpDib+12)
  BitCount=PeekW(lpDib+14)
  Compression=PeekL(lpDib+16)
  SizeImage=PeekL(lpDib+20)
  XPix_Meter=PeekL(lpDib+24)
  YPix_Meter=PeekL(lpDib+28)
  ColorsUsed=PeekL(lpDib+32)
  ColorsImportant=PeekL(lpDib+36)
 
  ;make BMP for CatchImage
  If BitCount<24
    PalletteSize=BitCount*8
  EndIf
  ;BMPFileHeader is
  ;"BM",BMPSize.l,Reserved.w, Reserved.w, Offset.l where bmp data starts
  ; the offset is after the pallette info
  ; pallette info is RGB+1 byte for each color in the pallette
  ; unless it's 24bit then there's no pallette
  BMPOffset=14+StructSize+PalletteSize
  BMPSize=BMPOffset+SizeImage
  
  *BMPBuffer=AllocateMemory(BMPSize)
  PokeS(*BMPBuffer,"BM")
  PokeL(*BMPBuffer+2,BMPSize)
  PokeL(*BMPBuffer+10,BMPOffset)
  CopyMemory(lpDib, *BMPBuffer+14, BMPSize-14)
  
  GlobalUnlock_(hDib)
  
  ImageNumber=CatchImage(#PB_Any,*BMPBuffer,BMPSize)
  
  If sJPGFileName=""
    sJPGFileName.s=SaveFileRequester("Save JPG file",GetHomeDirectory()+"My Documents\","JPG (*.jpg)|*.jpg",0)
  EndIf
  
  ;lQuality is spcified in terms of a number between 1 and 10, 10 being the best quality lowest compression
  ; if not specified and the quality is 0 or invalid, we will default to 7
  If lQuality<1 Or lQuality>10
    lQuality=7
  EndIf  
  ProcedureReturn SaveImage(ImageNumber,sJPGFileName,#PB_ImagePlugin_JPEG, lQuality)
  FreeImage(ImageNumber)
  FreeMemory(*BMPBuffer)
  
EndProcedure

DisableExplicit
I tried this code but the image is converted "flipped" :!:

Image

The DIB is converted correctly with Intel's ijl.dll. Maybe some changes since 2010 :?:
Coder, I just remembered something. Did you by chance leave the unicode option checked in the compiler options. I can't remember for sure, but I think this will mess with peeks and pokes. Try as a non-unicode app and see if this helps.
Missed it by that much!!
HK
coder14
Enthusiast
Enthusiast
Posts: 327
Joined: Tue Jun 21, 2011 10:39 am

Re: Scan2PDF

Post by coder14 »

harkon wrote:
coder14 wrote:@harkon: Can we use this function to convert the twain DIB to bitmap then save it?
http://www.purebasic.fr/english/viewtop ... 13&t=67403
I don't see why not. This could prove a really good and more robust solution. I don't have time to flesh this out and rework my app, but I did find some VB code to do this. It's on my dropbox;
https://dl.dropboxusercontent.com/u/653 ... lasses.zip

If you get this working, please post back.
Thank you. I can't find that function in your classes but I'll study them more. :)
coder14
Enthusiast
Enthusiast
Posts: 327
Joined: Tue Jun 21, 2011 10:39 am

Re: Scan2PDF

Post by coder14 »

harkon wrote:Coder, I just remembered something. Did you by chance leave the unicode option checked in the compiler options. I can't remember for sure, but I think this will mess with peeks and pokes. Try as a non-unicode app and see if this helps.
Hello harkon. For EZTW32.dll to work I need to use the x86 version of PB which is ASCII default. But TI-994A modified your code and it works now - no more flipping but the RGB pixel type is still 4 and not 3. It is very similar to your original code. I am still trying to figure out the differences. :lol:

http://www.purebasic.fr/english/viewtop ... 27#p500027

Thank you for your help. Many great gurus helped to solve this one. :D
User avatar
Fangbeast
PureBasic Protozoa
PureBasic Protozoa
Posts: 4749
Joined: Fri Apr 25, 2003 3:08 pm
Location: Not Sydney!!! (Bad water, no goats)

Re: Scan2PDF

Post by Fangbeast »

Harkon, I have 2 scanners connected and this code only finds the primary one that is currently disconnected as it is only used once every 6 months (maybe more) and of course I can go no further.

Is there any way to pop a dialogue to select the scanner that you want please? I can't do it as it's API that I don't understand.

The secondary scanner is the one always in use but it was installed 5 years after the primary and I need them both.
Amateur Radio, D-STAR/VK3HAF
Post Reply