使用libpng来“拆分"将图像分成段 [英] Using libpng to "split" an image into segments

查看:78
本文介绍了使用libpng来“拆分"将图像分成段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用libpng将图像拆分为不同的块.原因是我无法在当前使用的硬件上加载大于512x512的纹理.我之前通过混合使用SDL和SDL_Image来完成此任务.我基本上使用了SDL_BlitSurface中的srcrect参数来复制图像的一部分,然后将其转换为OpenGL纹理.将其与水平然后垂直的简单循环结合起来,我可以得到每个最大为512x512的纹理数组.然后,只需将它们渲染到正确的位置即可.

I'm trying to use libpng to split an image into different chunks. The reason being I can't load a texture larger than 512x512 on the hardware I'm working on currently. I accomplished this before with a mixture of SDL and SDL_Image. I basically used the srcrect argument in SDL_BlitSurface to copy just a portion of the image which I then converted into a OpenGL texture. Combine that with a simple loop horizontally then vertically I was able to get an array of textures each a max of 512x512. Then it was just a matter of rendering them at the correct position.

现在,我没有使用SDL的奢侈之处,因此我认为可以通过libpng直接自己完成此操作.基于一些谷歌搜索,我认为使用png_read_rows来读取我需要的部分只是一个问题.但这就是我遇到的问题,我不确定该怎么做.

Right now, I don't have the luxury of using SDL, so I figured it's possible to just does this directly myself via libpng. Based on some googling I think its just a matter of using png_read_rows to read just which parts I need. But that's where I'm stuck, I'm not exactly sure how to do that.

此外,如果您想知道为什么我不只是在gimp/photoshop/paint中分割图像或其他原因,那是因为我不控制它们,而是在运行时下载它们.

Also, if you wonder why I don't just split the images in gimp/photoshop/paint or whatever, it's because I don't control them and am downloading them at runtime.

非常感谢您的帮助.

推荐答案

您不必为提取图块而烦恼.您可以告诉OpenGL仅使用您提供的部分数据来初始化纹理.关键字是glPixelStorei(GL_UNPACK ...)参数.假设您的输入图像的尺寸为img.widthimg.height,并且RGB像素有4个字节,即每个像素一个填充字节,并且子图片由subimg.off_xsubimg.off_ysubimg.width.然后您可以像这样加载它:

You don't have to mess with extracting the tiles. You can tell OpenGL to just use some portion of the data you give it to initialize the texture. Keyword is glPixelStorei(GL_UNPACK...) parameters. Say your input image has dimensions img.width and img.height and there are 4 bytes to a RGB pixel, i.e. one byte padding for each pixel and your subpicture is defined by subimg.off_x, subimg.off_y, subimg.width, subimg.height. Then you can load it like this:

glPixelStorei(GL_UNPACK_ROW_LENGTH, img.width)
glPixelStorei(GL_UNPACK_SKIP_PIXELS, subimg.off_x)
glPixelStorei(GL_UNPACK_SKIP_ROWS, subimg.off_y)
glPixelStorei(GL_UNPACK_ALIGNMENT, 1)
glTexImage2D(GL_TEXTURE_2D, GL_RGB, 0,
             subimg.width, subimg.height, 0,
             GL_RGBA, GL_UNSIGNED_BYTE, pixeldata)

这篇关于使用libpng来“拆分"将图像分成段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