C/C++ Struct to FB Type conversation

General FreeBASIC programming questions.
Post Reply
UEZ
Posts: 988
Joined: May 05, 2017 19:59
Location: Germany

C/C++ Struct to FB Type conversation

Post by UEZ »

I am not very familiar with C/C++ structures.

Are my conversations correct?

C/C++ struct:

Code: Select all

// Signature for output function. Should return true if writing was successful.
// data/data_size is the segment of data to write, and 'picture' is for
// reference (and so one can make use of picture->custom_ptr).
typedef int (*WebPWriterFunction)(const uint8_t* data, size_t data_size,
                                  const WebPPicture* picture);
                                  
typedef int (*WebPProgressHook)(int percent, const WebPPicture* picture);

// Color spaces.
typedef enum WebPEncCSP {
  // chroma sampling
  WEBP_YUV420  = 0,        // 4:2:0
  WEBP_YUV420A = 4,        // alpha channel variant
  WEBP_CSP_UV_MASK = 3,    // bit-mask to get the UV sampling factors
  WEBP_CSP_ALPHA_BIT = 4   // bit that is set if alpha is present
} WebPEncCSP;

struct WebPAuxStats {
  int coded_size;         // final size

  float PSNR[5];          // peak-signal-to-noise ratio for Y/U/V/All/Alpha
  int block_count[3];     // number of intra4/intra16/skipped macroblocks
  int header_bytes[2];    // approximate number of bytes spent for header
                          // and mode-partition #0
  int residual_bytes[3][4];  // approximate number of bytes spent for
                             // DC/AC/uv coefficients for each (0..3) segments.
  int segment_size[4];    // number of macroblocks in each segments
  int segment_quant[4];   // quantizer values for each segments
  int segment_level[4];   // filtering strength for each segments [0..63]

  int alpha_data_size;    // size of the transparency data
  int layer_data_size;    // size of the enhancement layer data

  // lossless encoder statistics
  uint32_t lossless_features;  // bit0:predictor bit1:cross-color transform
                               // bit2:subtract-green bit3:color indexing
  int histogram_bits;          // number of precision bits of histogram
  int transform_bits;          // precision bits for transform
  int cache_bits;              // number of bits for color cache lookup
  int palette_size;            // number of color in palette, if used
  int lossless_size;           // final lossless size
  int lossless_hdr_size;       // lossless header (transform, huffman etc) size
  int lossless_data_size;      // lossless image data size

  uint32_t pad[2];        // padding for later use
};

// Main exchange structure (input samples, output bytes, statistics)
struct WebPPicture {
  //   INPUT
  //////////////
  // Main flag for encoder selecting between ARGB or YUV input.
  // It is recommended to use ARGB input (*argb, argb_stride) for lossless
  // compression, and YUV input (*y, *u, *v, etc.) for lossy compression
  // since these are the respective native colorspace for these formats.
  int use_argb;

  // YUV input (mostly used for input to lossy compression)
  WebPEncCSP colorspace;     // colorspace: should be YUV420 for now (=Y'CbCr).
  int width, height;         // dimensions (less or equal to WEBP_MAX_DIMENSION)
  uint8_t *y, *u, *v;        // pointers to luma/chroma planes.
  int y_stride, uv_stride;   // luma/chroma strides.
  uint8_t* a;                // pointer to the alpha plane
  int a_stride;              // stride of the alpha plane
  uint32_t pad1[2];          // padding for later use

  // ARGB input (mostly used for input to lossless compression)
  uint32_t* argb;            // Pointer to argb (32 bit) plane.
  int argb_stride;           // This is stride in pixels units, not bytes.
  uint32_t pad2[3];          // padding for later use

  //   OUTPUT
  ///////////////
  // Byte-emission hook, to store compressed bytes as they are ready.
  WebPWriterFunction writer;  // can be NULL
  void* custom_ptr;           // can be used by the writer.

  // map for extra information (only for lossy compression mode)
  int extra_info_type;    // 1: intra type, 2: segment, 3: quant
                          // 4: intra-16 prediction mode,
                          // 5: chroma prediction mode,
                          // 6: bit cost, 7: distortion
  uint8_t* extra_info;    // if not NULL, points to an array of size
                          // ((width + 15) / 16) * ((height + 15) / 16) that
                          // will be filled with a macroblock map, depending
                          // on extra_info_type.

  //   STATS AND REPORTS
  ///////////////////////////
  // Pointer to side statistics (updated only if not NULL)
  WebPAuxStats* stats;

  // Error code for the latest error encountered during encoding
  WebPEncodingError error_code;

  // If not NULL, report progress during encoding.
  WebPProgressHook progress_hook;

  void* user_data;        // this field is free to be set to any value and
                          // used during callbacks (like progress-report e.g.).

  uint32_t pad3[3];       // padding for later use

  // Unused for now
  uint8_t *pad4, *pad5;
  uint32_t pad6[8];       // padding for later use

  // PRIVATE FIELDS
  ////////////////////
  void* memory_;          // row chunk of memory for yuva planes
  void* memory_argb_;     // and for argb too.
  void* pad7[2];          // padding for later use
};
Full code can be found here: encode.h

FB Type Conversation:

Code: Select all

Type int8_t As Byte
Type int16_t As Short
Type int32_t As Long
Type int64_t As Longint
	
Type uint8_t As Ubyte
Type uint16_t as Ushort
Type uint32_t As Ulong
Type uint64_t As Ulongint

Enum WebPEncCSP
  ' chroma sampling
  WEBP_YUV420  = 0,        ' 4:2:0
  WEBP_YUV420A = 4,        ' alpha channel variant
  WEBP_CSP_UV_MASK = 3,    ' bit-mask to get the UV sampling factors
  WEBP_CSP_ALPHA_BIT = 4   ' bit that is set if alpha is present
End Enum

