jpgloader.bmx 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. Strict
  2. Rem
  3. bbdoc: Graphics/JPG loader
  4. about:
  5. The JPG loader module provides the ability to load JPG format #pixmaps.
  6. End Rem
  7. Module BRL.JPGLoader
  8. ModuleInfo "Version: 1.05"
  9. ModuleInfo "Author: Simon Armstrong, Jeffrey D. Panici"
  10. ModuleInfo "License: zlib/libpng"
  11. ModuleInfo "Copyright: Blitz Research Ltd"
  12. ModuleInfo "Modserver: BRL"
  13. ModuleInfo "History: 1.05 Release"
  14. ModuleInfo "History: Fixed SavePixmapJPeg"
  15. ModuleInfo "History: 1.04 Release"
  16. ModuleInfo "History: Removed print"
  17. ModuleInfo "History: 1.03 Release"
  18. ModuleInfo "History: Changed ReadBytes to Read for loader"
  19. ModuleInfo "History: Added SaveJPEG function, thanks to Jeffrey D. Panici for the writefunc `fix'"
  20. ModuleInfo "History: 1.02 Release"
  21. ModuleInfo "History: Added support for monochrome / single channel"
  22. Import BRL.Pixmap
  23. Import Pub.LibJPEG
  24. Private
  25. Function readfunc%( buf:Byte Ptr,count,src:Object )
  26. Local stream:TStream
  27. stream=TStream(src)
  28. Local n=stream.Read( buf,count )
  29. Return n
  30. End Function
  31. Function writefunc%( buf:Byte Ptr,count,src:Object )
  32. Local stream:TStream
  33. stream=TStream(src)
  34. Local n=stream.Write( buf,count )
  35. Return n
  36. End Function
  37. Public
  38. Rem
  39. bbdoc: Load a Pixmap in JPeg format
  40. about:
  41. #LoadPixmapJPeg loads a pixmap from @url in JPeg format.
  42. If the pixmap cannot be loaded, Null is returned.
  43. End Rem
  44. Function LoadPixmapJPeg:TPixmap( url:Object )
  45. Local jpg,width,height,depth,y
  46. Local pix:Byte Ptr
  47. Local pixmap:TPixmap
  48. Local stream:TStream
  49. stream=ReadStream( url )
  50. If Not stream Return
  51. Local res=loadjpg(stream,readfunc,width,height,depth,pix)
  52. stream.Close
  53. If res Return Null
  54. If width=0 Return
  55. Select depth
  56. Case 1
  57. pixmap=CreatePixmap( width,height,PF_I8 )
  58. For y=0 Until height
  59. CopyPixels pix+y*width,pixmap.PixelPtr(0,y),PF_I8,width
  60. Next
  61. Case 3
  62. pixmap=CreatePixmap( width,height,PF_RGB888 )
  63. For y=0 Until height
  64. CopyPixels pix+y*width*3,pixmap.PixelPtr(0,y),PF_RGB888,width
  65. Next
  66. End Select
  67. free_ pix
  68. Return pixmap
  69. End Function
  70. Rem
  71. bbdoc: Save a Pixmap in JPeg format
  72. about:
  73. Saves @pixmap to @url in JPeg format. If successful, #SavePixmapJPeg returns
  74. True, otherwise False.
  75. The optional @quality parameter should be in the range 0 to 100, where
  76. 0 indicates poor quality (smallest) and 100 indicates best quality (largest).
  77. End Rem
  78. Function SavePixmapJPeg( pixmap:TPixmap,url:Object,quality=75 )
  79. Assert quality>=1 And quality<=100
  80. Local stream:TStream=WriteStream( url )
  81. If Not stream Return
  82. pixmap=pixmap.convert(PF_RGB888)
  83. Local pix:Byte Ptr=pixmap.PixelPtr( 0,0 )
  84. savejpg(stream,writefunc,pixmap.width,pixmap.height,pixmap.pitch,pix,quality)
  85. stream.Close
  86. Return True
  87. End Function
  88. Private
  89. Type TPixmapLoaderJPG Extends TPixmapLoader
  90. Method LoadPixmap:TPixmap( stream:TStream )
  91. Return LoadPixmapJPeg( stream )
  92. End Method
  93. End Type
  94. New TPixmapLoaderJPG