gpt4 book ai didi

c# - 这行 C 代码在 C# 中相当于什么?

转载 作者:行者123 更新时间:2023-11-30 21:37:09 26 4
gpt4 key购买 nike

我需要在 C# 上重写一些 C 函数,我已经取得了一些进展,但遇到了一些我不明白的事情。通常我会问“这是什么?”,但在这种情况下我必须问“这到底是什么?!”

我在我不明白的行上方添加了注释。

static enumError conv_from_CMPR
(
Image_t * dest_img, // valid destination
const Image_t * src_img, // valid source
image_format_t iform // new image format, only valid IMG_X_*
)
{
DASSERT(dest_img);
DASSERT(src_img);
DASSERT( src_img->iform == IMG_CMPR );
DASSERT( iform >= IMG_X__MIN && iform <= IMG_X__MAX );

const uint bits_per_pixel = 4;
const uint block_width = 8;
const uint block_height = 8;

uint h_blocks, v_blocks, img_size;
enumError err = CalcImageBlock(src_img,
bits_per_pixel, block_width, block_height,
&h_blocks, &v_blocks, &img_size, false );
if (err)
return err;

//This is the line that I don't know how to convert to C#.
//Ok, u16 is looks like a numeric type, but what happens next I don't have any idea. I don't see where is variable name.
u16 (*rd16) ( const void * data_ptr ) = src_img->endian->rd16;

uint data_size;
u8 * data = AllocDataIMG(src_img,4,&data_size);
u8 * dest1 = data;
const u8 *src = src_img->data;

const uint block_size = block_width * 4;
const uint line_size = EXPAND8(src_img->width) * 4;
const uint delta[] = { 0, 16, 4*line_size, 4*line_size+16 };

while ( v_blocks-- > 0 )
{
u8 * dest2 = dest1;
uint hblk = h_blocks;
while ( hblk-- > 0 )
{
uint subb;
for ( subb = 0; subb < 4; subb++ )
{
u8 palette[4][4], *pal = palette[0];

const u16 val1 = rd16(src);
src += 2;
*pal++ = cc58[ val1 >> 11 & 0x1f ];
*pal++ = cc68[ val1 >> 5 & 0x3f ];
*pal++ = cc58[ val1 & 0x1f ];
*pal++ = 0xff;

const u16 val2 = rd16(src);
src += 2;
*pal++ = cc58[ val2 >> 11 & 0x1f ];
*pal++ = cc68[ val2 >> 5 & 0x3f ];
*pal++ = cc58[ val2 & 0x1f ];
*pal++ = 0xff;

if ( val1 > val2 )
{
*pal++ = ( 2 * palette[0][0] + palette[1][0] ) / 3;
*pal++ = ( 2 * palette[0][1] + palette[1][1] ) / 3;
*pal++ = ( 2 * palette[0][2] + palette[1][2] ) / 3;
*pal++ = 0xff;

*pal++ = ( 2 * palette[1][0] + palette[0][0] ) / 3;
*pal++ = ( 2 * palette[1][1] + palette[0][1] ) / 3;
*pal++ = ( 2 * palette[1][2] + palette[0][2] ) / 3;
*pal++ = 0xff;
}
else
{
*pal++ = ( palette[0][0] + palette[1][0] ) / 2;
*pal++ = ( palette[0][1] + palette[1][1] ) / 2;
*pal++ = ( palette[0][2] + palette[1][2] ) / 2;
*pal++ = 0xff;

*pal++ = 0;
*pal++ = 0;
*pal++ = 0;
*pal++ = 0;
}

u8 *dest3 = dest2 + delta[subb];
uint i;
for ( i = 0; i < 4; i++ )
{
u8 val = *src++;
memcpy(dest3+12, palette[ val & 3 ], 4); val >>= 2;
memcpy(dest3+ 8, palette[ val & 3 ], 4); val >>= 2;
memcpy(dest3+ 4, palette[ val & 3 ], 4); val >>= 2;
memcpy(dest3 , palette[ val & 3 ], 4);
dest3 += line_size;
}
}
dest2 += block_size;
}
dest1 += line_size * block_height;
}

DASSERT( src == src_img->data + img_size );
DASSERT( dest1 <= data + data_size );
AssignDataRGB(dest_img,src_img,data);
dest_img->alpha_status = 0;
return ERR_OK;
}

最佳答案

您的代码行将成为 C# 中的委托(delegate) See here

如果您有大量 C 代码(作为 DLL)并且需要从 C# 使用它们,那么您可能需要考虑保留 C 并通过 tlbimp 使用 COM Interop。 See here

关于c# - 这行 C 代码在 C# 中相当于什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36938190/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com