| @@ -245,22 +245,22 @@ unit glBitmap; | |||||
| // If you enable SDL_image all other libraries will be ignored! | // If you enable SDL_image all other libraries will be ignored! | ||||
| {.$DEFINE GLB_SDL_IMAGE} | {.$DEFINE GLB_SDL_IMAGE} | ||||
| // activate to enable png support with the unit pngimage. You can download it from http://pngdelphi.sourceforge.net/ | |||||
| // PNG ///////////////////////////////////////////////////////////////////////////////////////////// | |||||
| // activate to enable png support with the unit pngimage -> http://pngdelphi.sourceforge.net/ | |||||
| // if you enable pngimage the libPNG will be ignored | // if you enable pngimage the libPNG will be ignored | ||||
| {.$DEFINE GLB_PNGIMAGE} | {.$DEFINE GLB_PNGIMAGE} | ||||
| // activate to use the libPNG http://www.libpng.org/ | |||||
| // You will need an aditional header. | |||||
| // http://www.opengl24.de/index.php?cat=header&file=libpng | |||||
| {$DEFINE GLB_LIB_PNG} | |||||
| // activate to use the libPNG -> http://www.libpng.org/ | |||||
| // You will need an aditional header -> http://www.opengl24.de/index.php?cat=header&file=libpng | |||||
| {.$DEFINE GLB_LIB_PNG} | |||||
| // JPEG //////////////////////////////////////////////////////////////////////////////////////////// | |||||
| // if you enable delphi jpegs the libJPEG will be ignored | // if you enable delphi jpegs the libJPEG will be ignored | ||||
| {.$DEFINE GLB_DELPHI_JPEG} | {.$DEFINE GLB_DELPHI_JPEG} | ||||
| // activateto use the libJPEG http://www.ijg.org/ | |||||
| // You will need an aditional header. | |||||
| // http://www.opengl24.de/index.php?cat=header&file=libjpeg | |||||
| {.$DEFINE GLB_LIB_JPEG} | |||||
| // activate to use the libJPEG -> http://www.ijg.org/ | |||||
| // You will need an aditional header -> http://www.opengl24.de/index.php?cat=header&file=libjpeg | |||||
| {$DEFINE GLB_LIB_JPEG} | |||||
| ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// | ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// | ||||
| @@ -1777,17 +1777,10 @@ begin | |||||
| result := result + [ftPNG]; | result := result + [ftPNG]; | ||||
| {$ENDIF} | {$ENDIF} | ||||
| (* TODO | |||||
| {$IFDEF GLB_SUPPORT_JPEG_WRITE} | {$IFDEF GLB_SUPPORT_JPEG_WRITE} | ||||
| if Format in [ | |||||
| tfAlpha4, tfAlpha8, tfAlpha12, tfAlpha16, | |||||
| tfLuminance4, tfLuminance8, tfLuminance12, tfLuminance16, | |||||
| tfR3G3B2, tfRGB4, tfRGB5, tfRGB8, tfRGB10, tfRGB12, tfRGB16, | |||||
| tfDepth16, tfDepth24, tfDepth32] | |||||
| then | |||||
| if aFormat in [tfAlpha8, tfLuminance8, tfRGB8, tfBGR8] then | |||||
| result := result + [ftJPEG]; | result := result + [ftJPEG]; | ||||
| {$ENDIF} | {$ENDIF} | ||||
| *) | |||||
| end; | end; | ||||
| ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// | ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// | ||||
| @@ -5543,7 +5536,7 @@ begin | |||||
| try | try | ||||
| // signature | // signature | ||||
| StreamPos := aStream.Position; | StreamPos := aStream.Position; | ||||
| aStream.Read(signature, 8); | |||||
| aStream.Read(signature{%H-}, 8); | |||||
| aStream.Position := StreamPos; | aStream.Position := StreamPos; | ||||
| if png_check_sig(@signature, 8) <> 0 then begin | if png_check_sig(@signature, 8) <> 0 then begin | ||||
| @@ -5906,6 +5899,35 @@ type | |||||
| DestBuffer: array [1..4096] of byte; | DestBuffer: array [1..4096] of byte; | ||||
| end; | end; | ||||
| procedure glBitmap_libJPEG_error_exit(cinfo: j_common_ptr); cdecl; | |||||
| begin | |||||
| //DUMMY | |||||
| end; | |||||
| procedure glBitmap_libJPEG_output_message(cinfo: j_common_ptr); cdecl; | |||||
| begin | |||||
| //DUMMY | |||||
| end; | |||||
| procedure glBitmap_libJPEG_init_source(cinfo: j_decompress_ptr); cdecl; | |||||
| begin | |||||
| //DUMMY | |||||
| end; | |||||
| procedure glBitmap_libJPEG_term_source(cinfo: j_decompress_ptr); cdecl; | |||||
| begin | |||||
| //DUMMY | |||||
| end; | |||||
| procedure glBitmap_libJPEG_init_destination(cinfo: j_compress_ptr); cdecl; | |||||
| begin | |||||
| //DUMMY | |||||
| end; | |||||
| ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// | ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// | ||||
| function glBitmap_libJPEG_fill_input_buffer(cinfo: j_decompress_ptr): boolean; cdecl; | function glBitmap_libJPEG_fill_input_buffer(cinfo: j_decompress_ptr): boolean; cdecl; | ||||
| var | var | ||||
| @@ -6024,12 +6046,14 @@ var | |||||
| jpeg: jpeg_decompress_struct; | jpeg: jpeg_decompress_struct; | ||||
| jpeg_err: jpeg_error_mgr; | jpeg_err: jpeg_error_mgr; | ||||
| IntFormat: TglBitmapInternalFormat; | |||||
| IntFormat: TglBitmapFormat; | |||||
| pImage: pByte; | pImage: pByte; | ||||
| TempHeight, TempWidth: Integer; | TempHeight, TempWidth: Integer; | ||||
| pTemp: pByte; | pTemp: pByte; | ||||
| Row: Integer; | Row: Integer; | ||||
| FormatDesc: TFormatDescriptor; | |||||
| begin | begin | ||||
| result := false; | result := false; | ||||
| @@ -6038,18 +6062,18 @@ begin | |||||
| try | try | ||||
| // reading first two bytes to test file and set cursor back to begin | // reading first two bytes to test file and set cursor back to begin | ||||
| StreamPos := Stream.Position; | |||||
| Stream.Read(Temp[0], 2); | |||||
| Stream.Position := StreamPos; | |||||
| StreamPos := aStream.Position; | |||||
| aStream.Read({%H-}Temp[0], 2); | |||||
| aStream.Position := StreamPos; | |||||
| // if Bitmap then read file. | // if Bitmap then read file. | ||||
| if ((Temp[0] = $FF) and (Temp[1] = $D8)) then begin | if ((Temp[0] = $FF) and (Temp[1] = $D8)) then begin | ||||
| FillChar(jpeg, SizeOf(jpeg_decompress_struct), $00); | |||||
| FillChar(jpeg_err, SizeOf(jpeg_error_mgr), $00); | |||||
| FillChar(jpeg{%H-}, SizeOf(jpeg_decompress_struct), $00); | |||||
| FillChar(jpeg_err{%H-}, SizeOf(jpeg_error_mgr), $00); | |||||
| // error managment | // error managment | ||||
| jpeg.err := jpeg_std_error(@jpeg_err); | jpeg.err := jpeg_std_error(@jpeg_err); | ||||
| jpeg_err.error_exit := glBitmap_libJPEG_error_exit; | |||||
| jpeg_err.error_exit := glBitmap_libJPEG_error_exit; | |||||
| jpeg_err.output_message := glBitmap_libJPEG_output_message; | jpeg_err.output_message := glBitmap_libJPEG_output_message; | ||||
| // decompression struct | // decompression struct | ||||
| @@ -6069,7 +6093,7 @@ begin | |||||
| pub.bytes_in_buffer := 0; // forces fill_input_buffer on first read | pub.bytes_in_buffer := 0; // forces fill_input_buffer on first read | ||||
| pub.next_input_byte := nil; // until buffer loaded | pub.next_input_byte := nil; // until buffer loaded | ||||
| SrcStream := Stream; | |||||
| SrcStream := aStream; | |||||
| end; | end; | ||||
| // set global decoding state | // set global decoding state | ||||
| @@ -6083,11 +6107,11 @@ begin | |||||
| JCS_GRAYSCALE: | JCS_GRAYSCALE: | ||||
| begin | begin | ||||
| jpeg.out_color_space := JCS_GRAYSCALE; | jpeg.out_color_space := JCS_GRAYSCALE; | ||||
| IntFormat := ifLuminance; | |||||
| IntFormat := tfLuminance8; | |||||
| end; | end; | ||||
| else | else | ||||
| jpeg.out_color_space := JCS_RGB; | jpeg.out_color_space := JCS_RGB; | ||||
| IntFormat := ifRGB8; | |||||
| IntFormat := tfRGB8; | |||||
| end; | end; | ||||
| // reading image | // reading image | ||||
| @@ -6096,14 +6120,16 @@ begin | |||||
| TempHeight := jpeg.output_height; | TempHeight := jpeg.output_height; | ||||
| TempWidth := jpeg.output_width; | TempWidth := jpeg.output_width; | ||||
| FormatDesc := TFormatDescriptor.Get(IntFormat); | |||||
| // creating new image | // creating new image | ||||
| GetMem(pImage, FormatGetImageSize(glBitmapPosition(TempWidth, TempHeight), IntFormat)); | |||||
| GetMem(pImage, FormatDesc.GetSize(TempWidth, TempHeight)); | |||||
| try | try | ||||
| pTemp := pImage; | pTemp := pImage; | ||||
| for Row := 0 to TempHeight -1 do begin | for Row := 0 to TempHeight -1 do begin | ||||
| jpeg_read_scanlines(@jpeg, @pTemp, 1); | jpeg_read_scanlines(@jpeg, @pTemp, 1); | ||||
| Inc(pTemp, Trunc(FormatGetSize(IntFormat) * TempWidth)); | |||||
| Inc(pTemp, FormatDesc.GetSize(TempWidth, 1)); | |||||
| end; | end; | ||||
| // finish decompression | // finish decompression | ||||
| @@ -6162,9 +6188,9 @@ end; | |||||
| {$ENDIF} | {$ENDIF} | ||||
| {$IFDEF GLB_SUPPORT_JPEG_WRITE} | {$IFDEF GLB_SUPPORT_JPEG_WRITE} | ||||
| {$IF DEFEFINED(GLB_LIB_JPEG)} | |||||
| {$IF DEFINED(GLB_LIB_JPEG)} | |||||
| ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// | ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// | ||||
| procedure TglBitmap.SaveJPEG(Stream: TStream); | |||||
| procedure TglBitmap.SaveJPEG(const aStream: TStream); | |||||
| var | var | ||||
| jpeg: jpeg_compress_struct; | jpeg: jpeg_compress_struct; | ||||
| jpeg_err: jpeg_error_mgr; | jpeg_err: jpeg_error_mgr; | ||||
| @@ -6186,18 +6212,18 @@ var | |||||
| begin | begin | ||||
| if not (ftJPEG in FormatGetSupportedFiles(Format)) then | if not (ftJPEG in FormatGetSupportedFiles(Format)) then | ||||
| raise EglBitmapUnsupportedInternalFormat.Create('SaveJpg - ' + UNSUPPORTED_INTERNAL_FORMAT); | |||||
| raise EglBitmapUnsupportedFormat.Create(Format); | |||||
| if not init_libJPEG then | if not init_libJPEG then | ||||
| raise Exception.Create('SaveJPG - unable to initialize libJPEG.'); | raise Exception.Create('SaveJPG - unable to initialize libJPEG.'); | ||||
| try | try | ||||
| FillChar(jpeg, SizeOf(jpeg_compress_struct), $00); | |||||
| FillChar(jpeg_err, SizeOf(jpeg_error_mgr), $00); | |||||
| FillChar(jpeg{%H-}, SizeOf(jpeg_compress_struct), $00); | |||||
| FillChar(jpeg_err{%H-}, SizeOf(jpeg_error_mgr), $00); | |||||
| // error managment | // error managment | ||||
| jpeg.err := jpeg_std_error(@jpeg_err); | jpeg.err := jpeg_std_error(@jpeg_err); | ||||
| jpeg_err.error_exit := glBitmap_libJPEG_error_exit; | |||||
| jpeg_err.error_exit := glBitmap_libJPEG_error_exit; | |||||
| jpeg_err.output_message := glBitmap_libJPEG_output_message; | jpeg_err.output_message := glBitmap_libJPEG_output_message; | ||||
| // compression struct | // compression struct | ||||
| @@ -6215,21 +6241,21 @@ begin | |||||
| pub.next_output_byte := @DestBuffer[1]; | pub.next_output_byte := @DestBuffer[1]; | ||||
| pub.free_in_buffer := Length(DestBuffer); | pub.free_in_buffer := Length(DestBuffer); | ||||
| DestStream := Stream; | |||||
| DestStream := aStream; | |||||
| end; | end; | ||||
| // very important state | // very important state | ||||
| jpeg.global_state := CSTATE_START; | jpeg.global_state := CSTATE_START; | ||||
| jpeg.image_width := Width; | jpeg.image_width := Width; | ||||
| jpeg.image_height := Height; | jpeg.image_height := Height; | ||||
| case InternalFormat of | |||||
| ifAlpha, ifLuminance, ifDepth8: begin | |||||
| case Format of | |||||
| tfAlpha8, tfLuminance8: begin | |||||
| jpeg.input_components := 1; | jpeg.input_components := 1; | ||||
| jpeg.in_color_space := JCS_GRAYSCALE; | |||||
| jpeg.in_color_space := JCS_GRAYSCALE; | |||||
| end; | end; | ||||
| ifRGB8, ifBGR8: begin | |||||
| tfRGB8, tfBGR8: begin | |||||
| jpeg.input_components := 3; | jpeg.input_components := 3; | ||||
| jpeg.in_color_space := JCS_RGB; | |||||
| jpeg.in_color_space := JCS_RGB; | |||||
| end; | end; | ||||
| end; | end; | ||||
| @@ -6238,7 +6264,7 @@ begin | |||||
| jpeg_start_compress(@jpeg, true); | jpeg_start_compress(@jpeg, true); | ||||
| pTemp := Data; | pTemp := Data; | ||||
| if InternalFormat = ifBGR8 then | |||||
| if Format = tfBGR8 then | |||||
| GetMem(pTemp2, fRowSize) | GetMem(pTemp2, fRowSize) | ||||
| else | else | ||||
| pTemp2 := pTemp; | pTemp2 := pTemp; | ||||
| @@ -6246,7 +6272,7 @@ begin | |||||
| try | try | ||||
| for Row := 0 to jpeg.image_height -1 do begin | for Row := 0 to jpeg.image_height -1 do begin | ||||
| // prepare row | // prepare row | ||||
| if InternalFormat = ifBGR8 then | |||||
| if Format = tfBGR8 then | |||||
| CopyRow(pTemp2, pTemp) | CopyRow(pTemp2, pTemp) | ||||
| else | else | ||||
| pTemp2 := pTemp; | pTemp2 := pTemp; | ||||
| @@ -6257,7 +6283,7 @@ begin | |||||
| end; | end; | ||||
| finally | finally | ||||
| // free memory | // free memory | ||||
| if InternalFormat = ifBGR8 then | |||||
| if Format = tfBGR8 then | |||||
| FreeMem(pTemp2); | FreeMem(pTemp2); | ||||
| end; | end; | ||||
| jpeg_finish_compress(@jpeg); | jpeg_finish_compress(@jpeg); | ||||