void WriteImage(const std::string &name, const Float *rgb, const Bounds2i &outputBounds, const Point2i &totalResolution, Float gamma) { if (name.size() >= 5) { size_t suffixOffset = name.size() - 4; if (!strcmp(name.c_str() + suffixOffset, ".exr") || !strcmp(name.c_str() + suffixOffset, ".EXR")) { WriteImageEXR(name, rgb, outputBounds.pMax.x - outputBounds.pMin.x, outputBounds.pMax.y - outputBounds.pMin.y, totalResolution.x, totalResolution.y, outputBounds.pMin.x, outputBounds.pMin.y); return; } if (!strcmp(name.c_str() + suffixOffset, ".pfm") || !strcmp(name.c_str() + suffixOffset, ".PFM")) { Vector2i resolution = outputBounds.Diagonal(); WriteImagePFM(name, rgb, resolution.x, resolution.y); return; } if (!strcmp(name.c_str() + suffixOffset, ".tga") || !strcmp(name.c_str() + suffixOffset, ".TGA") || !strcmp(name.c_str() + suffixOffset, ".png") || !strcmp(name.c_str() + suffixOffset, ".PNG")) { // 8-bit formats; apply gamma Vector2i resolution = outputBounds.Diagonal(); std::unique_ptr<uint8_t[]> rgb8( new uint8_t[3 * resolution.x * resolution.y]); uint8_t *dst = rgb8.get(); for (int y = 0; y < resolution.y; ++y) { for (int x = 0; x < resolution.x; ++x) { #define TO_BYTE(v) (uint8_t(Clamp(255.f * powf((v), 1.f / gamma), 0.f, 255.f))) dst[0] = TO_BYTE(rgb[3 * (y * resolution.x + x) + 0]); dst[1] = TO_BYTE(rgb[3 * (y * resolution.x + x) + 1]); dst[2] = TO_BYTE(rgb[3 * (y * resolution.x + x) + 2]); #undef TO_BYTE dst += 3; } } if (!strcmp(name.c_str() + suffixOffset, ".tga") || !strcmp(name.c_str() + suffixOffset, ".TGA")) WriteImageTGA(name, rgb8.get(), outputBounds.pMax.x - outputBounds.pMin.x, outputBounds.pMax.y - outputBounds.pMin.y, totalResolution.x, totalResolution.y, outputBounds.pMin.x, outputBounds.pMin.y); else if (stbi_write_png(name.c_str(), resolution.x, resolution.y, 3, rgb8.get(), 3 * resolution.x) == 0) Error("Error writing PNG \"%s\"", name.c_str()); return; } } Error("Can't determine image file type from suffix of filename \"%s\"", name.c_str()); }
void WriteImage(const std::string &name, const Float *rgb, const Bounds2i &outputBounds, const Point2i &totalResolution) { Vector2i resolution = outputBounds.Diagonal(); if (HasExtension(name, ".exr")) { WriteImageEXR(name, rgb, resolution.x, resolution.y, totalResolution.x, totalResolution.y, outputBounds.pMin.x, outputBounds.pMin.y); } else if (HasExtension(name, ".exr")) { WriteImagePFM(name, rgb, resolution.x, resolution.y); } else if (HasExtension(name, ".tga") || HasExtension(name, ".png")) { // 8-bit formats; apply gamma Vector2i resolution = outputBounds.Diagonal(); std::unique_ptr<uint8_t[]> rgb8( new uint8_t[3 * resolution.x * resolution.y]); uint8_t *dst = rgb8.get(); for (int y = 0; y < resolution.y; ++y) { for (int x = 0; x < resolution.x; ++x) { #define TO_BYTE(v) (uint8_t) Clamp(255.f * GammaCorrect(v) + 0.5f, 0.f, 255.f) dst[0] = TO_BYTE(rgb[3 * (y * resolution.x + x) + 0]); dst[1] = TO_BYTE(rgb[3 * (y * resolution.x + x) + 1]); dst[2] = TO_BYTE(rgb[3 * (y * resolution.x + x) + 2]); #undef TO_BYTE dst += 3; } } if (HasExtension(name, ".tga")) WriteImageTGA( name, rgb8.get(), outputBounds.pMax.x - outputBounds.pMin.x, outputBounds.pMax.y - outputBounds.pMin.y, totalResolution.x, totalResolution.y, outputBounds.pMin.x, outputBounds.pMin.y); else if (stbi_write_png(name.c_str(), resolution.x, resolution.y, 3, rgb8.get(), 3 * resolution.x) == 0) Error("Error writing PNG \"%s\"", name.c_str()); } else { Error("Can't determine image file type from suffix of filename \"%s\"", name.c_str()); } }
int main(int argc, char *argv[]) { int nx, ny, ncha, cx, cy; int xmin, xmax, ymin, ymax; int x0,x1,y0,y1; int cxy,nxy; float *data, *d; char flag_mc = 1; if(argc < 6) { printf("not enough arguments...\n"); printf("usage:\n"); printf(" %s x0 y0 x1 y1 img-exr img-crop-exr\n",argv[0]); exit(0); } data = readMultiImageEXR(argv[5], &nx, &ny, &ncha); if (!data) { data = ReadImageEXR(argv[5], &nx, &ny); ncha = 3; flag_mc = 0; if(!data) { printf("error :: %s not a correct exr image \n", argv[5]); exit(-1); } } x0 = atoi(argv[1]); y0 = atoi(argv[2]); x1 = atoi(argv[3]); y1 = atoi(argv[4]); xmin = MAX(0,MIN(x0,x1)); xmax = MIN(nx-1,MAX(x0,x1)); ymin = MAX(0,MIN(y0,y1)); ymax = MIN(ny-1,MAX(y0,y1)); cx = xmax - xmin; cy = ymax - ymin; printf("Cropping: xmin:%d xmax:%d ymin:%d ymax:%d\n",xmin,xmax,ymin,ymax); //if cx or cy is negative parameters wrong if(cx <= 0 || cy <= 0) { printf("error :: incorrect crop values\n"); exit(-1); } nxy = nx * ny; cxy = cx * cy; d = (float*)malloc(sizeof(float) * cxy * ncha); /*Crop Image*/ for(int y=0;y<cy;y++) for(int x=0;x<cx;x++) for(int c=0;c<ncha;c++) d[x + cx*y + cxy*c] = data[x + xmin + nx*(y+ymin) + nxy*c]; if(flag_mc) { writeMultiImageEXR(argv[6], d, cx, cy, ncha); } else { WriteImageEXR(argv[6], d, cx, cy); } free(data); free(d); return 0; }