UIImageをTGA形式で保存する

32bitカラー専用でc++とstlで記述しています。
デバッグで使用することを前提にしているのでエラーチェックはしていません。
保存先がわかりにくいため、デバッグ時はコンソールに保存先を表示します。

Classes/chlibs/ui/ImageTga.h
//*****************************************************************************
//!     TGA 処理
//*****************************************************************************
#ifndef CH_IMAGE_TGA_H
#define CH_IMAGE_TGA_H
 
    //-------------------------------------------------------------------------
    //!     TGA 構造体
    //-------------------------------------------------------------------------
    #pragma pack(push,1)
    #pragma pack(1)
    struct TGA_HEADER {
        u08         IDLength ;                  //!< ID フィールドの長さ
        u08         ColorMapType ;              //!< カラーマップの形式
        u08         ImageType ;                 //!< イメージ形式
        u16         CMapStart ;                 //!< 最初のカラーマップエントリ
        u16         CMapLength ;                //!< カラーマップの長さ
        u08         CMapDepth ;                 //!< カラーマップエントリのサイズ
        u16         XOffset,YOffset ;           //!< イメージの原点
        u16         Width,Height ;              //!< イメージの幅と高さ
        u08         PixelDepth ;                //!< 1 ピクセルあたりのビット数
        u08         ImageDescriptor ;           //!< イメージ記述子ビット
    } ;
    #pragma pack(pop)
    //-------------------------------------------------------------------------
    //!     UIImage を TGA で保存
    //-------------------------------------------------------------------------
    void ImageSaveTag(const string filename,const UIImage *image) ;
 
#endif /* CH_IMAGE_TGA_H */
Classes/chlibs/ui/ImageTga.mm
//*****************************************************************************
//      TGA 処理
//*****************************************************************************
#import "../chlibs.h"
//*****************************************************************************
//      UIImage を TGA で保存
//*****************************************************************************
void ImageSaveTag(const string filename,const UIImage *image) {
    string fullpath = [NSHomeDirectory() UTF8String] ;
    fullpath += filename ;
 
    #ifdef DEBUG
        NSLog(@"save = %@",[NSString stringWithUTF8String:fullpath.c_str()]) ;
    #endif
 
    CGImageRef ImgRef   = image.CGImage ;
    size_t linebyte     = CGImageGetBytesPerRow(ImgRef) ;
    CFDataRef DataRef   = CGDataProviderCopyData(CGImageGetDataProvider(ImgRef)) ;
    const char *data    = (const char *)CFDataGetBytePtr(DataRef) ;
 
    TGA_HEADER header ;
    memset(&header,0,sizeof(header)) ;
    header.ImageType        = 2 ;               // フルカラー画像
    header.Width            = CGImageGetWidth(ImgRef) ;
    header.Height           = CGImageGetHeight(ImgRef) ;
    header.PixelDepth       = 32 ;              // 色深度
    header.ImageDescriptor  = 0x20 ;            // 上から下
 
    ofstream ofs(fullpath.c_str(),ios::out | ios::trunc | ios::binary) ;
    ofs.write((char *)&header,sizeof(header)) ;
    ofs.write(data,linebyte * header.Height) ;
    ofs.close() ;
 
    CFRelease(DataRef) ;
}
iphone/uiimageをtga形式で保存する.txt · 最終更新: 2018/03/18 09:56 (外部編集)
 
特に明示されていない限り、本Wikiの内容は次のライセンスに従います: CC Attribution-Share Alike 4.0 International
Recent changes RSS feed Donate Powered by PHP Valid XHTML 1.0 Valid CSS Driven by DokuWiki