常⽤图像格式(PNG,JPG)到SGI图像格式(RGB,BW)的转换常⽤图像格式(PNG,JPG)到SGI图像格式(RGB,BW)的转换
根据该⽹站的描述和给的相应例⼦,⾃⼰改写了⼀个把png,jpg等常⽤格式的图⽚转化为rgb,bw格式的代码。
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
\\待转换的图像长宽
#define IXSIZE      (512)
#define IYSIZE      (512)
#include<opencv.hpp>
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", ws);
printf("%d\n", ls);
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<Vec3b>(255, 0)[2], img.at<Vec3b>(255, 0)[1], img.at<Vec3b>(255, 0)[0]);
printf("%u %u %u\n", img.at<Vec3b>(0, 255)[2], img.at<Vec3b>(0, 255)[1], img.at<Vec3b>(0, 255)[0]);
imshow("img", img);
waitKey(0);
FILE *of;
char iname[80];
unsigned char outbuf[IXSIZE];
unsigned char outbuf[IXSIZE];
int i, x, y;
of = fopen("b", "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              */
fprintf格式
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<IYSIZE; y++) {
for (x = 0; x < IXSIZE; x++)
{
Vec3b vec3 = img.at<Vec3b>(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];
outbuf[x] = vec3[0];
}
}
fwrite(outbuf, IXSIZE, 1, of);
}
}
printf("sunccess\n");
fclose(of);
system("pause");
}
写⼊像素10会有意想不到的错误,所以把像素10都改为了9

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。