Catch image m4a flac cover art

Share your advanced PureBasic knowledge/code with the community.
User avatar
oryaaaaa
Addict
Addict
Posts: 825
Joined: Mon Jan 12, 2004 11:40 pm
Location: Okazaki, JAPAN

Catch image m4a flac cover art

Post by oryaaaaa »

catch image of cover art method of m4a purchased in iTunes Store.

Code: Select all

UseJPEGImageDecoder()
#CoverArtSize = 9*1024*1024 ; max 9MB

Procedure.l CoverArt_jpeg(FileName.s)
  Protected file_id.l, file_size.l, n1.l, n2.l, n3.l, n4.l
  Protected *CoverArt = AllocateMemory(#CoverArtSize)
  ;
  DataSection
    jpeg_head:
    Data.b $FF, $D8, $00
    jpeg_end:
    Data.b $FF, $D9, $00
    jpeg_mark:
    Data.b $4A, $46, $49, $46, $00
  EndDataSection
  ;
  file_id = ReadFile(#PB_Any, FileName)
  If Lof(file_id)>(#CoverArtSize)
    file_size = #CoverArtSize
  Else
    file_size = Lof(file_id)
  EndIf
  ReadData(file_id, *CoverArt, file_size) 
  CloseFile(file_id)
  ;
  For file_id = 0 To file_size
    If CompareMemory(*CoverArt+file_id+8, ?jpeg_mark, 4)
      n1 = file_id
    ElseIf n1>0 And CompareMemory(*CoverArt+file_id, ?jpeg_head, 2)
      n2 = file_id
    ElseIf n2>0 And CompareMemory(*CoverArt+file_id, ?jpeg_end, 2)
      n3 = file_id
      n4 =CatchImage(#PB_Any, *CoverArt+n2, n3-n2)
      If n4
        FreeMemory(*CoverArt)
        ProcedureReturn n4
      EndIf
    EndIf
  Next
  ;
  ProcedureReturn 0
EndProcedure