常用图像格式(PNG,JPG)到SGI图像格式(RGB,BW)的转换
网站链接
根据该网站的描述和给的相应例子,自己改写了一个把png,jpg等常用格式的图片转化为rgb,bw格式的代码。
#include
#include
#include
\\待转换的图像长宽
#define IXSIZE (512)
#define IYSIZE (512)
#include
using namespace cv;
void putbyte(FILE *outf, unsigned char val)
{
unsigned char buf[1];
buf[0] = val;
fwrite(buf, 1, 1, outf);
}
void putshort(FILE *outf, unsigned short val)
{
unsigned char buf[2];
buf[0] = (val >> 8);
buf[1] = (val >> 0);
fwrite(buf, 2, 1, outf);
}
static int putlong(FILE *outf, unsigned long val)
{
unsigned char buf[4];
buf[0] = (val >> 24);
buf[1] = (val >> 16);
buf[2] = (val >> 8);
buf[3] = (val >> 0);
return fwrite(buf, 4, 1, outf);
}
int main()
{
printf("%d\n", sizeof(unsigned));
Mat img, img1;
Mat dst;
img = imread("./scene_dense_mesh_refine_texture1.jpg");
img1 = imread("./test.jpg");
printf("%d\n", img.rows);
printf("%d\n", img.cols);
resize(img, dst, Size(256, 256), (0, 0), (0, 0), INTER_LINEAR);
imwrite("./test2.png", dst);
//imwrite("./test.jpg", dst);
printf("%u %u %u\n", img.at(255, 0)[2], img.at(255, 0)[1], img.at(255, 0)[0]);
printf("%u %u %u\n", img.at(0, 255)[2], img.at(0, 255)[1], img.at(0, 255)[0]);
imshow("img", img);
waitKey(0);
FILE *of;
char iname[80];
unsigned char outbuf[IXSIZE];
int i, x, y;
of = fopen("example.rgb", "w");
if (!of) {
fprintf(stderr, "sgiimage: can't open output file\n");
return -1;
}
putshort(of, 474); /* MAGIC */
putbyte(of, 0); /* STORAGE is VERBATIM */
putbyte(of, 1); /* BPC is 1 */
putshort(of, 3); /* DIMENSION is 2 */
putshort(of, IXSIZE); /* XSIZE */
putshort(of, IYSIZE); /* YSIZE */
putshort(of, 3); /* ZSIZE */
putlong(of, 0); /* PIXMIN is 0 */
putlong(of, 255); /* PIXMAX is 255 */
for (i = 0; i<4; i++) /* DUMMY 4 bytes */
putbyte(of, 1);
//strcpy(iname, "11111111111111111111111111111111111111111111111111111111111111111111111111111111");
fwrite(iname, 80, 1, of); /* IMAGENAME */
putlong(of, 0); /* COLORMAP is 0 */
for (i = 0; i<404; i++) /* DUMMY 404 bytes */
putbyte(of, 0);
for (int z = 0; z<3; z++)
{
for (y = 0; y(IXSIZE-1-y, x);
if (z == 0)
{
if (vec3[2] == 10)
vec3[2] = vec3[2] - 1;
outbuf[x] = vec3[2];
if(sizeof(vec3[2])!=1)
printf("%d\n", sizeof(vec3[2]));
if (vec3[2] > 255 || vec3[2] < 0)
printf("hrer\n");
}
if (z == 1)
{
//if (x > 128 && y < 128)
// outbuf[x] = 255;
//else
if (vec3[1] == 10)
vec3[1] = vec3[1] - 1;
outbuf[x] = vec3[1];
}
if (z == 2)
{
//if (x < 128 && y>128)
//{
// outbuf[x] = 255;
// }
//else
if (vec3[0] == 10)
vec3[0] = vec3[0] - 1;
outbuf[x] = vec3[0];
}
}
fwrite(outbuf, IXSIZE, 1, of);
}
}
printf("sunccess\n");
fclose(of);
system("pause");
}
写入像素10会有意想不到的错误,所以把像素10都改为了9