Enum WebPEncodingError
  VP8_ENC_OK = 0,
  VP8_ENC_ERROR_OUT_OF_MEMORY,            ' memory error allocating objects
  VP8_ENC_ERROR_BITSTREAM_OUT_OF_MEMORY,  ' memory error while flushing bits
  VP8_ENC_ERROR_NULL_PARAMETER,           ' a pointer parameter is NULL
  VP8_ENC_ERROR_INVALID_CONFIGURATION,    ' configuration is invalid
  VP8_ENC_ERROR_BAD_DIMENSION,            ' picture has invalid width/height
  VP8_ENC_ERROR_PARTITION0_OVERFLOW,      ' partition is bigger than 512k
  VP8_ENC_ERROR_PARTITION_OVERFLOW,       ' partition is bigger than 16M
  VP8_ENC_ERROR_BAD_WRITE,                ' error while flushing bytes
  VP8_ENC_ERROR_FILE_TOO_BIG,             ' file is bigger than 4G
  VP8_ENC_ERROR_USER_ABORT,               ' abort request by user
  VP8_ENC_ERROR_LAST                      ' list terminator. always last.
End Enum


Type tWebPAuxStats
  As Long coded_size         	' final size

  ' peak-signal-to-noise ratio for Y/U/V/All/Alpha
  As Single PSNRY, PSNRU, PSNRV, PSNRALL, PSNRAlpha
  
  ' number of intra4/intra16/skipped macroblocks
  As Long block_count_intra4, block_count_intra16, block_count_skipped
  
  ' approximate number of bytes spent for header and mode-partition #0
  As Long header_bytes, mode_partition_0
  
  ' approximate number of bytes spent for
  ' DC/AC/uv coefficients for each (0..3) segments.
  As Long residual_bytes_DC_segments0, residual_bytes_AC_segments0, residual_bytes_uv_segments0 
  As Long residual_bytes_DC_segments1, residual_bytes_AC_segments1, residual_bytes_uv_segments1
  As Long residual_bytes_DC_segments2, residual_bytes_AC_segments2, residual_bytes_uv_segments2 
  As Long residual_bytes_DC_segments3, residual_bytes_AC_segments3, residual_bytes_uv_segments3 
  
  ' number of macroblocks in each segments  
  As Long segment_size_segments0, segment_size_segments1, segment_size_segments2, segment_size_segments3
  
  ' quantizer values for each segments  
  As Long segment_quant0, segment_quant1, segment_quant2, segment_quant3
  
  ' filtering strength for each segments (0..63]
  As Long segment_level_segments0, segment_level_segments1, segment_level_segments2, segment_level_segments3

  As Long alpha_data_size    	' size of the transparency data
  As Long layer_data_size    	' size of the enhancement layer data

  ' lossless encoder statistics
  As uint32_t lossless_features   ' bit0:predictor bit1:cross-color transform
								  ' bit2:subtract-green bit3:color indexing
  As Long histogram_bits          ' number of precision bits of histogram
  As Long transform_bits          ' precision bits for transform
  As Long cache_bits              ' number of bits for color cache lookup
  As Long palette_size            ' number of color in palette, if used
  
  As Long lossless_size           ' final lossless size 
  As Long lossless_hdr_size       ' lossless header (transform, huffman etc) size
  As Long lossless_data_size      ' lossless image data size

  As uint32_t pad(2)       		  ' padding for later use
End Type

Type tWebPPicture
  '   INPUT
  ' Main flag for encoder selecting between ARGB or YUV input.
  ' It is recommended to use ARGB input (*argb, argb_stride) for lossless
  ' compression, and YUV input (*y, *u, *v, etc.) for lossy compression
  ' since these are the respective native colorspace for these formats.
  As Long use_argb

  ' YUV input (mostly used for input to lossy compression)
  As WebPEncCSP colorspace     	' colorspace: should be YUV420 for now (=Y'CbCr).
  As Long width, height         ' dimensions (less or equal to WEBP_MAX_DIMENSION)
  As uint8_t Ptr y, u, v        ' pointers to luma/chroma planes.
  As Long y_stride, uv_stride   ' luma/chroma strides.
  As uint8_t Ptr a              ' pointer to the alpha plane
  As Long a_stride              ' stride of the alpha plane
  As uint32_t pad1(2)           ' padding for later use

  ' ARGB input (mostly used for input to lossless compression)
  As uint32_t Ptr argb          ' Pointer to argb (32 bit) plane.
  As Long argb_stride           ' This is stride in pixels units, not bytes.
  As uint32_t pad2(3)         	' padding for later use

  '   OUTPUT
  ' Byte-emission hook, to store compressed bytes as they are ready.
  As uint32_t Ptr writer	 	' WebPWriterFunction result can be NULL
  As uint32_t Ptr custom_ptr    ' can be used by the writer.

  ' map for extra information (only for lossy compression mode)
  As Long extra_info_type    	' 1: intra type, 2: segment, 3: quant
								' 4: intra-16 prediction mode,
								' 5: chroma prediction mode,
								' 6: bit cost, 7: distortion
  As uint8_t Ptr extra_info    	' if not NULL, points to an array of size
								' ((width + 15) / 16) * ((height + 15) / 16) that
								' will be filled with a macroblock map, depending
								' on extra_info_type.

  '   STATS AND REPORTS
  ' Pointer to side statistics (updated only if not NULL)
  As tWebPAuxStats Ptr stats

  ' Error code for the latest error encountered during encoding
  As WebPEncodingError error_code

  ' If not NULL, report progress during encoding.
  As int32_t Ptr progress_hook	' WebPProgressHook result

  As int32_t Ptr user_data      ' this field is free to be set to any value and
								' used during callbacks (like progress-report e.g.).

  As uint32_t pad3(3)       	' padding for later use

  ' Unused for now
  As uint8_t Ptr pad4, pad5
  As uint32_t pad6(8)       	' padding for later use
	
  ' PRIVATE FIELDS
  As int32_t Ptr  memory_          	' row chunk of memory for yuva planes
  As int32_t Ptr  memory_argb_     	' and for argb too.
  As int32_t Ptr  pad7(2)      		' padding for later use
End Type

Dim As tWebPPicture test
? Sizeof(test)
Sleep

Thx.
Last edited by UEZ on May 02, 2022 18:51, edited 5 times in total.
dodicat
Posts: 7983
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: C/C++ Struct to FB Type conversation

Post by dodicat »

I must admit UEZ that with these things I cheat if at all possible.
With your second set on 64 bits :
int main()
{
struct WebPAuxStats test;
printf("%d",sizeof(test));

}

