int learnImage(const char * filename ,unsigned int numberOfHorizontalTiles,unsigned int numberOfVerticalTiles)
{
   struct Image * inputImage = readImage(filename,guessFilenameTypeStupid(filename),0);
   if (inputImage!=0)
   {
     unsigned int tileWidth =inputImage->width/numberOfHorizontalTiles;
     unsigned int tileHeight=inputImage->height/numberOfVerticalTiles;
     fprintf(stderr,"Sucessfully opened image ( %ux%u ) , each tile is %ux%u ..\n",inputImage->width,inputImage->height,tileWidth,tileHeight);


     unsigned int x,y,i=0;
     for (y=0; y<numberOfVerticalTiles; y++)
     {
      for (x=0; x<numberOfHorizontalTiles; x++)
      {
        struct Image * part = createImageBitBlt(inputImage,x*tileWidth , y*tileHeight , tileWidth, tileHeight);
        if (part!=0)
        {
          char tileString[512]={0};
          snprintf(tileString,512,"tile-%05d.jpg",i);
          writeImageFile(part,JPG_CODEC,tileString);
          destroyImage(part);
        }
       ++i;
      }
     }


     destroyImage(inputImage);
     return 1;
   }
  return 0;
}
static void emergencyDump(int quit)
{
  extern sqInt preSnapshot(void);
  extern sqInt postSnapshot(void);
  extern void writeImageFile(sqInt);
  char savedName[MAXPATHLEN];
  char baseName[MAXPATHLEN];
  char *term;
  int  dataSize, i;
  strncpy(savedName, imageName, MAXPATHLEN);
  strncpy(baseName, imageName, MAXPATHLEN);
  if ((term= strrchr(baseName, '.')))
    *term= '\0';
  for (i= 0; ++i;)
    {
      struct stat sb;
      sprintf(imageName, "%s-emergency-dump-%d.image", baseName, i);
      if (stat(imageName, &sb))
	break;
    }
  dataSize= preSnapshot();
  writeImageFile(dataSize);

  fprintf(stderr, "\n");
  printCallStack();
  fprintf(stderr, "\nTo recover valuable content from this image:\n");
  fprintf(stderr, "    squeak %s\n", imageName);
  fprintf(stderr, "and then evaluate\n");
  fprintf(stderr, "    Smalltalk processStartUpList: true\n");
  fprintf(stderr, "in a workspace.  DESTROY the dumped image after recovering content!");

  if (quit) abort();
  strncpy(imageName, savedName, sizeof(imageName));
}
Exemple #3
0
void testFlatFile1(std::string filename) {
	int x = 640;
	int y = 480;
	std::vector<std::string> c = {"R", "G", "B", deep::ALPHA};
	deep::Image * img = new deep::Image(x, y, c, "Nearest");
	drawCircle(0.6, 0.6, 0.2, {0.f, 0.f, 1.f, 0.5f}, img);
	drawCircle(0.4, 0.4, 0.2, {0.0, 1.0, 0.0, 0.25f}, img);
	writeImageFile(filename, x, y, 4, img->data(0,0,0));
	delete img;
}
Exemple #4
0
void testDeepReader(std::string deepFilename, std::string filenameRead) {
	deep::DeepImageReader reader(deepFilename);
	deep::DeepImage * d1 = reader.read();
	printDeepImageStats(*d1);

	deep::Image * img = deep::renderDeepImage(*d1);
	printFlatImageStats(*img);
	writeImageFile(filenameRead, img->width(), img->height(), 4, img->data(0,0,0));
	delete img;
	delete d1;
}
inline bool doWrite(const osg::Object & obj, WriteType type, const std::string& fileName, const Options * options)
{
    switch(type) {
        case WRITE_TYPE_IMAGE:        return writeImageFile      (static_cast<const osg::Image       &>(obj), fileName, options);
        case WRITE_TYPE_HEIGHT_FIELD: return writeHeightFieldFile(static_cast<const osg::HeightField &>(obj), fileName, options);
        case WRITE_TYPE_NODE:         return writeNodeFile       (static_cast<const osg::Node        &>(obj), fileName, options);
        case WRITE_TYPE_SHADER:       return writeShaderFile     (static_cast<const osg::Shader      &>(obj), fileName, options);
        // WRITE_TYPE_OBJECT
        default:                      return writeObjectFile(obj, fileName, options);
    }
}
Exemple #6
0
void writePngFileBase64(char * buf,int lenBuf,char * title,int lenTitle){
  int lenDec = -1;
  char * convert = base64_decode(buf,lenBuf,&lenDec);
  int lenBase64 = strlen(convert);
  char path[256] = "images/";
  checkOrCreateDir(path); //	
  char dot[] =".png";
  strcat(path,title);	
  strcat(path,dot);
  printf(">>writePngFileBase64>>base64 decode string size is:%d >>origin buff size is:%d\n",lenBase64,lenBuf);
  printf(">>writePngFileBase64>>new path is:%s>>base64 decode buf new size is:%d \n",path,lenDec);
  writeImageFile(convert,lenDec,path);
 
}
Exemple #7
0
void testSinglePixelFile(std::vector<std::string> channels, std::string filename) {
	deep::DeepImage * img = new deep::DeepImage(1, 1, channels);
	img->addSample(0, 0, {1.f, 1.f, 0.f, 0.5f, 1.f, 1.f});
	img->addSample(0, 0, {0.f, 1.f, 1.f, 0.5f, 4.f, 4.f});
//	img->addSample(0, 0, {1.f, 1.f, 0.f, 1.f, 5.f, 5.f});
	deep::Image * flatImg = deep::renderDeepImage(*img);
	writeImageFile(filename, 1, 1, flatImg->channels(), flatImg->data(0,0,0));
	int c = 0;
	for (auto channelName : flatImg->channelNames()) {
		std::cout << channelName << " : " << *flatImg->data(0,0,c) << std::endl;
		c++;
	}
	delete img;
	delete flatImg;
}
Exemple #8
0
void testDeepAddition(std::string deepFilename1, std::string deepFilename2, std::string filenameComb) {
	deep::DeepImageReader reader(deepFilename1);
	deep::DeepImage * d1 = reader.read();
	//		printDeepImageStats(*d1);

	deep::DeepImageReader reader2(deepFilename2);
	deep::DeepImage * d2 = reader2.read();
	//	printDeepImageStats(*d2);

	d1->addDeepImage(*d2);
	//		printDeepImageStats(*d2);

	deep::Image * combImg = deep::renderDeepImage(*d1);
	writeImageFile(filenameComb, combImg->width(), combImg->height(), 4, combImg->data(0,0,0));
	delete d1;
	delete d2;
	delete combImg;
}
Exemple #9
0
void testDeepSubtraction2(std::vector<std::string> channels, std::string filenameSub) {
	int x = 640;
	int y = 480;
	deep::DeepImage * img1 = new deep::DeepImage(x, y, channels);
	// Red background.
	drawDeepRect(0, 0, x, y, {1.f, 0.f, 0.f, 1.f, 10.f, 15.f}, img1);

	deep::DeepImage * img2 = new deep::DeepImage(x, y, channels);
	// Green circle.
	drawDeepCircle(400, 300, 100, {0.f, 1.f, 0.f, 0.5f, 5.f, 5.f}, img2);

	img1->subtractDeepImage(*img2);

	deep::Image * subImg = deep::renderDeepImage(*img1);
	writeImageFile(filenameSub, subImg->width(), subImg->height(), 4, subImg->data(0,0,0));

	delete img1;
	delete img2;
}
Exemple #10
0
void testSubAdd(std::string filename1, std::string filename2) {
	deep::DeepImageReader reader(filename1);
	deep::DeepImage * d1 = reader.read();

	deep::DeepImageReader reader2(filename2);
	deep::DeepImage * d2 = reader2.read();

	d1->subtractDeepImage(*d2);
	deep::Image * subImg = deep::renderDeepImage(*d1);
	delete d1;
	d1 = reader.read();

	d2->subtractDeepImage(*d1);
	deep::Image * subImg2 = deep::renderDeepImage(*d2);

	for (int y = 0; y < subImg->height(); ++y) {
		for (int x = 0; x < subImg->width(); ++x) {
			float a1 = *subImg->data(y, x, 3);
			float a2 = *subImg2->data(y, x, 3);
			float a = std::max(std::min(a1 + a2, 1.f), 0.f);
			for (int c = 0; c < subImg->channels()-1; ++c) {
				float d1 = *subImg->data(y, x, c);
				float d2 = *subImg2->data(y, x, c);
				if (d2 < 0.0) {
					std::cerr << "Less than 0 value at (" << x << "," << y << ")[" << c << "]=" << d2 << std::endl;
					throw std::exception();
				} else {
					*subImg->data(y, x, c) = (a1*d1+a2*d2)/a;
				}
			}
			*subImg->data(y, x, 3) = a;
		}
	}

	std::cout << "Info about " << "deepsubcomb.png" << std::endl;
	printFlatImageStats(*subImg);
	writeImageFile("deepsubcomb.png", subImg->width(), subImg->height(), 4, subImg->data(0,0,0));
	delete d2;
	delete d1;
	delete subImg2;
	delete subImg;
}
Exemple #11
0
void testDeepSubtraction(std::string deepFilename1, std::string deepFilename2, std::string filenameSub) {
	deep::DeepImageReader reader(deepFilename1);
	deep::DeepImage * d1 = reader.read();
//		printDeepImageStats(*d1);

	deep::DeepImageReader reader2(deepFilename2);
	deep::DeepImage * d2 = reader2.read();
//	printDeepImageStats(*d2);

	d1->subtractDeepImage(*d2);
//		printDeepImageStats(*d2);

	deep::Image * subImg = deep::renderDeepImage(*d1);
	std::cout << "Info about " << filenameSub << std::endl;
	deep::printFlatImageStats(*subImg);
	writeImageFile(filenameSub, subImg->width(), subImg->height(), 4, subImg->data(0,0,0));
	delete d1;
	delete d2;
	delete subImg;
}
Exemple #12
0
void testDeepFile2(std::vector<std::string> channels, std::string filename, std::string deepFilename) {
	int x = 640;
	int y = 480;
	deep::DeepImage * img = new deep::DeepImage(x, y, channels);

	drawDeepCircle(300, 200, 100, {1.f, 0.f, 0.f, 0.5f, 5.f, 7.f}, img);

	std::cout << "Info about " << deepFilename << std::endl;
	printDeepImageStats(*img);

	deep::DeepImageWriter writer(deepFilename, *img);
	writer.open();
	writer.write();
	writer.close();

	deep::Image * flatImg = deep::renderDeepImage(*img);
	delete img;
	writeImageFile(filename, x, y, 4, flatImg->data(0,0,0));
	delete flatImg;
}
int runFilter(int argc, char *argv[])
{
 char * filenameInput=argv[1];
 char * filenameOutput=argv[2];
 unsigned int inputType = guessFilenameTypeStupid(filenameInput);
 struct Image * inputImage = readImage(filenameInput,inputType,0);
 struct Image * outputImage = 0; //This will get allocated when and if needed

 if (inputImage!=0)
 {
    unsigned int outputType = guessFilenameTypeStupid(filenameOutput);
    unsigned int i=0;
      for (i=0; i<argc; i++)
      {

        if ( strcmp(argv[i],"--learn")==0 )
        {
          destroyImage(inputImage);
          learnImage(filenameInput,atoi(argv[i+1]),atoi(argv[i+2]));
          exit(0);
        } else
        if ( strcmp(argv[i],"--rgbcube")==0 )
        {
          unsigned int dim=32;
          unsigned char R = (char) atoi(argv[i]+1);
          unsigned char G = (char) atoi(argv[i]+2);
          unsigned char B = (char) atoi(argv[i]+3);

          outputImage = createImage( dim , dim , 3 , 8 );


           bitbltColorRGB(outputImage->pixels ,   0  ,  0 , dim , dim ,  R ,  G ,  B , dim-1 , dim-1);

           writeImageFile(outputImage,PPM_CODEC ,"new_mX.pnm");
           writeImageFile(outputImage,PPM_CODEC ,"new_pX.pnm");
           writeImageFile(outputImage,PPM_CODEC ,"new_mY.pnm");
           writeImageFile(outputImage,PPM_CODEC ,"new_pY.pnm");
           writeImageFile(outputImage,PPM_CODEC ,"new_mZ.pnm");
           writeImageFile(outputImage,PPM_CODEC ,"new_pZ.pnm");
          destroyImage(outputImage);

        } else
        if ( strcmp(argv[i],"--envcube")==0 )
        {
          fprintf(stdout,"Converting Environment Cube \n");
          unsigned int outputType = guessFilenameTypeStupid(filenameOutput);
          //outputImage = createSameDimensionsImage(inputImage);

          unsigned int outputWidth = inputImage->width;
          unsigned int outputHeight = (unsigned int ) (3*inputImage->width)/4;
          outputImage = createImage( outputWidth , outputHeight , 3 , 8 );

         createCubeMapFace(  outputImage->pixels ,  outputImage->width , outputImage->height , outputImage->channels , outputImage->bitsperpixel ,
                             inputImage->pixels ,  inputImage->width , inputImage->height , inputImage->channels , inputImage->bitsperpixel
                          );


         struct Image * partImg=0;
         unsigned int outX=0 , outY=0 , outWidth=0 , outHeight=0;
         getCubeMap2DCoords(outputWidth,outputHeight, /*x*/ -1 , /*y*/  0  , /*z*/  0 , &outX , &outY , &outWidth , &outHeight );
         partImg=createImageBitBlt( outputImage , outX , outY , outWidth , outHeight );
         writeImageFile(partImg,PPM_CODEC ,"new_mX.pnm"); destroyImage(partImg);

         getCubeMap2DCoords(outputWidth,outputHeight, /*x*/  1 , /*y*/  0  , /*z*/  0 , &outX , &outY , &outWidth , &outHeight );
         partImg=createImageBitBlt( outputImage , outX , outY , outWidth , outHeight );
         writeImageFile(partImg,PPM_CODEC ,"new_pX.pnm"); destroyImage(partImg);

         getCubeMap2DCoords(outputWidth,outputHeight, /*x*/  0 , /*y*/ -1  , /*z*/  0 , &outX , &outY , &outWidth , &outHeight );
         partImg=createImageBitBlt( outputImage , outX , outY , outWidth , outHeight );
         writeImageFile(partImg,PPM_CODEC ,"new_mY.pnm"); destroyImage(partImg);

         getCubeMap2DCoords(outputWidth,outputHeight, /*x*/  0 , /*y*/  1  , /*z*/  0 , &outX , &outY , &outWidth , &outHeight );
         partImg=createImageBitBlt( outputImage , outX , outY , outWidth , outHeight );
         writeImageFile(partImg,PPM_CODEC ,"new_pY.pnm"); destroyImage(partImg);

         getCubeMap2DCoords(outputWidth,outputHeight, /*x*/  0 , /*y*/  0  , /*z*/ -1 , &outX , &outY , &outWidth , &outHeight );
         partImg=createImageBitBlt( outputImage , outX , outY , outWidth , outHeight );
         writeImageFile(partImg,PPM_CODEC ,"new_mZ.pnm"); destroyImage(partImg);

         getCubeMap2DCoords(outputWidth,outputHeight, /*x*/  0 , /*y*/  0  , /*z*/  1 , &outX , &outY , &outWidth , &outHeight );
         partImg=createImageBitBlt( outputImage , outX , outY , outWidth , outHeight );
         writeImageFile(partImg,PPM_CODEC ,"new_pZ.pnm"); destroyImage(partImg);
        } else
        if ( strcmp(argv[i],"--compare")==0 )
        {
          unsigned int outputType = guessFilenameTypeStupid(filenameOutput);
          outputImage = readImage(filenameOutput,outputType ,0);

          float noise = calculatePSNR( outputImage->pixels ,  outputImage->width , outputImage->height , outputImage->channels ,
                                       inputImage->pixels ,  inputImage->width , inputImage->height , inputImage->channels );

           fprintf(stdout,"Compared Detected Noise is %0.4f dB \n",noise);
           exit(0);
        } else
        if ( strcmp(argv[i],"--gaussian")==0 )
        {
          monochrome(inputImage);
          outputImage = createSameDimensionsImage(inputImage);

         unsigned int normalizeGaussianKernel=1;
         unsigned int kernelWidth=5;
         unsigned int kernelHeight=5;
         float * convolutionMatrix=allocateGaussianKernel(kernelWidth,kernelHeight,normalizeGaussianKernel);
         float divisor=1.0;

         float * inF = copyUCharImage2Float(inputImage->pixels ,  inputImage->width , inputImage->height , inputImage->channels );
         float * outF = (float*) malloc(sizeof(float) *  outputImage->width * outputImage->height *  outputImage->channels );


         convolutionFilter1ChF(
                                 outF ,  outputImage->width , outputImage->height ,
                                 inF,  inputImage->width , inputImage->height ,
                                 convolutionMatrix , kernelWidth , kernelHeight , &divisor
                              );


         free(convolutionMatrix);

         castFloatImage2UChar(outputImage->pixels, outF, outputImage->width , outputImage->height ,  outputImage->channels );
         free(inF);
         free(outF);
        } else
        if ( strcmp(argv[i],"--ctbilateral")==0 )
        {
          monochrome(inputImage);
          outputImage = createSameDimensionsImage(inputImage);

          float sigma = atof(argv[i+1]);
          constantTimeBilateralFilter(
                                       inputImage->pixels  ,  inputImage->width , inputImage->height , inputImage->channels ,
                                       outputImage->pixels ,  outputImage->width , outputImage->height
                                      ,&sigma //sigma
                                      ,atoi(argv[i+2]) //bins
                                      ,atoi(argv[i+3]) //useDeriche
                                     );

        } else
        if ( strcmp(argv[i],"--deriche")==0 )
        {
          monochrome(inputImage);
          outputImage = createSameDimensionsImage(inputImage);
          float sigma = atof(argv[i+1]);
          dericheRecursiveGaussianGray( outputImage->pixels ,  outputImage->width , outputImage->height , inputImage->channels ,
                                        inputImage->pixels ,  inputImage->width , inputImage->height ,
                                        &sigma , atoi(argv[i+2])
                                       );
        } else
        if ( strcmp(argv[i],"--dericheF")==0 )
        {
          fprintf(stderr,"This is a test call for casting code , this shouldnt be normally used..\n");
          monochrome(inputImage);
          outputImage = createSameDimensionsImage(inputImage);
          float sigma = atof(argv[i+1]);

          //outputImage = copyImage(inputImage);
         float * inF = copyUCharImage2Float(inputImage->pixels ,  inputImage->width , inputImage->height , inputImage->channels );
         float * outF = (float*) malloc(sizeof(float) *  outputImage->width * outputImage->height *  outputImage->channels );

         dericheRecursiveGaussianGrayF(  outF  ,  outputImage->width , outputImage->height ,  inputImage->channels ,
                                         inF ,  inputImage->width , inputImage->height  ,
                                         &sigma , atoi(argv[i+2])
                                        );

         castFloatImage2UChar(outputImage->pixels, outF, outputImage->width , outputImage->height ,  outputImage->channels );
         free(inF);
         free(outF);
        } else
        if ( strcmp(argv[i],"--median")==0 )
        {
           outputImage = copyImage(inputImage);
           medianFilter3ch(
                         outputImage->pixels ,  outputImage->width , outputImage->height ,
                         inputImage->pixels ,  inputImage->width , inputImage->height  ,
                         atoi(argv[i+1]) , atoi(argv[i+2])
                        );
        } else
        if ( strcmp(argv[i],"--meansat")==0 )
        {
           outputImage = copyImage(inputImage);
           meanFilterSAT(
                         outputImage->pixels ,  outputImage->width , outputImage->height , outputImage->channels ,
                         inputImage->pixels ,  inputImage->width , inputImage->height , inputImage->channels ,
                         atoi(argv[i+1]) , atoi(argv[i+2])
                        );
        } else
        if ( strcmp(argv[i],"--monochrome")==0 )
        {
          outputImage = copyImage(inputImage);
          monochrome(outputImage);
        } else
        if ( strcmp(argv[i],"--bilateral")==0 )
        {
          outputImage = copyImage(inputImage);
          bilateralFilter( outputImage->pixels ,  outputImage->width , outputImage->height ,
                           inputImage->pixels ,  inputImage->width , inputImage->height ,
                            atof(argv[i+1]) , atof(argv[i+2]) , atoi(argv[i+3])
                         );
        } else
        if ( strcmp(argv[i],"--contrast")==0 )
        {
          outputImage = copyImage(inputImage);
          contrast(outputImage,atof(argv[i+1]));
        } else
        if ( strcmp(argv[i],"--sattest")==0 )
        {
            float * tmp = allocateGaussianKernel(3,5.0,1);
            if (tmp!=0) { free(tmp); }

            tmp = allocateGaussianKernel(9,5.0,1);
            if (tmp!=0) { free(tmp); }


            tmp = allocateGaussianKernel(15,5.0,1);
            if (tmp!=0) { free(tmp); }


            summedAreaTableTest();
            unsigned int * integralImageOutput = 0;
            integralImageOutput = generateSummedAreaTableRGB(inputImage->pixels ,  inputImage->width , inputImage->height);
            if (integralImageOutput!=0)
            {
              free(integralImageOutput);
              fprintf(stderr,"integralImage test was successful\n");
            }
        }
      }

    writeImageFile(outputImage,outputType ,filenameOutput);
    destroyImage(outputImage);
    destroyImage(inputImage);
    return 1;
 }
 return 0;
}