GLFilePGM.pas 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. //
  2. // This unit is part of the GLScene Engine, http://glscene.org
  3. //
  4. unit GLFilePGM;
  5. (* PGM image loader *)
  6. {$I GLScene.inc}
  7. interface
  8. uses
  9. Winapi.OpenGL,
  10. System.Classes,
  11. System.SysUtils,
  12. OpenGLTokens,
  13. GLContext,
  14. GLGraphics,
  15. GLTextureFormat,
  16. GLS.Strings,
  17. GLApplicationFileIO,
  18. GLS.CUDAUtility;
  19. type
  20. TGLPGMImage = class(TGLBaseImage)
  21. public
  22. class function Capabilities: TGLDataFileCapabilities; override;
  23. procedure LoadFromFile(const filename: string); override;
  24. procedure SaveToFile(const filename: string); override;
  25. procedure LoadFromStream(stream: TStream); override;
  26. procedure SaveToStream(stream: TStream); override;
  27. procedure AssignFromTexture(textureContext: TGLContext;
  28. const textureHandle: Cardinal; textureTarget: TGLTextureTarget;
  29. const CurrentFormat: Boolean; const intFormat: TGLInternalFormat);
  30. reintroduce;
  31. end;
  32. // ------------------------------------------------------------------
  33. implementation
  34. // ------------------------------------------------------------------
  35. // ------------------
  36. // ------------------ TGLPGMImage ------------------
  37. // ------------------
  38. procedure TGLPGMImage.LoadFromFile(const filename: string);
  39. var
  40. w, h: Integer;
  41. cutBuffer: System.PSingle;
  42. begin
  43. if FileExists(filename) then
  44. begin
  45. if not IsCUTILInitialized then
  46. if not InitCUTIL then
  47. begin
  48. EInvalidRasterFile.Create(strCUTILFailed);
  49. exit;
  50. end;
  51. cutBuffer := nil;
  52. if cutLoadPGMf(PAnsiChar(AnsiString(filename)), cutBuffer, w, h) then
  53. begin
  54. ResourceName := filename;
  55. UnMipmap;
  56. FLOD[0].Width := w;
  57. FLOD[0].Height := h;
  58. FLOD[0].Depth := 0;
  59. fColorFormat := GL_LUMINANCE;
  60. fInternalFormat := tfLUMINANCE_FLOAT32;
  61. fDataType := GL_FLOAT;
  62. fCubeMap := false;
  63. fTextureArray := false;
  64. fElementSize := GetTextureElementSize(tfLUMINANCE_FLOAT32);
  65. ReallocMem(fData, DataSize);
  66. Move(cutBuffer^, fData^, DataSize);
  67. cutFree(cutBuffer);
  68. end;
  69. end
  70. else
  71. raise EInvalidRasterFile.CreateFmt('File %s not found', [filename]);
  72. end;
  73. procedure TGLPGMImage.SaveToFile(const filename: string);
  74. begin
  75. if not IsCUTILInitialized then
  76. if not InitCUTIL then
  77. begin
  78. EInvalidRasterFile.Create(strCUTILFailed);
  79. exit;
  80. end;
  81. if not cutSavePGMf(PAnsiChar(AnsiString(filename)), System.PSingle(fData),
  82. FLOD[0].Width, FLOD[0].Height) then
  83. raise EInvalidRasterFile.Create('Saving to file failed');
  84. end;
  85. procedure TGLPGMImage.LoadFromStream(stream: TStream);
  86. begin
  87. Assert(false, 'Stream loading not supported');
  88. end;
  89. procedure TGLPGMImage.SaveToStream(stream: TStream);
  90. begin
  91. Assert(false, 'Stream saving not supported');
  92. end;
  93. procedure TGLPGMImage.AssignFromTexture(textureContext: TGLContext;
  94. const textureHandle: Cardinal; textureTarget: TGLTextureTarget;
  95. const CurrentFormat: Boolean; const intFormat: TGLInternalFormat);
  96. var
  97. oldContext: TGLContext;
  98. contextActivate: Boolean;
  99. texFormat: Cardinal;
  100. residentFormat: TGLInternalFormat;
  101. glTarget: Cardinal;
  102. begin
  103. if not((textureTarget = ttTexture2D) or (textureTarget = ttTextureRect)) then
  104. exit;
  105. oldContext := CurrentGLContext;
  106. contextActivate := (oldContext <> textureContext);
  107. if contextActivate then
  108. begin
  109. if Assigned(oldContext) then
  110. oldContext.Deactivate;
  111. textureContext.Activate;
  112. end;
  113. glTarget := DecodeTextureTarget(textureTarget);
  114. try
  115. textureContext.GLStates.TextureBinding[0, textureTarget] := textureHandle;
  116. fLevelCount := 0;
  117. fCubeMap := false;
  118. fTextureArray := false;
  119. fColorFormat := GL_LUMINANCE;
  120. fDataType := GL_FLOAT;
  121. // Check level existence
  122. gl.GetTexLevelParameteriv(glTarget, 0, GL_TEXTURE_INTERNAL_FORMAT,
  123. @texFormat);
  124. if texFormat > 1 then
  125. begin
  126. gl.GetTexLevelParameteriv(glTarget, 0, GL_TEXTURE_WIDTH, @FLOD[0].Width);
  127. gl.GetTexLevelParameteriv(glTarget, 0, GL_TEXTURE_HEIGHT,
  128. @FLOD[0].Height);
  129. FLOD[0].Depth := 0;
  130. residentFormat := OpenGLFormatToInternalFormat(texFormat);
  131. if CurrentFormat then
  132. fInternalFormat := residentFormat
  133. else
  134. fInternalFormat := intFormat;
  135. Inc(fLevelCount);
  136. end;
  137. if fLevelCount > 0 then
  138. begin
  139. fElementSize := GetTextureElementSize(fColorFormat, fDataType);
  140. ReallocMem(fData, DataSize);
  141. gl.GetTexImage(glTarget, 0, fColorFormat, fDataType, fData);
  142. end
  143. else
  144. fLevelCount := 1;
  145. gl.CheckError;
  146. finally
  147. if contextActivate then
  148. begin
  149. textureContext.Deactivate;
  150. if Assigned(oldContext) then
  151. oldContext.Activate;
  152. end;
  153. end;
  154. end;
  155. class function TGLPGMImage.Capabilities: TGLDataFileCapabilities;
  156. begin
  157. Result := [dfcRead, dfcWrite];
  158. end;
  159. //------------------------------------------------
  160. initialization
  161. //------------------------------------------------
  162. RegisterRasterFormat('pgm', 'Portable Graymap', TGLPGMImage);
  163. end.