gives 188

and fb
dim as tWebPAuxStats test
print sizeof(test)
gives
192
UEZ
Posts: 988
Joined: May 05, 2017 19:59
Location: Germany

Re: C/C++ Struct to FB Type conversation

Post by UEZ »

dodicat wrote: May 02, 2022 18:25 I must admit UEZ that with these things I cheat if at all possible.
With your second set on 64 bits :
int main()
{
struct WebPAuxStats test;
printf("%d",sizeof(test));

}

gives 188

and fb
dim as tWebPAuxStats test
print sizeof(test)
gives
192
Thanks dodicat. How do you compile the C/C++ code?
srvaldez
Posts: 3379
Joined: Sep 25, 2005 21:54

Re: C/C++ Struct to FB Type conversation

Post by srvaldez »

hi UEZ
in this instance fbfrog seems to have no trouble

Code: Select all

#pragma once

extern "C"

type WebPPicture as WebPPicture_
type WebPWriterFunction as function(byval data as const ubyte ptr, byval data_size as uinteger, byval picture as const WebPPicture ptr) as long
type WebPProgressHook as function(byval percent as long, byval picture as const WebPPicture ptr) as long

type WebPEncCSP as long
enum
	WEBP_YUV420 = 0
	WEBP_YUV420A = 4
	WEBP_CSP_UV_MASK = 3
	WEBP_CSP_ALPHA_BIT = 4
end enum

type WebPPicture_
	use_argb as long
	colorspace as WebPEncCSP
	width as long
	height as long
	y as ubyte ptr
	u as ubyte ptr
	v as ubyte ptr
	y_stride as long
	uv_stride as long
	a as ubyte ptr
	a_stride as long
	pad1(0 to 1) as ulong
	argb as ulong ptr
	argb_stride as long
	pad2(0 to 2) as ulong
	writer as WebPWriterFunction
	custom_ptr as any ptr
	extra_info_type as long
	extra_info as ubyte ptr
	stats as WebPAuxStats ptr
	error_code as WebPEncodingError
	progress_hook as WebPProgressHook
	user_data as any ptr
	pad3(0 to 2) as ulong
	pad4 as ubyte ptr
	pad5 as ubyte ptr
	pad6(0 to 7) as ulong
	memory_ as any ptr
	memory_argb_ as any ptr
	pad7(0 to 1) as any ptr
end type

end extern
but sometimes you need to make some tweaks to the translation, you won't know until you try to compile
dodicat
Posts: 7983
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: C/C++ Struct to FB Type conversation

Post by dodicat »

I have Dev-Cpp running from the fb distro in the news section:
The MinGW-w64 toolchains used for the main win32/win64 builds can be found here:
winlibs-mingw-w64-i686-9.3.0-7.0.0-r3-sjlj.7z
winlibs-mingw-w64-x86_64-9.3.0-7.0.0-r3-sjlj.7z

I copied the 64 bit distro to MinGW folder on the C drive
I have put the bin folder on the system path.
UEZ
Posts: 988
Joined: May 05, 2017 19:59
Location: Germany

Re: C/C++ Struct to FB Type conversation

Post by UEZ »

Thanks srvaldez -> FBFrog converted the encode.h this way:

Code: Select all

#pragma once

#include once "crt/stddef.bi"
#include once "inttypes.bi"

extern "C"

#define WEBP_WEBP_ENCODE_H_
#define WEBP_WEBP_TYPES_H_
'' TODO: #define WEBP_INLINE inline
'' TODO: # define WEBP_EXTERN extern __attribute__ ((visibility ("default")))
#define WEBP_ABI_IS_INCOMPATIBLE(a, b) (((a) shr 8) <> ((b) shr 8))
const WEBP_ENCODER_ABI_VERSION = &h020e
declare function WebPGetEncoderVersion() as long
declare function WebPEncodeRGB(byval rgb as const ubyte ptr, byval width as long, byval height as long, byval stride as long, byval quality_factor as single, byval output as ubyte ptr ptr) as uinteger
declare function WebPEncodeBGR(byval bgr as const ubyte ptr, byval width as long, byval height as long, byval stride as long, byval quality_factor as single, byval output as ubyte ptr ptr) as uinteger
declare function WebPEncodeRGBA(byval rgba as const ubyte ptr, byval width as long, byval height as long, byval stride as long, byval quality_factor as single, byval output as ubyte ptr ptr) as uinteger
declare function WebPEncodeBGRA(byval bgra as const ubyte ptr, byval width as long, byval height as long, byval stride as long, byval quality_factor as single, byval output as ubyte ptr ptr) as uinteger
declare function WebPEncodeLosslessRGB(byval rgb as const ubyte ptr, byval width as long, byval height as long, byval stride as long, byval output as ubyte ptr ptr) as uinteger
declare function WebPEncodeLosslessBGR(byval bgr as const ubyte ptr, byval width as long, byval height as long, byval stride as long, byval output as ubyte ptr ptr) as uinteger
declare function WebPEncodeLosslessRGBA(byval rgba as const ubyte ptr, byval width as long, byval height as long, byval stride as long, byval output as ubyte ptr ptr) as uinteger
declare function WebPEncodeLosslessBGRA(byval bgra as const ubyte ptr, byval width as long, byval height as long, byval stride as long, byval output as ubyte ptr ptr) as uinteger
declare sub WebPFree(byval ptr as any ptr)

type WebPImageHint as long
enum
	WEBP_HINT_DEFAULT = 0
	WEBP_HINT_PICTURE
	WEBP_HINT_PHOTO
	WEBP_HINT_GRAPH
	WEBP_HINT_LAST
end enum

type WebPConfig
	lossless as long
	quality as single
	method as long
	image_hint as WebPImageHint
	target_size as long
	target_PSNR as single
	segments as long
	sns_strength as long
	filter_strength as long
	filter_sharpness as long
	filter_type as long
	autofilter as long
	alpha_compression as long
	alpha_filtering as long
	alpha_quality as long
	pass as long
	show_compressed as long
	preprocessing as long
	partitions as long
	partition_limit as long
	emulate_jpeg_size as long
	thread_level as long
	low_memory as long
	near_lossless as long
	exact as long
	use_delta_palette as long
	use_sharp_yuv as long
	pad(0 to 1) as ulong
