2

imgファイルを読み取るプログラムを取得したため、構造体へのいくつかのポイントを保持する構造体を作成しようとしています。このファイルが16、24、または32ビットであるかどうかに応じて、そのように扱いたいです。この問題は、pixelpointer が他の構造体の 1 つを指すようにしようとすると発生します (「型 'Pixel24' から型 'Pixel' に割り当てるときに互換性のない型」)。これを機能させる方法が本当にわからないので、本当に助けが必要です。

また、ファイルを開くときに特定のファイル拡張子を確認したいのですが、それが正しいファイル拡張子である場合にのみ処理したいのですが、その方法についての手がかりがありません。

typedef struct pixel16 {
    uint8_t R, G, B;
}__attribute__((packed)) Pixel16;

typedef struct pixel24 {
    uint8_t R, G, B;
}__attribute__((packed)) Pixel24;

typedef struct pixel32 {
    uint8_t R, G, B, A;
}__attribute__((packed)) Pixel32;


typedef struct pixel{
    Pixel16 pixel16;
    Pixel24 pixel24;
    Pixel32 pixel32;
}__attribute__((packed)) Pixel;

typedef struct file{
    Header *imageHeader;
    Pixel *imageData;
}File

void something(File file){
   Pixel *pixelptr;

    if(file->imageHeader.pixelDepth == 16)
        *pixelptr = file->imageData->pixel16;
    else if(file->imageHeader.pixelDepth == 24)
        *pixelptr = file->imageData->pixel24;
    else
        *pixelptr = file->imageData->pixel32;
}
4

2 に答える 2

1

ユニオンを使用できます:

typedef struct {
    int depth;
    union {
        Pixel16 p16;
        Pixel24 p24;
        Pixel32 p32;
    } data;
} Pixel;

if (pixel.depth == 16)
    pixel.data.p16 = ...;
else if (pixel.depth == 24)
    pixel.data.p24 = ...;
else if (pixel.depth == 32)
    pixel.data.p32 = ...;

共用体を使用すると、最大のメンバーのストレージのみを占有しながら、そのフィールドのいずれかにアクセスできます。

strrpbrk()拡張部分については、文字列を最後から最初のドットまたはディレクトリ区切りまでスキャンするために使用できます。

ext = strrpbrk(path, "./");

if (!ext || *ext != '.')
    ; /* No extension found. */

if (!strcmp(ext, ".png")
    ...
else if (!strcmp(ext, ".bmp")
    ...
else
    ... /* Unknown extension. */
于 2013-11-14T18:57:24.657 に答える
0

ユニオンと構造体を組み合わせて使用​​します。

typedef struct pixel16 {
    uint8_t R, G, B;
}__attribute__((packed)) Pixel16;

typedef struct pixel24 {
     uint8_t R, G, B;
}__attribute__((packed)) Pixel24;

typedef struct pixel32 {
     uint8_t R, G, B, A;
}__attribute__((packed)) Pixel32;


typedef union pixel {
    Pixel16 pixel16;
    Pixel24 pixel24;
    Pixel32 pixel32;
} Pixel;

typedef struct file{
    Pixel *imageData;
}File

次に、次のようなもので使用します。

void something(File file){
Pixel *pixelptr;

    if(file->imageHeader.pixelDepth == 16)
        *pixelptr = file->imageData.pixel16;
    else if(file->imageHeader.pixelDepth == 24)
        *pixelptr = file->imageData.pixel24;
    else
        *pixelptr = file->imageData.pixel32;
}
于 2013-11-14T19:10:17.297 に答える