Basic Univers

; Amelioration de la fonction Plot() et Point() avec une image
; Permet de transférer une image dans un tableau et inversement.


Procedure ImageToTable(Image, Table) ; Retourne 1 si l'image a été chargée dans le tableau ImageID=#Image : Table=@Tableau(), ex : Dim Tableau(ImageWidth(), ImageHeight()) -> @Tableau()
  If Image>= 0 And Table
    UseImage(Image)
    ImageID = ImageID()
    Hdc = CreateCompatibleDC_( GetDC_( ImageID ))
    If HDC
      bmi.BITMAPINFO
      bm.BITMAP
      GetObject_( ImageID , SizeOf(BITMAP), @bm.BITMAP)
      bmi\bmiHeader\biSize = SizeOf(BITMAPINFOHEADER)
      bmi\bmiheader\biWidth = bm\bmWidth
      bmi\bmiheader\biHeight = bm\bmHeight
      bmi\bmiheader\biPlanes = 1
      bmi\bmiheader\biBitCount = 32
      bmi\bmiheader\biCompression = #BI_RGB
      HList = AllocateMemory(bm\bmWidth*bm\bmHeight*4)
      GetDIBits_(hDC, ImageID , 0, bm\bmHeight, HList, bmi, #DIB_RGB_COLORS )

      For nn = 0 To bm\bmWidth - 1
        For n = 0 To bm\bmHeight - 1
          s = HList + nn * 4 +(bm\bmHeight - 1 - n) * bm\bmWidth * 4
          d = Table + n * 4 + nn * bm\bmHeight * 4
          CopyMemory(s + 2, d, 1)
          CopyMemory(s + 1, d + 1, 1)
          CopyMemory(s, d + 2, 1)
        Next n
        Table + 4
      Next nn
      FreeMemory(HList)
    Else
      ProcedureReturn
    EndIf
    ProcedureReturn 1
  EndIf
EndProcedure



Procedure TableToImage(Image, Table) ; Crée une image à partir du tableau Image=#Image, Table=@Tableau(), ex : Dim Tableau(ImageWidth(),ImageHeight()) -> @Tableau()
  If IsImage(Image) And Table
    ImageID = UseImage(Image)
    bm.BITMAP
    GetObject_( ImageID , SizeOf(BITMAP), @bm.BITMAP)
    bmi.BITMAPINFO
    bmi\bmiHeader\biSize = SizeOf(BITMAPINFOHEADER)
    bmi\bmiheader\biWidth = bm\bmWidth
    bmi\bmiheader\biHeight = bm\bmHeight
    bmi\bmiheader\biPlanes = 1
    bmi\bmiheader\biBitCount = 32
    bmi\bmiheader\biCompression = #BI_RGB
    pixel = AllocateMemory(bm\bmHeight*bm\bmWidth*4)
    For nn = 0 To bm\bmwidth - 1
      For n = 0 To bm\bmheight - 1
        s = Table + n * 4 + nn * bm\bmHeight * 4
        d = pixel + nn * 4 +(bm\bmHeight - 1 - n) * bm\bmWidth * 4
        CopyMemory(s, d + 2, 1)
        CopyMemory(s + 1, d + 1, 1)
        CopyMemory(s + 2, d, 1)
      Next
      table + 4
    Next
    HDC = StartDrawing( ImageOutput())
    SetDIBits_(HDC, ImageID , 0, ImageHeight(), pixel, bmi, #DIB_RGB_COLORS )
    StopDrawing()
    FreeMemory(pixel)
    ProcedureReturn 1
  EndIf
EndProcedure