2
0

GLS.FileJPEG.pas 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. //
  2. // The multimedia graphics platform GLScene https://github.com/glscene
  3. //
  4. unit GLS.FileJPEG;
  5. (* Methods for loading Jpeg images *)
  6. interface
  7. {$I GLScene.inc}
  8. uses
  9. Winapi.OpenGL,
  10. Winapi.OpenGLext,
  11. System.Classes,
  12. System.SysUtils,
  13. Vcl.Graphics,
  14. Vcl.Imaging.Jpeg,
  15. GLS.OpenGLTokens,
  16. GLS.Context,
  17. GLS.Graphics,
  18. GLS.TextureFormat,
  19. GLS.ApplicationFileIO,
  20. GLS.VectorGeometry;
  21. type
  22. TGLJPEGImage = class(TGLBaseImage)
  23. private
  24. FAbortLoading: boolean;
  25. FDivScale: longword;
  26. FDither: boolean;
  27. FSmoothing: boolean;
  28. FProgressiveEncoding: boolean;
  29. procedure SetSmoothing(const AValue: boolean);
  30. public
  31. constructor Create; override;
  32. class function Capabilities: TGLDataFileCapabilities; override;
  33. procedure LoadFromFile(const filename: string); override;
  34. procedure SaveToFile(const filename: string); override;
  35. procedure LoadFromStream(AStream: TStream); override;
  36. procedure SaveToStream(AStream: TStream); override;
  37. // Assigns from any Texture.
  38. procedure AssignFromTexture(textureContext: TGLContext;
  39. const textureHandle: Cardinal;
  40. textureTarget: TGLTextureTarget;
  41. const CurrentFormat: Boolean;
  42. const intFormat: TGLInternalFormat); reintroduce;
  43. property DivScale: longword read FDivScale write FDivScale;
  44. property Dither: boolean read FDither write FDither;
  45. property Smoothing: boolean read FSmoothing write SetSmoothing;
  46. property ProgressiveEncoding: boolean read FProgressiveEncoding;
  47. end;
  48. procedure Jpeg2Bmp(const BmpFileName, JpgFileName: string);
  49. //---------------------------------------------------------------------
  50. implementation
  51. //---------------------------------------------------------------------
  52. procedure Jpeg2Bmp(const BmpFileName, JpgFileName: string);
  53. var
  54. Bmp: TBitmap;
  55. Jpg: TJPEGImage;
  56. begin
  57. Bmp := TBitmap.Create;
  58. Jpg := TJPEGImage.Create;
  59. try
  60. Jpg.LoadFromFile(JpgFileName);
  61. Bmp.Assign(Jpg);
  62. Bmp.SaveToFile(BmpFileName);
  63. finally
  64. Jpg.Free;
  65. Bmp.Free;
  66. end;
  67. end;
  68. // ------------------
  69. // ------------------ TGLJPEGImage ------------------
  70. // ------------------
  71. constructor TGLJPEGImage.Create;
  72. begin
  73. inherited;
  74. FAbortLoading := False;
  75. FDivScale := 1;
  76. FDither := False;
  77. end;
  78. procedure TGLJPEGImage.LoadFromFile(const filename: string);
  79. var
  80. fs: TStream;
  81. begin
  82. if FileStreamExists(fileName) then
  83. begin
  84. fs := TFileStream.Create(fileName, fmOpenRead);
  85. try
  86. LoadFromStream(fs);
  87. finally
  88. fs.Free;
  89. ResourceName := filename;
  90. end;
  91. end
  92. else
  93. raise EInvalidRasterFile.CreateFmt('File %s not found', [filename]);
  94. end;
  95. procedure TGLJPEGImage.SaveToFile(const filename: string);
  96. var
  97. fs: TStream;
  98. begin
  99. fs := TFileStream.Create(fileName, fmOpenWrite or fmCreate);
  100. try
  101. SaveToStream(fs);
  102. finally
  103. fs.Free;
  104. end;
  105. ResourceName := filename;
  106. end;
  107. procedure TGLJPEGImage.LoadFromStream(AStream: TStream);
  108. var
  109. JpegImage: TJpegImage;
  110. begin
  111. try
  112. JpegImage := TJPEGImage.Create;
  113. JpegImage.LoadFromStream(AStream);
  114. if JpegImage.Grayscale then
  115. begin
  116. fColorFormat := GL_LUMINANCE;
  117. fInternalFormat := tfLUMINANCE8;
  118. fElementSize := 1;
  119. end
  120. else
  121. begin
  122. fColorFormat := GL_BGR;
  123. fInternalFormat := tfRGB8;
  124. fElementSize := 3;
  125. end;
  126. fDataType := GL_UNSIGNED_BYTE;
  127. FLOD[0].Width := JpegImage.Width;
  128. FLOD[0].Height := JpegImage.Height;
  129. FLOD[0].Depth := 0;
  130. fCubeMap := False;
  131. fTextureArray := False;
  132. ReallocMem(fData, DataSize);
  133. finally
  134. JpegImage.Free;
  135. end;
  136. end;
  137. procedure TGLJPEGImage.SaveToStream(AStream: TStream);
  138. begin
  139. end;
  140. procedure TGLJPEGImage.AssignFromTexture(textureContext: TGLContext;
  141. const textureHandle: Cardinal; textureTarget: TGLTextureTarget;
  142. const CurrentFormat: boolean; const intFormat: TGLInternalFormat);
  143. begin
  144. end;
  145. procedure TGLJPEGImage.SetSmoothing(const AValue: boolean);
  146. begin
  147. if FSmoothing <> AValue then
  148. FSmoothing := AValue;
  149. end;
  150. class function TGLJPEGImage.Capabilities: TGLDataFileCapabilities;
  151. begin
  152. Result := [dfcRead {, dfcWrite}];
  153. end;
  154. //-----------------------------------
  155. initialization
  156. //-----------------------------------
  157. RegisterRasterFormat('jpg', 'Joint Photographic Experts Group Image',
  158. TGLJPEGImage);
  159. RegisterRasterFormat('jpeg', 'Joint Photographic Experts Group Image',
  160. TGLJPEGImage);
  161. RegisterRasterFormat('jpe', 'Joint Photographic Experts Group Image',
  162. TGLJPEGImage);
  163. end.