• Agg:PPM格式图像生成


    PPM是一个Linux下的简单图像格式,可以用Xnview打开。Agg的教程第一个,就是生成PPM格式的图像。PPM格式定义参见:http://en.wikipedia.org/wiki/Netpbm_format。以下是一个简单的画点程序:

    buffer.cpp

    #include <stdio.h>
    #include <string.h>
    #include "agg_rendering_buffer.h"
    
    const int HEIGHT = 480;
    const int WIDTH = 640;
    
    /* PPM(Portable PixMap)
     * Portable BitMap    [1]    P1    ASCII    P4    binary    .pbm    0–1 (black & white)
     * Portable GrayMap    [2]    P2    ASCII    P5    binary    .pgm    0–255 (gray scale)
     * Portable PixMap    [3]    P3    ASCII    P6    binary    .ppm    0–255 (RGB)
     *
     * Row 0:R G B R G B ...
     * Row 1:R G B R G B ...
     * ...
     */ 
    bool writePpm(const unsigned char* buffer,
            unsigned width,
            unsigned height,
            const char* fileName)
    {
        FILE* fd = fopen(fileName, "wb");
        if(fd)
        {
            fprintf(fd, "P6 %d %d 255 ", width, height);
            fwrite(buffer, 1, width * height * 3, fd);
            fclose(fd);
            return true;
        }
        return false;
    }
    
    bool setPixel(agg::rendering_buffer& rbuf,
            int x,
            int y,
            unsigned char r,
            unsigned char g, 
            unsigned char b)
    {
        unsigned char* ptr = rbuf.row_ptr(y) + x * 3;
        *ptr++ = r;
        *ptr++ = g;
        *ptr++ = b;
        return true;
    }
    
    int main(int argc, char* argv[])
    {
        unsigned char* buffer = new unsigned char[WIDTH * HEIGHT * 3];
    
        memset(buffer, 255, WIDTH * HEIGHT * 3);
    
        agg::rendering_buffer rbuf(buffer, 
                WIDTH, 
                HEIGHT, 
                WIDTH * 3);
    
        for(int i = 0; i < WIDTH; i ++)
        {
            setPixel(rbuf, i, 10, 255, 0, 0);
        }    
        for(int i = 0; i < HEIGHT; i ++)
        {
            setPixel(rbuf, 120, i, 0, 255, 0);
        }
        writePpm(buffer, WIDTH, HEIGHT, "agg_test.ppm");
    
        delete [] buffer;
        return 0;
    }

    Makefile:

    OBJS = buffer.cpp 
    #agg_win32_bmp.cpp agg_platform_support.cpp
    
    CC = g++
    COMPILER_FLAGS = -w -m32
    LINKER_FLAGS = -lagg -lgdi32
    
    HEADERS = -I./agg/include
    LIBS = -L./agg/lib 
    
    OBJ_NAME = agg.exe
    
    all    :$(OBJS)
        $(CC) $(OBJS) $(COMPILER_FLAGS) $(HEADERS) $(LIBS) $(LINKER_FLAGS) -o $(OBJ_NAME)
    
    clean:
        rm -f *.o *.exe

    画出来很简单:

  • 相关阅读:
    MongoDB权限验证
    【大型网站开发系列第一篇】——网站结构层次
    php5.4的htmlspecialchars处理中文
    将session值字符串重新生成session
    php支持短标签
    solr suggest+autocomplete实现自动提示
    linux命令
    【技术】【转】字节序问题大端法小端法
    eeePC(易PC)变态测试!(上)
    "挑iPod不问价"就是不懂“性价比”?
  • 原文地址:https://www.cnblogs.com/jpeg/p/draw-ppm-image.html
Copyright © 2020-2023  润新知