end type

type WebPPreset as long
enum
	WEBP_PRESET_DEFAULT = 0
	WEBP_PRESET_PICTURE
	WEBP_PRESET_PHOTO
	WEBP_PRESET_DRAWING
	WEBP_PRESET_ICON
	WEBP_PRESET_TEXT
end enum

declare function WebPConfigInitInternal(byval as WebPConfig ptr, byval as WebPPreset, byval as single, byval as long) as long
#define WebPConfigInit(config) clng(WebPConfigInitInternal((config), WEBP_PRESET_DEFAULT, 75.f, &h020e))
#define WebPConfigPreset(config, preset, quality) clng(WebPConfigInitInternal((config), (preset), (quality), &h020e))
declare function WebPConfigLosslessPreset(byval config as WebPConfig ptr, byval level as long) as long
declare function WebPValidateConfig(byval config as const WebPConfig ptr) as long

type WebPAuxStats
	coded_size as long
	PSNR(0 to 4) as single
	block_count(0 to 2) as long
	header_bytes(0 to 1) as long
	residual_bytes(0 to 2, 0 to 3) as long
	segment_size(0 to 3) as long
	segment_quant(0 to 3) as long
	segment_level(0 to 3) as long
	alpha_data_size as long
	layer_data_size as long
	lossless_features as ulong
	histogram_bits as long
	transform_bits as long
	cache_bits as long
	palette_size as long
	lossless_size as long
	lossless_hdr_size as long
	lossless_data_size as long
	pad(0 to 1) as ulong
end type

type WebPPicture as WebPPicture_
type WebPWriterFunction as function(byval data as const ubyte ptr, byval data_size as uinteger, byval picture as const WebPPicture ptr) as long

type WebPMemoryWriter
	mem as ubyte ptr
	size as uinteger
	max_size as uinteger
	pad(0 to 0) as ulong
end type

declare sub WebPMemoryWriterInit(byval writer as WebPMemoryWriter ptr)
declare sub WebPMemoryWriterClear(byval writer as WebPMemoryWriter ptr)
declare function WebPMemoryWrite(byval data as const ubyte ptr, byval data_size as uinteger, byval picture as const WebPPicture ptr) as long
type WebPProgressHook as function(byval percent as long, byval picture as const WebPPicture ptr) as long

type WebPEncCSP as long
enum
	WEBP_YUV420 = 0
	WEBP_YUV420A = 4
	WEBP_CSP_UV_MASK = 3
	WEBP_CSP_ALPHA_BIT = 4
end enum

type WebPEncodingError as long
enum
	VP8_ENC_OK = 0
	VP8_ENC_ERROR_OUT_OF_MEMORY
	VP8_ENC_ERROR_BITSTREAM_OUT_OF_MEMORY
	VP8_ENC_ERROR_NULL_PARAMETER
	VP8_ENC_ERROR_INVALID_CONFIGURATION
	VP8_ENC_ERROR_BAD_DIMENSION
	VP8_ENC_ERROR_PARTITION0_OVERFLOW
	VP8_ENC_ERROR_PARTITION_OVERFLOW
	VP8_ENC_ERROR_BAD_WRITE
	VP8_ENC_ERROR_FILE_TOO_BIG
	VP8_ENC_ERROR_USER_ABORT
	VP8_ENC_ERROR_LAST
end enum

const WEBP_MAX_DIMENSION = 16383

type WebPPicture_
	use_argb as long
	colorspace as WebPEncCSP
	width as long
	height as long
	y as ubyte ptr
	u as ubyte ptr
	v as ubyte ptr
	y_stride as long
	uv_stride as long
	a as ubyte ptr
	a_stride as long
	pad1(0 to 1) as ulong
	argb as ulong ptr
	argb_stride as long
	pad2(0 to 2) as ulong
	writer as WebPWriterFunction
	custom_ptr as any ptr
	extra_info_type as long
	extra_info as ubyte ptr
	stats as WebPAuxStats ptr
	error_code as WebPEncodingError
	progress_hook as WebPProgressHook
	user_data as any ptr
	pad3(0 to 2) as ulong
	pad4 as ubyte ptr
	pad5 as ubyte ptr
	pad6(0 to 7) as ulong
	memory_ as any ptr
	memory_argb_ as any ptr
	pad7(0 to 1) as any ptr
end type

declare function WebPPictureInitInternal(byval as WebPPicture ptr, byval as long) as long
#define WebPPictureInit(picture) clng(WebPPictureInitInternal((picture), &h020e))
declare function WebPPictureAlloc(byval picture as WebPPicture ptr) as long
declare sub WebPPictureFree(byval picture as WebPPicture ptr)
declare function WebPPictureCopy(byval src as const WebPPicture ptr, byval dst as WebPPicture ptr) as long
declare function WebPPlaneDistortion(byval src as const ubyte ptr, byval src_stride as uinteger, byval ref as const ubyte ptr, byval ref_stride as uinteger, byval width as long, byval height as long, byval x_step as uinteger, byval type as long, byval distortion as single ptr, byval result as single ptr) as long
declare function WebPPictureDistortion(byval src as const WebPPicture ptr, byval ref as const WebPPicture ptr, byval metric_type as long, byval result as single ptr) as long
declare function WebPPictureCrop(byval picture as WebPPicture ptr, byval left as long, byval top as long, byval width as long, byval height as long) as long
declare function WebPPictureView(byval src as const WebPPicture ptr, byval left as long, byval top as long, byval width as long, byval height as long, byval dst as WebPPicture ptr) as long
declare function WebPPictureIsView(byval picture as const WebPPicture ptr) as long
declare function WebPPictureRescale(byval pic as WebPPicture ptr, byval width as long, byval height as long) as long
declare function WebPPictureImportRGB(byval picture as WebPPicture ptr, byval rgb as const ubyte ptr, byval rgb_stride as long) as long
declare function WebPPictureImportRGBA(byval picture as WebPPicture ptr, byval rgba as const ubyte ptr, byval rgba_stride as long) as long
declare function WebPPictureImportRGBX(byval picture as WebPPicture ptr, byval rgbx as const ubyte ptr, byval rgbx_stride as long) as long
declare function WebPPictureImportBGR(byval picture as WebPPicture ptr, byval bgr as const ubyte ptr, byval bgr_stride as long) as long
declare function WebPPictureImportBGRA(byval picture as WebPPicture ptr, byval bgra as const ubyte ptr, byval bgra_stride as long) as long
declare function WebPPictureImportBGRX(byval picture as WebPPicture ptr, byval bgrx as const ubyte ptr, byval bgrx_stride as long) as long
declare function WebPPictureARGBToYUVA(byval picture as WebPPicture ptr, byval as WebPEncCSP) as long
declare function WebPPictureARGBToYUVADithered(byval picture as WebPPicture ptr, byval colorspace as WebPEncCSP, byval dithering as single) as long
declare function WebPPictureSharpARGBToYUVA(byval picture as WebPPicture ptr) as long
declare function WebPPictureSmartARGBToYUVA(byval picture as WebPPicture ptr) as long
declare function WebPPictureYUVAToARGB(byval picture as WebPPicture ptr) as long
declare sub WebPCleanupTransparentArea(byval picture as WebPPicture ptr)
declare function WebPPictureHasTransparency(byval picture as const WebPPicture ptr) as long
declare sub WebPBlendAlpha(byval pic as WebPPicture ptr, byval background_rgb as ulong)
declare function WebPEncode(byval config as const WebPConfig ptr, byval picture as WebPPicture ptr) as long

