Exemplo n.º 1
0
bool writeHDRImage(HDRImage& img, const std::string& filename)
{
    if(!img)
    {
        throw std::runtime_error("Image::Write called on incomplete image");
    }
    
    std::string extension = filename.substr(filename.length()-4, 4);
    
    int width =img.getDimensions()[1];
    int height=img.getDimensions()[2];
    int channels=img.getDimensions()[0];
    
    for(int i =0; i < img.numElements(); i++)
    {
        img[i] = std::max<float>(0.0, img[i]);
    }
    
    //special case branching... mostly becuase jpeg doens't write with this writer.  This allows autodetection of the extension regardless though.
    
    if(extension != ".hdr")
    {
        throw std::runtime_error("Unrecognized extension for writeHDRImage");
    }
    
    return stbi_write_hdr(filename.c_str(), width, height, channels, img.dataPtr());
}
Exemplo n.º 2
0
Arquivo: image.c Projeto: aiwl/ir
XBOOL hdr_image_save(HDRImage *image, const char *path)
{
    if (!image)
        return XFALSE;
    if (stbi_write_hdr(path, image->width, image->height, 3, &image->pixels[0][0]) < 0)
        return XFALSE;
    return XTRUE;
}
Exemplo n.º 3
0
void CImgWrapper<T>::save(const std::string& fn) const
{
	//imgptr->save(fn.c_str());
	std::string ext = fn.substr(fn.size()-4);
	//std::cerr << "Stuff" << ext;     
//	
//	int stbi_write_png(char const *filename, int w, int h, int comp, const void *data, int stride_in_bytes);
//	int stbi_write_bmp(char const *filename, int w, int h, int comp, const void *data);
//	int stbi_write_tga(char const *filename, int w, int h, int comp, const void *data);
//	int stbi_write_jpg(char const *filename, int w, int h, int comp, const void *data, int quality);
	
	if (ext == ".hdr" || ext==".HDR")
	{
		cimg_library::CImg<float> imgout = *imgptr;
		imgout.permute_axes("cxyz");
		stbi_write_hdr(fn.c_str(), imgptr->width(), imgptr->height(), imgptr->spectrum(), imgout.data());
	}
	else if (
		ext == ".png" || ext == ".PNG" || 
		ext==".bmp" || ext==".BMP" || 
		ext==".jpg" || ext==".JPG" || ext==".JPEG" || ext==".jpeg"||
		ext==".tga" || ext==".TGA")
	{
		cimg_library::CImg<unsigned char> imgout = *imgptr;
		imgout.permute_axes("cxyz");
		const char* fnc = fn.c_str(); int w = imgptr->width(); int h = imgptr->height(); int c = imgptr->spectrum();
		const unsigned char* dt = imgout.data();

		switch (ext[1])
		{
		case 'p':
		case 'P':
			stbi_write_png(fnc, w, h, c, dt, 0); break;
		case 'b':
		case 'B':
			stbi_write_bmp(fnc, w, h, c, dt); break;
		case 'j':
		case 'J':
			stbi_write_jpg(fnc, w, h, c, dt, 8); break;
		case 't':
		case 'T':
			stbi_write_tga(fnc, w, h, c, dt); break;
		};
	}
	else
	{
		imgptr->save(fn.c_str());
	}
}
Exemplo n.º 4
0
int image_ImageData_save(image_ImageData *dst, const char* format, const char* filename) {
	int succeded = 0;
	if (strncmp(format, "png", 3) == 0)
		succeded = stbi_write_png(filename, dst->w, dst->h, dst->c, (const float*)dst->pixels, 0);
	else if (strncmp(format, "bmp", 3) == 0)
     	succeded = stbi_write_bmp(filename, dst->w, dst->h, dst->c, (const float*)dst->surface);
	else if (strncmp(format, "tga", 3) == 0)
     	succeded = stbi_write_tga(filename, dst->w, dst->h, dst->c, (const float*)dst->pixels);
	else if (strncmp(format, "hdr", 3) == 0)
     	succeded = stbi_write_hdr(filename, dst->w, dst->h, dst->c, (const float*)dst->surface);
	else
		printf("%s %s %s \n", "Error, format:", format,  " is not avalabile.Only png,bmp,tga and hdr image formats are possible");
	if(succeded != 0)
		return 1;
	else {
		printf("%s %s \n", "Error: failed to save imageData: ", filename);
		return 0;
	}
}
Exemplo n.º 5
0
void ImageTargetFileStbImage::finalize()
{
	if( mExtension == "png" ) {
		if( ! stbi_write_png( mFilePath.string().c_str(), (int)mWidth, (int)mHeight, mNumComponents, mData.get(), (int)mRowBytes ) )
			throw ImageIoExceptionFailedWrite();
	}
	else if( mExtension == "bmp" ) {
		if( ! stbi_write_bmp( mFilePath.string().c_str(), (int)mWidth, (int)mHeight, mNumComponents, mData.get() ) )
			throw ImageIoExceptionFailedWrite();
	}
	else if( mExtension == "tga" ) {
		if( ! stbi_write_tga( mFilePath.string().c_str(), (int)mWidth, (int)mHeight, mNumComponents, mData.get() ) )
			throw ImageIoExceptionFailedWrite();
	}
	else if( mExtension == "hdr" ) {
		if( ! stbi_write_hdr( mFilePath.string().c_str(), (int)mWidth, (int)mHeight, mNumComponents, (const float*)mData.get() ) )
			throw ImageIoExceptionFailedWrite();
	}
}
Exemplo n.º 6
0
	    int write_hdr(char const *filename, int x, int y, int comp, Array<unsigned char> bytes,
        	unsigned int byteOffset, unsigned int byteLength)
	    {
	        return stbi_write_hdr(filename, x, y, comp, (float*)(&bytes[0] + byteOffset));
	    }
