/**************************************************************************** ** ** SaveAsPCX ** ** Write a PCX file version of the image */ int SaveAsPCX(FILE *f, OctreeType *octree, int resx, int resy, double nrgbr, double nrgbg, double nrgbb, int npal, RGBType *colortable) { PCXhdr hdr; FILE *pf; unsigned char *buf; PCXCOLOR *pal; int i,j,k; char pcxfile[128]; double r, g, b; RGBType color; printf("\nPCX file name : "); scanf("%s",pcxfile); if ((pf = fopen(pcxfile, "wb")) == NULL) { fprintf(stderr, "Can't open file %s for writing\n", pcxfile); return 1; } /* ** Set up the PCX palette. */ pal = (PCXCOLOR *)malloc(256*sizeof(PCXCOLOR)); for (k = 0; k < npal ; k++) { pal[k].r = (unsigned char)(colortable[k].r); pal[k].g = (unsigned char)(colortable[k].g); pal[k].b = (unsigned char)(colortable[k].b); } /* ** Fill in the PCX header */ hdr.manuf = (char)10; hdr.vers = (char)5; hdr.rle = (char)1; hdr.bitpx = 8; hdr.x1 = hdr.y1 = 0; hdr.x2 = resx - 1; hdr.y2 = resy - 1; hdr.hres = 640; hdr.vres = 480; hdr.hscreen = 0; hdr.vscreen = 0; hdr.bpline = hdr.x2; hdr.vmode = (char)0; hdr.nplanes = 1; hdr.palinfo = 1; memset(hdr.xtra,0x00,54); fwrite(&hdr, sizeof(PCXhdr), 1, pf); /* ** Scan the image and pack pixels for the PCX image */ buf = (unsigned char *)malloc(resx * sizeof(unsigned char)); for (i = 0; i < resy; i++) { fprintf(stderr, "%1.0f\%\r", 100.0*(float)i/(float)resy); /* ** Build a scan line of color table indices */ for (j = 0; j < resx; j++) { if (fscanf(f,"%lf %lf %lf",&r,&g,&b) == EOF) { fprintf(stderr, "\n\nUnexpected end of input file (%d,%d)\n", i, j); exit(1); } color.r = (byte)(r * nrgbr); color.g = (byte)(g * nrgbg); color.b = (byte)(b * nrgbb); if (color.r > nrgbr) color.r = nrgbr; if (color.g > nrgbg) color.g = nrgbg; if (color.b > nrgbb) color.b = nrgbb; buf[j] = QuantizeColor(octree, &color); } /* ** Encode this scan line and write it to the file. If return is non-0 ** then the write failed */ if (encline(buf, hdr.x2, pf)) { fclose(pf); free(buf); free(pal); return 1; } } /* ** Store the palette */ for (i = 0; i < 256; i++) { pal[i].r *= 4; pal[i].g *= 4; pal[i].b *= 4; } putc(0x0c, pf); fwrite(pal, 768, 1, pf); fclose(pf); free(buf); free(pal); return 0; }
/**************************************************************************** ** ** SaveAsPCX ** ** Write a PCX file version of the image */ int CPalette::SavePalettizedAsPCX( cv::Mat& image, // matrix is Palettized pixels char* pcxfile ) { int stat = 0; double nrgbr = 63, nrgbg =63, nrgbb = 63; int resx = image.cols; int resy = image.rows; int npal = m_PaletteSize; ColorData *colortable = m_table; // COctreeNode *octree = m_pOctree; PCXhdr hdr; // unsigned char *buf; PCXCOLOR *pal; int i,k; // RGBType color; unsigned char *pPix; FILE *pf; fopen_s(&pf,pcxfile, "wb"); if (pf == NULL) { // fprintf(stderr, "Can't open file %s for writing\n", pcxfile); return 1; } /* ** Set up the PCX Palettte. */ pal = (PCXCOLOR *)malloc(256*sizeof(PCXCOLOR)); if(!pal) { stat = 2; goto ESCAPE2; } for (k = 0; k < npal ; k++) { pal[k].r = (unsigned char)(colortable[k].color.r); pal[k].g = (unsigned char)(colortable[k].color.g); pal[k].b = (unsigned char)(colortable[k].color.b); } /* ** Fill in the PCX header */ hdr.manuf = (char)10; hdr.vers = (char)5; hdr.rle = (char)1; hdr.bitpx = 8; hdr.x1 = hdr.y1 = 0; hdr.x2 = resx - 1; hdr.y2 = resy - 1; hdr.hres = 640; hdr.vres = 480; hdr.hscreen = 0; hdr.vscreen = 0; hdr.bpline = hdr.x2; hdr.vmode = (char)0; hdr.nplanes = 1; hdr.palinfo = 1; memset(hdr.xtra,0x00,54); fwrite(&hdr, sizeof(PCXhdr), 1, pf); /* ** Scan the image and pack pixels for the PCX image */ for (i = 0; i < resy; i++) { pPix = image.ptr(i); /* ** Encode this scan line and write it to the file. If return is non-0 ** then the write failed */ if (encline(pPix, hdr.x2, pf)) { stat = 3; goto ESCAPE1; } } /* ** Store the palette */ #if(0) for (i = 0; i < 256; i++) { pal[i].r *= 4; pal[i].g *= 4; pal[i].b *= 4; } #endif putc(0x0c, pf); fwrite(pal, 768, 1, pf); ESCAPE1: free(pal); ESCAPE2: fclose(pf); return stat; }