end extern
This helped me a lot to find the issue I had. :)

@dodicat: I will give it a try... Thx.
UEZ
Posts: 988
Joined: May 05, 2017 19:59
Location: Germany

Re: C/C++ Struct to FB Type conversation

Post by UEZ »

If I look to this code here https://developers.google.com/speed/web ... coding_api I can see one of the sections following code:

Code: Select all

// Set up a byte-writing method (write-to-memory, in this case):
WebPMemoryWriter writer;
WebPMemoryWriterInit(&writer);
pic.writer = WebPMemoryWrite; <-- what is this? Accoring to encode.h it is a function
pic.custom_ptr = &writer;
When you look to encode.h WebPMemoryWrite is a function. What is WebPMemoryWrite here?
dodicat
Posts: 7983
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: C/C++ Struct to FB Type conversation

Post by dodicat »

In that state it must be a function pointer.

Code: Select all

#include<stdio.h>
#include<stdlib.h>


typedef   int (*Operation)(int a);

int tst(int x)
{
	return x;
}

struct test{
	Operation  writer;
};


int main()
{
	struct test y;
	y.writer=tst;
	printf("%p\n",y.writer);
	system("pause");
	
}
 
fb

Code: Select all



type Operation as function(as integer) as integer

function tst(x as integer) as integer
	return x
end function

type test
 as Operation  writer
end type


function fbmain() as integer
	dim as test y
	y.writer=@tst
      print y.writer
end function
fbmain
sleep
	

 
UEZ
Posts: 988
Joined: May 05, 2017 19:59
Location: Germany

Re: C/C++ Struct to FB Type conversation

Post by UEZ »

dodicat wrote: May 02, 2022 23:29 In that state it must be a function pointer.

Code: Select all

#include<stdio.h>
#include<stdlib.h>


typedef   int (*Operation)(int a);

int tst(int x)
{
	return x;
}

struct test{
	Operation  writer;
};


int main()
{
	struct test y;
	y.writer=tst;
	printf("%p\n",y.writer);
	system("pause");
	
}
 
fb

Code: Select all



type Operation as function(as integer) as integer

function tst(x as integer) as integer
	return x
end function

type test
 as Operation  writer
end type


function fbmain() as integer
	dim as test y
	y.writer=@tst
      print y.writer
end function
fbmain
sleep
	

 
Thanks dodicat - that helped me a lot to get the function run properly!

I need to do a cast to get it run without any warnings:

Code: Select all

pic.writer = Cast(WebPWriterFunction, @WebPMemoryWrite)
Here the windows test code to encode an image:

Code: Select all

#Ifdef __Fb_64bit__
	#Inclib "webp_x64"
    #Inclib "gdiplus"
    #Include Once "win/gdiplus-c.bi"
#Else
	#Inclib "webp_x86"
    #Include Once "win/gdiplus.bi"
    Using gdiplus
#Endif

#Define CRLF (Chr(13, 10))

Type int8_t As Byte
Type int16_t As Short
Type int32_t As Long
Type int64_t As Longint
	
Type uint8_t As Ubyte
Type uint16_t As Ushort
Type uint32_t As Ulong
Type uint64_t As Ulongint


Extern "C"
#Define WEBP_WEBP_ENCODE_H_
#Define WEBP_WEBP_TYPES_H_
#Define WEBP_ABI_IS_INCOMPATIBLE(a, b) (((a) Shr 8) <> ((b) Shr 8))
Const WEBP_ENCODER_ABI_VERSION = &h020e
Declare Function WebPGetEncoderVersion() As Long
Declare Function WebPEncodeRGB(Byval Rgb As Const Ubyte Ptr, Byval Width As Long, Byval height As Long, Byval stride As Long, Byval quality_factor As Single, Byval Output As Ubyte Ptr Ptr) As Uinteger
Declare Function WebPEncodeBGR(Byval bgr As Const Ubyte Ptr, Byval Width As Long, Byval height As Long, Byval stride As Long, Byval quality_factor As Single, Byval Output As Ubyte Ptr Ptr) As Uinteger
Declare Function WebPEncodeRGBA(Byval Rgba As Const Ubyte Ptr, Byval Width As Long, Byval height As Long, Byval stride As Long, Byval quality_factor As Single, Byval Output As Ubyte Ptr Ptr) As Uinteger
Declare Function WebPEncodeBGRA(Byval bgra As Const Ubyte Ptr, Byval Width As Long, Byval height As Long, Byval stride As Long, Byval quality_factor As Single, Byval Output As Ubyte Ptr Ptr) As Uinteger
Declare Function WebPEncodeLosslessRGB(Byval Rgb As Const Ubyte Ptr, Byval Width As Long, Byval height As Long, Byval stride As Long, Byval Output As Ubyte Ptr Ptr) As Uinteger
Declare Function WebPEncodeLosslessBGR(Byval bgr As Const Ubyte Ptr, Byval Width As Long, Byval height As Long, Byval stride As Long, Byval Output As Ubyte Ptr Ptr) As Uinteger
Declare Function WebPEncodeLosslessRGBA(Byval Rgba As Const Ubyte Ptr, Byval Width As Long, Byval height As Long, Byval stride As Long, Byval Output As Ubyte Ptr Ptr) As Uinteger
Declare Function WebPEncodeLosslessBGRA(Byval bgra As Const Ubyte Ptr, Byval Width As Long, Byval height As Long, Byval stride As Long, Byval Output As Ubyte Ptr Ptr) As Uinteger
Declare Sub WebPFree(Byval Ptr As Any Ptr)

