bool saveAsPPM(const std::unique_ptr<Image>& img_ptr, const char* filepath) { FILE* fp = fopen(filepath, "wb"); if (!fp) { printf("error loading file %s at saveAsPPM()\n", filepath); return false; } const size_t w = img_ptr->getWidth(); const size_t h = img_ptr->getHeight(); const pixval maxVal = 255; pixel** pxls = ppm_allocarray(w, h); if (!pxls) { printf("error allocating memory space at saveAsPPM()"); fclose(fp); return false; } for (size_t y = 0; y < h; y++) { for (size_t x = 0; x < w; x++) { if (img_ptr->getChannelNum() >= 3) { float vclip[3]; for (size_t c = 0; c < 3; c++) { vclip[c] = (img_ptr->getValues(c))[y*w + x]; if (vclip[c] < 0) vclip[c] = 0; if (vclip[c] > 1) vclip[c] = 1; } pxls[y][x].r = static_cast<pixval>(vclip[0] * maxVal); pxls[y][x].g = static_cast<pixval>(vclip[1] * maxVal); pxls[y][x].b = static_cast<pixval>(vclip[2] * maxVal); } else { float vclip = (img_ptr->getValues(0))[y*w + x]; if (vclip < 0) vclip = 0; if (vclip > 1) vclip = 1; pxls[y][x].r = static_cast<pixval>(vclip * maxVal); pxls[y][x].g = static_cast<pixval>(vclip * maxVal); pxls[y][x].b = static_cast<pixval>(vclip * maxVal); } } } ppm_writeppm(fp, pxls, w, h, maxVal, 0); ppm_freearray(pxls, h); fclose(fp); return true; }
ppm_t *pnm_ppm_alloc(ppm_t *ppm, int cols, int rows) { if (!ppm) ppm = pnm_ppm_create(); ppm->rows = rows; ppm->cols = cols; ppm->pixels = ppm_allocarray(cols, rows); return (ppm); }
void save_depth_ppm(char *file_name, struct depth_image_t *image, int max) { pixel **data = ppm_allocarray(IMG_WIDTH, IMG_HEIGHT); // Copy data back into PPM structure for(int x = 0; x < IMG_WIDTH; x++) { for(int y = 0; y < IMG_HEIGHT; y++) { struct depth8_t p = image->pixels[x][y]; // PPM data is indexed by y, then x PPM_ASSIGN(data[y][x], p.d, p.d, p.d); } } FILE *outfp = fopen(file_name, "w"); ppm_writeppm(outfp, data, IMG_WIDTH, IMG_HEIGHT, max, 0); }
static void createCanvas(unsigned int const width, unsigned int const height, pixval const maxval, canvas ** const canvasPP) { canvas * canvasP; MALLOCVAR_NOFAIL(canvasP); canvasP->width = width; canvasP->height = height; canvasP->pixels = ppm_allocarray(width, height); canvasP->maxval = maxval; *canvasPP = canvasP; }
void savebmp(const char *filename, float wf, float hf, int dpi, std::vector< std::vector <Pixel> >* data) { /* Example program fragment to read a PAM or PNM image from stdin, add up the values of every sample in it (I don't know why), and write the image unchanged to stdout. */ FILE* f = fopen(filename,"wb"); pm_init(filename, 0); pixel** truePix = ppm_allocarray(wf,hf); for (int i = 0; i < wf; i++) { for (int j = 0; j < hf; j++) { PPM_ASSIGN(truePix[i][j],data->at(i).at(j).getColor()->getRed(), data->at(i).at(j).getColor()->getGreen(), data->at(i).at(j).getColor()->getBlue()); } } ppm_writeppm(f, truePix, (int)wf, (int)hf, 256, 0); ppm_freearray(truePix, (int)hf); }
ppm_t *pnm_image2ppm(ppm_t *ppm, pnm_image_t *image) { int i,j,k; if (!image) return (ppm); if (!ppm) { ppm = malloc(sizeof(ppm_t)); ppm_allocarray(ppm->cols=image->cols, ppm->rows=image->rows); } for (j=0; j < ppm->rows; j++) for (k=0; k < ppm->cols; k++) PPM_ASSIGN(ppm->pixels[j][k], (gray) image->pixels[0][j][k], (gray) image->pixels[1][j][k], (gray) image->pixels[2][j][k]); ppm->maxval = (gray) image->maxval; return (ppm); }
void write_img(FILE *f, const char *fname, int n, int num_img) { FILE *out; int32_t width, height; int x, y; unsigned char p; pixel **img; char newname[1024]; const char *basename; fseek(f, 116 + 40 * (num_img - 1), SEEK_SET); width = read_LEint32(f); height = read_LEint32(f); img = ppm_allocarray(width, height); fseek(f, 100 + 40 * num_img + (width * height + 24) * n, SEEK_SET); for (y = 0; y < height; y++) for (x = 0; x < width; x++) { p = getc(f); img[y][x] = cmap[p]; } basename = strrchr(fname, '/'); if (basename != NULL) basename++; else basename = fname; strcpy(newname, basename); if (strlen(newname) > 4 && strcasecmp(newname + strlen(newname) - 4, ".mat") == 0) newname[strlen(newname) - 4] = '\0'; sprintf(newname + strlen(newname), "_%d.ppm", n); out = fopen(newname, "wb"); if (out == NULL) { perror(newname); exit(1); } ppm_writeppm(out, img, width, height, 255, 0); ppm_freearray(img, height); fclose(out); }
int main(int argc, char **argv) { FILE *infile, *outfile; int cols, rows; pixval maxval; pixel **pic; int ncols, nrows; pixel **npic; int r,c; int x1,y1,x2,y2; if ( argc != 7) Usage(); infile=fopen(argv[1],"r"); if (!infile) { fprintf(stderr,"Can't open file %s for reading!\n",argv[1]); exit(0); } outfile=fopen(argv[2],"w"); if (!outfile) { fprintf(stderr,"Can't open file %s for writing!\n",argv[2]); exit(0); } sscanf(argv[3],"%d",&x1); sscanf(argv[4],"%d",&y1); sscanf(argv[5],"%d",&x2); sscanf(argv[6],"%d",&y2); pic=ppm_readppm(infile,&cols,&rows,&maxval); printf("cols=%d, rows=%d, region=(%d,%d) (%d,%d)\n",cols,rows,x1,y1,x2,y2); ncols=x2-x1+1; nrows=y2-y1+1; npic=ppm_allocarray(ncols,nrows); for (r=0; r<nrows; r++) for (c=0; c<ncols; c++) npic[r][c]=pic[y1+r][x1+c]; printf("Cutting done\n"); ppm_writeppm(outfile,npic,ncols,nrows,maxval,0); fclose(infile); fclose(outfile); return 1; }
int main(int argc, char * argv[]) { int argn; const char * const usage = "[-[no]black] [-[no]wpoint] [-[no]label] [-no[axes]] [-full]\n\ [-xy|-upvp] [-rec709|-ntsc|-ebu|-smpte|-hdtv|-cie]\n\ [-red <x> <y>] [-green <x> <y>] [-blue <x> <y>]\n\ [-white <x> <y>] [-gamma <g>]\n\ [-size <s>] [-xsize|-width <x>] [-ysize|-height <y>]"; const struct colorSystem *cs; int widspec = FALSE, hgtspec = FALSE; int xBias, yBias; int upvp = FALSE; /* xy or u'v' color coordinates? */ int showWhite = TRUE; /* Show white point ? */ int showBlack = TRUE; /* Show black body curve ? */ int fullChart = FALSE; /* Fill entire tongue ? */ int showLabel = TRUE; /* Show labels ? */ int showAxes = TRUE; /* Plot axes ? */ ppm_init(&argc, argv); argn = 1; cs = &Rec709system; /* default */ while (argn < argc && argv[argn][0] == '-' && argv[argn][1] != '\0') { if (pm_keymatch(argv[argn], "-xy", 2)) { upvp = FALSE; } else if (pm_keymatch(argv[argn], "-upvp", 1)) { upvp = TRUE; } else if (pm_keymatch(argv[argn], "-xsize", 1) || pm_keymatch(argv[argn], "-width", 2)) { if (widspec) { pm_error("already specified a size/width/xsize"); } argn++; if ((argn == argc) || (sscanf(argv[argn], "%d", &sxsize) != 1)) pm_usage(usage); widspec = TRUE; } else if (pm_keymatch(argv[argn], "-ysize", 1) || pm_keymatch(argv[argn], "-height", 2)) { if (hgtspec) { pm_error("already specified a size/height/ysize"); } argn++; if ((argn == argc) || (sscanf(argv[argn], "%d", &sysize) != 1)) pm_usage(usage); hgtspec = TRUE; } else if (pm_keymatch(argv[argn], "-size", 2)) { if (hgtspec || widspec) { pm_error("already specified a size/height/ysize"); } argn++; if ((argn == argc) || (sscanf(argv[argn], "%d", &sysize) != 1)) pm_usage(usage); sxsize = sysize; hgtspec = widspec = TRUE; } else if (pm_keymatch(argv[argn], "-rec709", 1)) { cs = &Rec709system; } else if (pm_keymatch(argv[argn], "-ntsc", 1)) { cs = &NTSCsystem; } else if (pm_keymatch(argv[argn], "-ebu", 1)) { cs = &EBUsystem; } else if (pm_keymatch(argv[argn], "-smpte", 2)) { cs = &SMPTEsystem; } else if (pm_keymatch(argv[argn], "-hdtv", 2)) { cs = &HDTVsystem; } else if (pm_keymatch(argv[argn], "-cie", 1)) { cs = &CIEsystem; } else if (pm_keymatch(argv[argn], "-black", 3)) { showBlack = TRUE; /* Show black body curve */ } else if (pm_keymatch(argv[argn], "-wpoint", 2)) { showWhite = TRUE; /* Show white point of color system */ } else if (pm_keymatch(argv[argn], "-noblack", 3)) { showBlack = FALSE; /* Don't show black body curve */ } else if (pm_keymatch(argv[argn], "-nowpoint", 3)) { showWhite = FALSE; /* Don't show white point of system */ } else if (pm_keymatch(argv[argn], "-label", 1)) { showLabel = TRUE; /* Show labels. */ } else if (pm_keymatch(argv[argn], "-nolabel", 3)) { showLabel = FALSE; /* Don't show labels */ } else if (pm_keymatch(argv[argn], "-axes", 1)) { showAxes = TRUE; /* Show axes. */ } else if (pm_keymatch(argv[argn], "-noaxes", 3)) { showAxes = FALSE; /* Don't show axes */ } else if (pm_keymatch(argv[argn], "-full", 1)) { fullChart = TRUE; /* Fill whole tongue full-intensity */ } else if (pm_keymatch(argv[argn], "-gamma", 2)) { cs = &Customsystem; argn++; if ((argn == argc) || (sscanf(argv[argn], "%lf", &Customsystem.gamma) != 1)) pm_usage(usage); } else if (pm_keymatch(argv[argn], "-red", 1)) { cs = &Customsystem; argn++; if ((argn == argc) || (sscanf(argv[argn], "%lf", &Customsystem.xRed) != 1)) pm_usage(usage); argn++; if ((argn == argc) || (sscanf(argv[argn], "%lf", &Customsystem.yRed) != 1)) pm_usage(usage); } else if (pm_keymatch(argv[argn], "-green", 1)) { cs = &Customsystem; argn++; if ((argn == argc) || (sscanf(argv[argn], "%lf", &Customsystem.xGreen) != 1)) pm_usage(usage); argn++; if ((argn == argc) || (sscanf(argv[argn], "%lf", &Customsystem.yGreen) != 1)) pm_usage(usage); } else if (pm_keymatch(argv[argn], "-blue", 1)) { cs = &Customsystem; argn++; if ((argn == argc) || (sscanf(argv[argn], "%lf", &Customsystem.xBlue) != 1)) pm_usage(usage); argn++; if ((argn == argc) || (sscanf(argv[argn], "%lf", &Customsystem.yBlue) != 1)) pm_usage(usage); } else if (pm_keymatch(argv[argn], "-white", 1)) { cs = &Customsystem; argn++; if ((argn == argc) || (sscanf(argv[argn], "%lf", &Customsystem.xWhite) != 1)) pm_usage(usage); argn++; if ((argn == argc) || (sscanf(argv[argn], "%lf", &Customsystem.yWhite) != 1)) pm_usage(usage); } else { pm_usage(usage); } argn++; } if (argn != argc) { /* Extra bogus arguments ? */ pm_usage(usage); } pixcols = sxsize; pixrows = sysize; pixels = ppm_allocarray(pixcols, pixrows); /* Partition into plot area and axes and establish subwindow. */ xBias = Sz(32); yBias = Sz(20); makeAllBlack(pixels, pixcols, pixrows); drawTongueOutline(pixels, pixcols, pixrows, Maxval, upvp, xBias, yBias); fillInTongue(pixels, pixcols, pixrows, Maxval, cs, upvp, xBias, yBias, fullChart); if (showAxes) drawAxes(pixels, pixcols, pixrows, Maxval, upvp, xBias, yBias); if (showWhite) plotWhitePoint(pixels, pixcols, pixrows, Maxval, cs, upvp, xBias, yBias); if (showBlack) plotBlackBodyCurve(pixels, pixcols, pixrows, Maxval, upvp, xBias, yBias); /* Plot wavelengths around periphery of the tongue. */ if (showAxes) plotMonochromeWavelengths(pixels, pixcols, pixrows, Maxval, cs, upvp, xBias, yBias); if (showLabel) writeLabel(pixels, pixcols, pixrows, Maxval, cs); ppm_writeppm(stdout, pixels, pixcols, pixrows, Maxval, FALSE); return 0; }
static void ppmHist(FILE * const ifP, unsigned int const cols, unsigned int const rows, xelval const maxval, int const format, bool const dots, bool const no_white, bool const no_black, bool const colorWanted[3], bool const verbose, xelval const startval, xelval const endval, unsigned int const histWidth, unsigned int const histHeight, bool const clipSpec, unsigned int const clipCount) { pixel ** pixels; unsigned int i; unsigned int * hist[3]; /* Subscript is enum wantedColor */ double vscale; unsigned int hmax; createHist(colorWanted, histWidth, &hist); if ((pixels = ppm_allocarray (histWidth, histHeight)) == NULL) pm_error("no space for output array (%u pixels)", histWidth * histHeight); for (i = 0; i < histHeight; ++i) memset(pixels[i], 0, histWidth * sizeof(pixels[i][0])); fillPpmBins(ifP, cols, rows, maxval, format, colorWanted, verbose, startval, endval, histWidth, hist); /* find the highest-valued slot and set the vertical scale value */ if (verbose) pm_message("finding max. slot height..."); if (clipSpec) hmax = clipCount; else hmax = maxSlotCountAll(hist, histWidth, no_white, no_black); assert(hmax > 0); clipHistogramAll(hist, histWidth, hmax); vscale = (double) histHeight / hmax; if (verbose && pm_have_float_format()) pm_message("Done: height = %u, vertical scale factor = %g", hmax, vscale); for (i = 0; i < histWidth; ++i) { if (hist[WANT_RED]) { unsigned int j; bool plotted; plotted = FALSE; for (j = histHeight - (int)(vscale * hist[WANT_RED][i]); j < histHeight && !plotted; ++j) { PPM_PUTR(pixels[j][i], maxval); plotted = dots; } } if (hist[WANT_GRN]) { unsigned int j; bool plotted; plotted = FALSE; for (j = histHeight - (int)(vscale * hist[WANT_GRN][i]); j < histHeight && !plotted; ++j) { PPM_PUTG(pixels[j][i], maxval); plotted = dots; } } if (hist[WANT_BLU]) { unsigned int j; bool plotted; plotted = FALSE; for (j = histHeight - (int)(vscale * hist[WANT_BLU][i]); j < histHeight && !plotted; ++j) { PPM_PUTB(pixels[j][i], maxval); plotted = dots; } } } ppm_writeppm(stdout, pixels, histWidth, histHeight, maxval, 0); }
/** write a scrap of the data, * return the filename, * if the file exist do nothing * argument fname is optional * the coordinates of the image part are geodata e.g. Gauss Krueger **/ QString GeoImage::part(float west, float north, float east, float south, QString fname) { if (fname.isEmpty()) { //create output filname Q_ASSERT(contains("key")); QString dir = CleanUp::getTmpDir(); fname.sprintf("%s/%s_%f_%f_%f_%f", dir.toLatin1().constData(), value("key").toLatin1().constData(), west, north, east, south); } qDebug("# GeoImage::part %s (%f, %f, %f, %f)", fname.toLatin1().constData(), west, north, east, south); QFile f(fname); if (f.exists()) return fname; const void *data_p = data(); //get pointer to data Q_ASSERT(data_p); if (type_ == UNKNOWN) return 0; int dx, dy, i, j, rx1, ry1, rx2, ry2; picBBox(west, north, east, south, rx1, ry2, rx2, ry1); dx = rx2 - rx1 + 1; dy = ry2 - ry1 + 1; if (dx <= 0 || dy <= 0) { qDebug("# (ERROR) GeoImage::part: (dx=%d=%d-%d || dy=%d=%d-%d)", dx, rx2, rx1, dy, ry2, ry1); throw ImageException(rx1,rx2,dx,ry1,ry2,dy); } FILE *of = fopen(fname.toLatin1().constData(), "w"); if (!of) { throw FileIOException(FileIOException::OPEN_FAILED,fname); } switch (type_) { case PBM:{ bit *bpi, *bpo, **dat_b_out = pbm_allocarray(dx, dy); bit **dat_b = (bit **) data_p; for (i = 0; i < dy; i++) { bpi = dat_b[i + ry1] + rx1; bpo = dat_b_out[i]; for (j = 0; j < dx; j++) { *bpo = *bpi; bpo++; bpi++; } } pbm_writepbm(of, (bit **) dat_b_out, dx, dy, 0); pbm_freearray(dat_b_out, dy); } break; case PGM:{ gray *gpi, *gpo, **dat_g_out = pgm_allocarray(dx, dy); gray **dat_g = (gray **) data_p; gray maxval = 0; for (i = 0; i < dy; i++) { gpi = dat_g[i + ry1] + rx1; gpo = dat_g_out[i]; for (j = 0; j < dx; j++) { *gpo = *gpi; if (*gpi > maxval) maxval = *gpi; gpo++; gpi++; } } pgm_writepgm(of, (gray **) dat_g_out, dx, dy, maxval, 0); pgm_freearray(dat_g_out, dy); } break; case PPM:{ pixel *ppi, *ppo, **dat_p_out = ppm_allocarray(dx, dy); pixel **dat_p = (pixel **) data_p; pixval maxval = 255; //! should be calculated for (i = 0; i < dy; i++) { ppi = dat_p[i + ry1] + rx1; ppo = dat_p_out[i]; for (j = 0; j < dx; j++) { *ppo = *ppi; ppo++; ppi++; } } ppm_writeppm(of, (pixel **) dat_p_out, dx, dy, maxval, 0); ppm_freearray(dat_p_out, dy); } break; default: pfm_geo_set(geoWest(),geoNorth(),geoEast(),geoSouth()); pfm_writepfm_region_type(of, data_, cols_, rows_, minval_, maxval_, rx1, ry1, rx2, ry2, type_); break; } fclose(of); return fname; }