在 C 中裁剪 .ppm 文件
我正在开发一个 C 程序,该程序将 .ppm 文件从起点像素 (x,y)(裁剪图像的左上角)裁剪到终点像素 (x+w,x+h)(裁剪图像的左下角)裁剪后的图像)。
.ppm 文件中的数据格式如下:
rgbrgbrgbrgbrgbrg b
rgbrgbrgbrgbrgbrg b
rgbrgbrgbrgbrgbrg b
rgbrgbrgbrgbrgbrgb
有没有一种简单的方法,避免使用二维数组,使用 scanf() 来做到这一点?
I'm working on a C program that crops .ppm files from a starting point pixel (x,y) (top left corner of cropped image) to an end point pixel (x+w,x+h)(bottom left corner of cropped image).
The data in .ppm files is of the following format:
r g b r g b r g b r g b r g b r g b
r g b r g b r g b r g b r g b r g b
r g b r g b r g b r g b r g b r g b
r g b r g b r g b r g b r g b r g b
Is there a simple way, wich avoids the use of 2 dimensional arrays, to do this using scanf()?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
一种简单的方法是在读入文件时简单地跟踪像素坐标。如果当前位于裁剪矩形中,请存储该像素;如果您当前位于裁剪矩形中,则存储该像素;否则,跳过它。
如果你想变得更奇特:找出每行开头的字节偏移量,查找它,然后读取整行。One easy way would be to simply keep track of your pixel coordinate as you read the file in. If you're currently in the crop rectangle, store the pixel; otherwise, skip it.
If you want to get more fancy: figure out the byte offset for the start of each row, seek to it, then read in the whole row.警告,某些 pnm 文件处于二进制模式(它们在文件内容开头的幻数不同)。
也许查找 pnmcrop 的来源会有所帮助?
Warning, some pnm files are in binary mode (they differ by magic number in the beginning of the file contents).
Maybe lookup the sources of pnmcrop would help?