Esempio n. 1
0
int main(int argc, char *argv[]) {
    IMG_DeepShadow fp;
    int xres, yres;

    if (argc < 3 || !fp.open(argv[1])) {
    	usage(argv[0]);
    }

    char * sdfFileName = argv[2];
    std::vector<std::string> channelNames;

    // Read the texture options in the file
    dumpOptions(fp);

    // Query the resolution
    fp.resolution(xres, yres);
    printf("%s[%d,%d] (%d channels)\n", argv[1], xres, yres, fp.getChannelCount());

    for (int i = 0; i < fp.getChannelCount(); i++) {
    	const IMG_DeepShadowChannel * chp = fp.getChannel(i);
    	printf("%s ", chp->getName());
    	if (strcmp(chp->getName(), "C") == 0) {
    		channelNames.push_back("R");
    		channelNames.push_back("G");
    		channelNames.push_back("B");
    		channelNames.push_back("A");
    	}
    }
    channelNames.push_back("Z");
    channelNames.push_back("ZBack");
    std::cout << std::endl;

    DeepImage * deepImage = new DeepImage(xres, yres, channelNames);
    std::cout << "Pre load" << std::endl;
    printDeepImageStats(*deepImage);

    IMG_DeepPixelReader pixel(fp);

    const IMG_DeepShadowChannel * pzp = nullptr;
    const IMG_DeepShadowChannel * ofp = nullptr;
    const IMG_DeepShadowChannel * cp = nullptr;

    for (int i = 0; i < fp.getChannelCount(); i++) {
		if (strcmp(fp.getChannel(i)->getName(), "Pz") == 0)
			pzp = fp.getChannel(i);
		else if (strcmp(fp.getChannel(i)->getName(), "Of") == 0) {
			ofp = fp.getChannel(i);
		} else if (strcmp(fp.getChannel(i)->getName(), "C") == 0) {
			cp = fp.getChannel(i);
		}
	}

//    const IMG_DeepShadowChannel * chp;
    const UT_Options * opt = fp.getTBFOptions();
    std::string interp = opt->getOptionS("texture:depth_interp");
    bool linearInterp = (interp.compare("continuous") == 0);
    int numChannels = channelNames.size();

    for (int y = 0; y < yres; ++y) {
        for (int x = 0; x < xres; ++x) {
        	if (!pixel.open(x, y)) {
				printf("\tUnable to open pixel [%d,%d]!\n", x, y);
				return 0;
			}
        	pixel.uncomposite(*pzp, *ofp, true);
        	// Get the number of z-records for the pixel
			int numSamples = pixel.getDepth();
			if (numSamples > 0) {
				for (int d = 0; d < numSamples; ++d) {
					std::vector<DeepDataType> values(numChannels);
					// Get color:
					const float * c = pixel.getData(*cp, d);
					const float * pz = pixel.getData(*pzp, d);
					const float * pzBack = pz;
					if (linearInterp) {
						if (d + 1 < numSamples) {
							pzBack = pixel.getData(*pzp, d + 1);
						} else {
							continue;
						}
					}
					for (int i = 0; i < cp->getTupleSize(); ++i) {
						values[i] = c[i];
						if (i < 3 && c[3] > 0.0) {
							// Unpremult.
							values[i] /= c[3];
						}
					}
					values[4] = pz[0];
					values[5] = pzBack[0];
//					for (int i = 0; i < fp.getChannelCount(); i++) {
//						chp = fp.getChannel(i);
//						int offset = -1;
//						if (strcmp(chp->getName(), "C") == 0) {
//							offset = 0;
//						} else if (strcmp(chp->getName(), "Pz") == 0) {
//							offset = 4;
//						}
//						if (offset >= 0) {
//							const float * p = pixel.getData(*chp, d);
//							for (int t = 0; t < chp->getTupleSize(); ++t) {
//								values[offset+t] = p[t];
//							}
//						}
//					}
//					values[5] = values[4];
//					values[0] /= values[3]; values[1] /= values[3]; values[2] /= values[3];
					deepImage->addSample(yres-y-1, x, values);
				}
			}
        }
    }

    std::cout << "Post load" << std::endl;
    printDeepImageStats(*deepImage);
    DeepImageWriter writer(sdfFileName, *deepImage);
    if (writer.open()) {
    	writer.write();
    	writer.close();
    }
    delete deepImage;

    // Print the raw pixel data
//	printPixel(fp, 0, 0);
//	printPixel(fp, xres >> 1, 0);
//	printPixel(fp, xres - 1, 0);
	printPixel(fp, 0, yres >> 1);
	printPixel(fp, xres >> 1, yres >> 1);
//	printPixel(fp, xres - 1, yres >> 1);
//	printPixel(fp, 0, yres - 1);
	printPixel(fp, xres >> 1, yres - 1);
//	printPixel(fp, xres - 1, yres - 1);

    return 0;
}