Type WEBP_CSP_MODE As Long
Enum
	MODE_RGB = 0
	MODE_RGBA = 1
	MODE_BGR = 2
	MODE_BGRA = 3
	MODE_ARGB = 4
	MODE_RGBA_4444 = 5
	MODE_RGB_565 = 6
	MODE_rgbA2 = 7
	MODE_bgrA2 = 8
	MODE_Argb2 = 9
	MODE_rgbA2_4444 = 10
	MODE_YUV = 11
	MODE_YUVA = 12
	MODE_LAST = 13
End Enum

Type WebPImageHint As Long
Enum
	WEBP_HINT_DEFAULT = 0
	WEBP_HINT_PICTURE
	WEBP_HINT_PHOTO
	WEBP_HINT_GRAPH
	WEBP_HINT_LAST
End Enum

Type WebPConfig
	lossless As Long
	quality As Single
	method As Long
	image_hint As WebPImageHint
	target_size As Long
	target_PSNR As Single
	segments As Long
	sns_strength As Long
	filter_strength As Long
	filter_sharpness As Long
	filter_type As Long
	autofilter As Long
	alpha_compression As Long
	alpha_filtering As Long
	alpha_quality As Long
	pass As Long
	show_compressed As Long
	preprocessing As Long
	partitions As Long
	partition_limit As Long
	emulate_jpeg_size As Long
	thread_level As Long
	low_memory As Long
	near_lossless As Long
	exact As Long
	use_delta_palette As Long
	use_sharp_yuv As Long
	pad(0 To 1) As Ulong
End Type

Type WebPPreset As Long
Enum
	WEBP_PRESET_DEFAULT = 0
	WEBP_PRESET_PICTURE
	WEBP_PRESET_PHOTO
	WEBP_PRESET_DRAWING
	WEBP_PRESET_ICON
	WEBP_PRESET_TEXT
End Enum

Declare Function WebPConfigInitInternal(Byval As WebPConfig Ptr, Byval As WebPPreset, Byval As Single, Byval As Long) As Long
#Define WebPConfigInit(config) Clng(WebPConfigInitInternal((config), WEBP_PRESET_DEFAULT, 75.f, &h020e))
#Define WebPConfigPreset(config, Preset, quality) Clng(WebPConfigInitInternal((config), (Preset), (quality), &h020e))
Declare Function WebPConfigLosslessPreset(Byval config As WebPConfig Ptr, Byval level As Long) As Long
Declare Function WebPValidateConfig(Byval config As Const WebPConfig Ptr) As Long

Type WebPAuxStats
	coded_size As Long
	PSNR(0 To 4) As Single
	block_count(0 To 2) As Long
	header_bytes(0 To 1) As Long
	residual_bytes(0 To 2, 0 To 3) As Long
	segment_size(0 To 3) As Long
	segment_quant(0 To 3) As Long
	segment_level(0 To 3) As Long
	alpha_data_size As Long
	layer_data_size As Long
	lossless_features As Ulong
	histogram_bits As Long
	transform_bits As Long
	cache_bits As Long
	palette_size As Long
	lossless_size As Long
	lossless_hdr_size As Long
	lossless_data_size As Long
	pad(0 To 1) As Ulong
End Type

Type WebPPicture As WebPPicture_
Type WebPWriterFunction As Function(Byval Data As Const Ubyte Ptr, Byval data_size As Uinteger, Byval picture As Const WebPPicture Ptr) As Long

Type WebPMemoryWriter
	mem As Ubyte Ptr
	size As Uinteger
	max_size As Uinteger
	pad(0 To 0) As Ulong
End Type

Declare Sub WebPMemoryWriterInit(Byval writer As WebPMemoryWriter Ptr)
Declare Sub WebPMemoryWriterClear(Byval writer As WebPMemoryWriter Ptr)
Declare Function WebPMemoryWrite(Byval Data As Const Ubyte Ptr, Byval data_size As Uinteger, Byval picture As Const WebPPicture Ptr) As Long
Type WebPProgressHook As Function(Byval percent As Long, Byval picture As Const WebPPicture Ptr) As Long

Type WebPEncCSP As Long
Enum
	WEBP_YUV420 = 0
	WEBP_YUV420A = 4
	WEBP_CSP_UV_MASK = 3
	WEBP_CSP_ALPHA_BIT = 4
End Enum

Type WebPEncodingError As Long
Enum
	VP8_ENC_OK = 0
	VP8_ENC_ERROR_OUT_OF_MEMORY
	VP8_ENC_ERROR_BITSTREAM_OUT_OF_MEMORY
	VP8_ENC_ERROR_NULL_PARAMETER
	VP8_ENC_ERROR_INVALID_CONFIGURATION
	VP8_ENC_ERROR_BAD_DIMENSION
	VP8_ENC_ERROR_PARTITION0_OVERFLOW
	VP8_ENC_ERROR_PARTITION_OVERFLOW
	VP8_ENC_ERROR_BAD_WRITE
	VP8_ENC_ERROR_FILE_TOO_BIG
	VP8_ENC_ERROR_USER_ABORT
	VP8_ENC_ERROR_LAST
End Enum

Const WEBP_MAX_DIMENSION = 16383

