我需要在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;
}