Exemplo n.º 7
0
int main(int argc, char **argv)
{
   int w,h;
   //test_ycbcr();

   #if 0
   // test hdr asserts
   for (h=0; h < 100; h += 2)
      for (w=0; w < 200; ++w)
         hdr_data[h][w][0] = (float) rand(),
         hdr_data[h][w][1] = (float) rand(),
         hdr_data[h][w][2] = (float) rand();

   stbi_write_hdr("output/test.hdr", 200,200,3,hdr_data[0][0]);
   #endif

   if (argc > 1) {
      int i, n;

      for (i=1; i < argc; ++i) {
         int res;
         int w2,h2,n2;
         unsigned char *data;
         printf("%s\n", argv[i]);
         res = stbi_info(argv[1], &w2, &h2, &n2);
         data = stbi_load(argv[i], &w, &h, &n, 4); if (data) free(data); else printf("Failed &n\n");
         data = stbi_load(argv[i], &w, &h,  0, 1); if (data) free(data); else printf("Failed 1\n");
         data = stbi_load(argv[i], &w, &h,  0, 2); if (data) free(data); else printf("Failed 2\n");
         data = stbi_load(argv[i], &w, &h,  0, 3); if (data) free(data); else printf("Failed 3\n");
         data = stbi_load(argv[i], &w, &h, &n, 4);
         assert(data);
         assert(w == w2 && h == h2 && n == n2);
         assert(res);
         if (data) {
            char fname[512];
            stb_splitpath(fname, argv[i], STB_FILE);
            stbi_write_png(stb_sprintf("output/%s.png", fname), w, h, 4, data, w*4);
            stbi_write_bmp(stb_sprintf("output/%s.bmp", fname), w, h, 4, data);
            stbi_write_tga(stb_sprintf("output/%s.tga", fname), w, h, 4, data);
            stbi_write_png_to_func(dummy_write,0, w, h, 4, data, w*4);
            stbi_write_bmp_to_func(dummy_write,0, w, h, 4, data);
            stbi_write_tga_to_func(dummy_write,0, w, h, 4, data);
            free(data);
         } else
            printf("FAILED 4\n");
      }
   } else {
      int i, nope=0;
      #ifdef PNGSUITE_PRIMARY
      char **files = stb_readdir_files("pngsuite/primary");
      #else
      char **files = stb_readdir_files("images");
      #endif
      for (i=0; i < stb_arr_len(files); ++i) {
         int n;
         char **failed = NULL;
         unsigned char *data;
         printf(".");
         //printf("%s\n", files[i]);
         data = stbi_load(files[i], &w, &h, &n, 0); if (data) free(data); else stb_arr_push(failed, "&n");
         data = stbi_load(files[i], &w, &h,  0, 1); if (data) free(data); else stb_arr_push(failed, "1");
         data = stbi_load(files[i], &w, &h,  0, 2); if (data) free(data); else stb_arr_push(failed, "2");
         data = stbi_load(files[i], &w, &h,  0, 3); if (data) free(data); else stb_arr_push(failed, "3");
         data = stbi_load(files[i], &w, &h,  0, 4); if (data)           ; else stb_arr_push(failed, "4");
         if (data) {
            char fname[512];

            #ifdef PNGSUITE_PRIMARY
            int w2,h2;
            unsigned char *data2;
            stb_splitpath(fname, files[i], STB_FILE_EXT);
            data2 = stbi_load(stb_sprintf("pngsuite/primary_check/%s", fname), &w2, &h2, 0, 4);
            if (!data2)
               printf("FAILED: couldn't load 'pngsuite/primary_check/%s\n", fname);
            else {
               if (w != w2 || h != w2 || 0 != memcmp(data, data2, w*h*4)) {
                  int x,y,c;
                  if (w == w2 && h == h2)
                     for (y=0; y < h; ++y)
                        for (x=0; x < w; ++x)
                           for (c=0; c < 4; ++c)
                              assert(data[y*w*4+x*4+c] == data2[y*w*4+x*4+c]);
                  printf("FAILED: %s loaded but didn't match PRIMARY_check 32-bit version\n", files[i]);
               }
               free(data2);
            }
            #else
            stb_splitpath(fname, files[i], STB_FILE);
            stbi_write_png(stb_sprintf("output/%s.png", fname), w, h, 4, data, w*4);
            #endif
            free(data);
         }
         if (failed) {
            int j;
            printf("FAILED: ");
            for (j=0; j < stb_arr_len(failed); ++j)
               printf("%s ", failed[j]);
            printf(" -- %s\n", files[i]);
         }
      }
      printf("Tested %d files.\n", i);
   }
   return 0;
}
Exemplo n.º 8
0
void image::saveHDR(const std::string &baseFilename) {
    std::string filename = baseFilename + ".hdr";
    stbi_write_hdr(filename.c_str(), xSize, ySize, 3, (const float *) pixels);
    std::cout << "Saved " + filename + "." << std::endl;
}
JNIEXPORT jint JNICALL Java_org_lwjgl_stb_STBImageWrite_nstbi_1write_1hdr(JNIEnv *__env, jclass clazz, jlong filenameAddress, jint w, jint h, jint comp, jlong dataAddress) {
	const char *filename = (const char *)(intptr_t)filenameAddress;
	const float *data = (const float *)(intptr_t)dataAddress;
	UNUSED_PARAMS(__env, clazz)
	return (jint)stbi_write_hdr(filename, w, h, comp, data);
}