Type WebPPicture_
	use_argb As Long
	colorspace As WebPEncCSP
	Width As Long
	height As Long
	y As Ubyte Ptr
	u As Ubyte Ptr
	v As Ubyte Ptr
	y_stride As Long
	uv_stride As Long
	a As Ubyte Ptr
	a_stride As Long
	pad1(0 To 1) As Ulong
	argb As Ulong Ptr
	argb_stride As Long
	pad2(0 To 2) As Ulong
	writer As WebPWriterFunction
	custom_ptr As Any Ptr
	extra_info_type As Long
	extra_info As Ubyte Ptr
	stats As WebPAuxStats Ptr
	error_code As WebPEncodingError
	progress_hook As WebPProgressHook
	user_data As Any Ptr
	pad3(0 To 2) As Ulong
	pad4 As Ubyte Ptr
	pad5 As Ubyte Ptr
	pad6(0 To 7) As Ulong
	memory_ As Any Ptr
	memory_argb_ As Any Ptr
	pad7(0 To 1) As Any Ptr
End Type

Declare Function WebPPictureInitInternal(Byval As WebPPicture Ptr, Byval As Long) As Long
#Define WebPPictureInit(picture) Clng(WebPPictureInitInternal((picture), &h020e))
Declare Function WebPPictureAlloc(Byval picture As WebPPicture Ptr) As Long
Declare Sub WebPPictureFree(Byval picture As WebPPicture Ptr)
Declare Function WebPPictureCopy(Byval src As Const WebPPicture Ptr, Byval dst As WebPPicture Ptr) As Long
Declare Function WebPPlaneDistortion(Byval src As Const Ubyte Ptr, Byval src_stride As Uinteger, Byval ref As Const Ubyte Ptr, Byval ref_stride As Uinteger, Byval Width As Long, Byval height As Long, Byval x_step As Uinteger, Byval Type As Long, Byval distortion As Single Ptr, Byval result As Single Ptr) As Long
Declare Function WebPPictureDistortion(Byval src As Const WebPPicture Ptr, Byval ref As Const WebPPicture Ptr, Byval metric_type As Long, Byval result As Single Ptr) As Long
Declare Function WebPPictureCrop(Byval picture As WebPPicture Ptr, Byval Left As Long, Byval top As Long, Byval Width As Long, Byval height As Long) As Long
Declare Function WebPPictureView(Byval src As Const WebPPicture Ptr, Byval Left As Long, Byval top As Long, Byval Width As Long, Byval height As Long, Byval dst As WebPPicture Ptr) As Long
Declare Function WebPPictureIsView(Byval picture As Const WebPPicture Ptr) As Long
Declare Function WebPPictureRescale(Byval pic As WebPPicture Ptr, Byval Width As Long, Byval height As Long) As Long
Declare Function WebPPictureImportRGB(Byval picture As WebPPicture Ptr, Byval Rgb As Const Ubyte Ptr, Byval rgb_stride As Long) As Long
Declare Function WebPPictureImportRGBA(Byval picture As WebPPicture Ptr, Byval Rgba As Const Ubyte Ptr, Byval rgba_stride As Long) As Long
Declare Function WebPPictureImportRGBX(Byval picture As WebPPicture Ptr, Byval rgbx As Const Ubyte Ptr, Byval rgbx_stride As Long) As Long
Declare Function WebPPictureImportBGR(Byval picture As WebPPicture Ptr, Byval bgr As Const Ubyte Ptr, Byval bgr_stride As Long) As Long
Declare Function WebPPictureImportBGRA(Byval picture As WebPPicture Ptr, Byval bgra As Const Ubyte Ptr, Byval bgra_stride As Long) As Long
Declare Function WebPPictureImportBGRX(Byval picture As WebPPicture Ptr, Byval bgrx As Const Ubyte Ptr, Byval bgrx_stride As Long) As Long
Declare Function WebPPictureARGBToYUVA(Byval picture As WebPPicture Ptr, Byval As WebPEncCSP) As Long
Declare Function WebPPictureARGBToYUVADithered(Byval picture As WebPPicture Ptr, Byval colorspace As WebPEncCSP, Byval dithering As Single) As Long
Declare Function WebPPictureSharpARGBToYUVA(Byval picture As WebPPicture Ptr) As Long
Declare Function WebPPictureSmartARGBToYUVA(Byval picture As WebPPicture Ptr) As Long
Declare Function WebPPictureYUVAToARGB(Byval picture As WebPPicture Ptr) As Long
Declare Sub WebPCleanupTransparentArea(Byval picture As WebPPicture Ptr)
Declare Function WebPPictureHasTransparency(Byval picture As Const WebPPicture Ptr) As Long
Declare Sub WebPBlendAlpha(Byval pic As WebPPicture Ptr, Byval background_rgb As Ulong)
Declare Function WebPEncode(Byval config As Const WebPConfig Ptr, Byval picture As WebPPicture Ptr) As Long
End Extern

Dim Shared gdipToken As ULONG_PTR
Dim Shared GDIp As GdiplusStartupInput 

Private Function _GDIPlus_Startup() As Bool
	GDIp.GdiplusVersion = 1
	If GdiplusStartup(@gdipToken, @GDIp, NULL) <> 0 Then
		Error 1
		Return False
	Endif
	Return True
End Function

Private Sub _GDIPlus_Shutdown()
	GdiplusShutdown(gdipToken)
End Sub

