设为首页 加入收藏

TOP

使用libpng读取PNG图片像素数据(三)
2014-11-24 02:56:07 来源: 作者: 【 】 浏览:6
Tags:使用 libpng 读取 PNG 图片 像素 数据
]; // red
//rgba[pos++] = row_pointers[row][col + 1]; // green
//rgba[pos++] = row_pointers[row][col + 2]; // blue
//rgba[pos++] = row_pointers[row][col + 3]; // alpha
//}
//}



//unlike store the pixel data from top-left corner, store them from bottom-left corner for OGLES Texture drawing...
int pos = (width * height * 4) - (4 * width);
for(int row = 0; row < height; row++)
{
for(int col = 0; col < (4 * width); col += 4)
{
rgba[pos++] = row_pointers[row][col]; // red
rgba[pos++] = row_pointers[row][col + 1]; // green
rgba[pos++] = row_pointers[row][col + 2]; // blue
rgba[pos++] = row_pointers[row][col + 3]; // alpha
}
pos=(pos - (width * 4)*2); //move the pointer back two rows
}


ImageInfo* imageInfo = (ImageInfo*)kdMalloc(sizeof(ImageInfo));
imageInfo->pixelData = rgba;
imageInfo->imageHeight = height;
imageInfo->imageWidth = width;


//clean up after the read, and free any memory allocated
png_destroy_read_struct(&png_ptr, &info_ptr, NULL);
fclose(file);
return imageInfo;
}


//----------------------------------------------------------------------------------------------------------------



int createPNGTextureFromStream(const u8* pixelData, const u32& dataSize)
{
GLuint textureID;
glEnable(GL_TEXTURE_2D);
glGenTextures(1,&textureID);
glBindTexture(GL_TEXTURE_2D,textureID);
ImageInfo* imageInfo = decodePNGFromStream(pixelData, dataSize);


glTexImage2D(GL_TEXTURE_2D,0,GL_RGBA,imageInfo->imageWidth,imageInfo->imageHeight,0,


GL_RGBA,GL_UNSIGNED_BYTE,imageInfo->pixelData);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_NEAREST);


delete[] imageInfo->pixelData;


delete imageInfo;
return textureID;


}



//----------------------------------------------------------------------------------------------------------------



void main()


{


//Testcase1: decoding png data from a raw png bufferstream and create the corresponding Texture


//假设我们从某个地方可以拿到一个unsigned char* 的PNG数据源pixelData.


int texId = createPNGTextureFromStream(pixelData, dataSize);



//Testcase2: decoding png data from a given png file and and create the corresponding Texture


char* fileName = "example.png";


int texId = createPNGTextureFromFile(fileName);



//现在我们就可以用创建出来的textureID来绘制纹理了。。。。


}


推荐阅读:


首页 上一页 1 2 3 下一页 尾页 3/3/3
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇Java工厂模型小结 下一篇利用libpng中的函数读写PNG文件

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容:

·Sphinx : 高性能SQL (2025-12-24 10:18:11)
·Pandas 性能优化 - (2025-12-24 10:18:08)
·MySQL 索引 - 菜鸟教 (2025-12-24 10:18:06)
·Shell 基本运算符 - (2025-12-24 09:52:56)
·Shell 函数 | 菜鸟教 (2025-12-24 09:52:54)