QR Code decoder library ported to PB

Share your advanced PureBasic knowledge/code with the community.
infratec
Always Here
Always Here
Posts: 6817
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: QR Code decoder library ported to PB

Post by infratec »

I changed the second part of the listing above.

Or please replace Procedure.i ImageToGrayScaleBuffer(Img.i) with:

Code: Select all

Procedure.i ImageToGrayScaleBuffer(Img.i)
  
  Structure rgba
    b.a
    g.a
    r.a
    alpha.a
  EndStructure
  
  
  Protected.i ImgWidth, ImgHeight, PixelBytes, LinePadBytes, X, Y
  Protected *Buffer, *BufferPos.Ascii, *ImgPos.rgba
  
  
  If IsImage(Img)
    
    If StartDrawing(ImageOutput(Img))
      
      ImgWidth = ImageWidth(Img)
      ImgHeight = ImageHeight(Img)
      
      *Buffer = AllocateMemory(ImgWidth * ImgHeight, #PB_Memory_NoClear)
      If *Buffer
        PixelBytes = 3
        *ImgPos = DrawingBuffer()
        
        If DrawingBufferPixelFormat() & #PB_PixelFormat_32Bits_RGB : PixelBytes = 4 : EndIf
        If DrawingBufferPixelFormat() & #PB_PixelFormat_32Bits_BGR : PixelBytes = 4 : EndIf
        LinePadBytes = DrawingBufferPitch() - (ImgWidth * PixelBytes)
        
        Debug "PixelBytes: " + Str(PixelBytes)
        
        ImgWidth - 1
        ImgHeight - 1
        
        *BufferPos = *Buffer
        
        For Y = 0 To ImgHeight
          For X = 0 To ImgWidth
            ;Debug Hex(*ImgPos\a) + " " + Hex(*ImgPos\r) + " " + Hex(*ImgPos\g) + " " + Hex(*ImgPos\b)
            If PixelBytes = 3
              *BufferPos\a = (*ImgPos\r + *ImgPos\g  + *ImgPos\b) / 3
            Else
              If *ImgPos\alpha > 127
                *BufferPos\a = (*ImgPos\r + *ImgPos\g  + *ImgPos\b) / 3
              Else
                *BufferPos\a = $FF
              EndIf
            EndIf
            ;*BufferPos\a = 0.2990 * *ImgPos\r + 0.5870 * *ImgPos\g  + 0.1140 * *ImgPos\b ; TV
            ;*BufferPos\a = 0.2126 * *ImgPos\r + 0.7152 * *ImgPos\g  + 0.0722 * *ImgPos\b ; ITU-R BT.709 HDTV and CIE 1931 sRGB
            ;*BufferPos\a = 0.2627 * *ImgPos\r + 0.6780 * *ImgPos\g  + 0.0593 * *ImgPos\b ; ITU-R BT.2100 HDR
            *BufferPos + 1
            *ImgPos + PixelBytes
          Next X
          *ImgPos + LinePadBytes
        Next Y
        
      EndIf
      StopDrawing()
    EndIf
    
  EndIf
  
  ProcedureReturn *Buffer
  
EndProcedure
Then the image from wikipedia works.
This image is strange. It uses only black color (0 0 0) and it only sets the transparency.
User avatar
skywalk
Addict
Addict
Posts: 3972
Joined: Wed Dec 23, 2009 10:14 pm
Location: Boston, MA

Re: QR Code decoder library ported to PB

Post by skywalk »

Thanks! It works now for the wiki image and your previous qr-make lib creations. 8)
The nice thing about standards is there are so many to choose from. ~ Andrew Tanenbaum
infratec
Always Here
Always Here
Posts: 6817
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: QR Code decoder library ported to PB

Post by infratec »

Made a module out of it, to avoid naming conflicts.
infratec
Always Here
Always Here
Posts: 6817
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: QR Code decoder library ported to PB

Post by infratec »

Updated the listings in the first two postings to the current git version of quirc: 8 Oct 2021
User avatar
Thorsten1867
Addict
Addict
Posts: 1366
Joined: Wed Aug 24, 2005 4:02 pm
Location: Germany

Re: QR Code decoder library ported to PB

Post by Thorsten1867 »

QRCode - Module (Encoding/Decoding/Gadget)
Translated with http://www.DeepL.com/Translator

Download of PureBasic - Modules
Download of PureBasic - Programs

[Windows 11 x64] [PB V5.7x]
Post Reply