Function WebP_CreateWebPAdvancedFromBitmap(Byval filename As Zstring Ptr, Byval hBitmap As Any Ptr, Byval WebPPreset As Long = WEBP_PRESET_DEFAULT, _
										   Byval lossless As Long = 0, Byval quality As Single = 75, Byval method As Long = 4, Byval sns_strength As Long = 0, _
										   Byval filter_sharpness As Long = 0, Byval filter_strength As Long = 0, Byval pass As Long = 10) As Long Export
	If _GDIPlus_Startup() = False Then Return -1
	Dim As Single w, h
	Dim As Integer iStatus = GdipGetImageDimension(hBitmap, @w, @h)
	If iStatus <> 0 Orelse w < 1 Orelse h < 1 Orelse w > WEBP_MAX_DIMENSION Orelse h > WEBP_MAX_DIMENSION Then 
		_GDIPlus_Shutdown()
		Return -2
	Endif
	
	Dim As WebPConfig config
	With config
		.lossless = Iif(lossless < 0, 0, Iif(lossless > 1, 1, lossless))
		.quality = Iif(quality < 0, 0, Iif(quality > 100, 100, quality))
		.method = Iif(method < 0, 0, Iif(method > 6, 6, method))
		.sns_strength = Iif(sns_strength < 0, 0, Iif(sns_strength > 100, 100, sns_strength)) 
		.filter_sharpness = Iif(filter_sharpness < 0, 0, Iif(filter_sharpness > 7, 7, filter_sharpness))
		.filter_strength = Iif(filter_strength < 0, 0, Iif(filter_strength > 100, 100, filter_strength))
		.pass = Iif(pass < 1, 1, Iif(pass > 10, 10, pass))
		.segments = 1
		.filter_type = 0
		.autofilter = 0
		.alpha_compression = 1
		.alpha_filtering = 2
		.alpha_quality = 100
		.thread_level = 1
		.use_sharp_yuv = 1
		.near_lossless = quality
		.exact = 0
	End With
	
	If WebPValidateConfig(@config) = 0 Then Return -3
	If WebPConfigInitInternal(@config, WebPPreset, quality, WEBP_ENCODER_ABI_VERSION) = 0 Then Return -4
	Dim As WebPPicture pic
	If WebPPictureInitInternal(@pic, WEBP_ENCODER_ABI_VERSION) = 0 Then	Return -5
	With pic
		.width = w
		.height = h
		.use_argb = 0
	End With
	Dim As Rect tRect = Type(0, 0, w - 1, h - 1)
	Dim As BitmapData tBitmapData
	GdipBitmapLockBits(hBitmap, Cast(Any Ptr, @tRect), ImageLockModeRead, PixelFormat32bppARGB, @tBitmapData)
	WebPPictureImportBGRA(@pic, tBitmapData.Scan0, tBitmapData.stride)
	'If WebPPictureAlloc(@pic) = 0 Then Return -6
	Dim As WebPMemoryWriter writer
	WebPMemoryWriterInit(@writer)
	With pic
		.writer = Cast(WebPWriterFunction, @WebPMemoryWrite)
		.custom_ptr = @writer
	End With
	Dim As Long ret = WebPEncode(@config, @pic)
	GdipBitmapUnlockBits(hBitmap, @tBitmapData)
	_GDIPlus_Shutdown()
	If ret = 0 Then
		WebPPictureFree(@pic)
		WebPMemoryWriterClear(@writer)
		WebPFree(writer.mem)
		Return -7
	End If
	If writer.size > 399 Then
	Dim As Integer hFile
		hFile = Freefile()
		Open *filename For Binary Access Write As #hFile
		Put #hFile, 0, writer.mem[0], writer.size
		Close #hFile
	Endif
	? "Output size: " & writer.size
	'MessageBoxEx(Null, "Size = " & Str(writer.max_size) & ", pMem = " & Str(writer.mem), "Debug", MB_ICONINFORMATION Or MB_OK Or MB_APPLMODAL Or MB_TOPMOST, 1033)
	WebPPictureFree(@pic)
	WebPMemoryWriterClear(@writer)
	WebPFree(writer.mem)
	Return 1
End Function

_GDIPlus_Startup()
Dim As Any Ptr hBitmap 
GdipLoadImageFromFile(Wstr("Auge.jpg"), @hBitmap)
? WebP_CreateWebPAdvancedFromBitmap("C:\Temp\Test_Auge.webp", hBitmap)
GdipDisposeImage(hBitmap)
_GDIPlus_Shutdown
Sleep
You must put the files libwebp_x64.a and libwebp_x86.a into the same folder to compile it properly. Further, don't forget to modify the filename at the bottom of the code to load.
Last edited by UEZ on May 04, 2022 20:00, edited 2 times in total.
dodicat
Posts: 7983
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: C/C++ Struct to FB Type conversation

Post by dodicat »

UEZ
#Ifdef __Fb_64bit__
#Inclib "webp_x64"
#Inclib "gdiplus"
#Include Once "win/gdiplus-c.bi"
#Else
#Inclib "webp_x86"
#Include Once "win/gdiplus.bi"
Using gdiplus
#Endif

I cannot find these required .a files in your download link.
libwebp_x64.a
libwebp_x86.a

only these:
libwebp.a
libwebp.la
libwebpdecoder.a
libwebpdecoder.la
libwebpdemux.a
libwebpdemux.la
libwebpmux.a
libwebpmux.la
UEZ
Posts: 988
Joined: May 05, 2017 19:59
Location: Germany

Re: C/C++ Struct to FB Type conversation

Post by UEZ »

Sorry, I forgot to write that you must rename libwebp.a accordingly to libwebp_x64.a / libwebp_x86.a.
You can also set the pathes manually if you let them in the folders.
dodicat
Posts: 7983
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: C/C++ Struct to FB Type conversation

Post by dodicat »

UEZ
I get this on the console
Output size: 990
1
But the image is blank when I open the .webp with Google or Edge.
(Tested 64 bit library)
UEZ
Posts: 988
Joined: May 05, 2017 19:59
Location: Germany

Re: C/C++ Struct to FB Type conversation

Post by UEZ »

I have the same problem when calling the DLL version with Autoit, but it works correctly when running from code above.
Also when I use the DLL code (replacing the code from above), it works - the webp is created correctly.

I need to investigate it further to see where the problem lies ...

Edit: it seems to work properly with "Auge.jpg" image but not with the rest :shock:
dodicat
Posts: 7983
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: C/C++ Struct to FB Type conversation

Post by dodicat »

line approx 295
If I do this
.use_argb = 0
it works.
tested 64 bits.
UEZ
Posts: 988
Joined: May 05, 2017 19:59
Location: Germany

Re: C/C++ Struct to FB Type conversation

Post by UEZ »

dodicat wrote: May 03, 2022 18:19 line approx 295
If I do this
.use_argb = 0
it works.
tested 64 bits.
Good catch dodicat! I just wonder why Auge.jpg works...

Anyhow, thank you very much!
Post Reply