Ejemplo n.º 1
0
void
readRgba (const char fileName[],
          Array2D<Rgba> &pixels,
          int &width,
          int &height)
{
    //
    // Read an RGBA image using class RgbaInputFile:
    //
    //	- open the file
    //	- allocate memory for the pixels
    //	- describe the memory layout of the pixels
    //	- read the pixels from the file
    //
    
    RgbaInputFile file (fileName);
    Box2i dw = file.dataWindow();
    
    width  = dw.max.x - dw.min.x + 1;
    height = dw.max.y - dw.min.y + 1;
    pixels.resizeErase (height, width);
    
    file.setFrameBuffer (&pixels[0][0] - dw.min.x - dw.min.y * width, 1, width);
    file.readPixels (dw.min.y, dw.max.y);
}
Ejemplo n.º 2
0
void
readRgba2 (const char fileName[])
{
    //
    // Read an RGBA image using class RgbaInputFile.
    // Read the pixels, 10 scan lines at a time, and
    // store the pixel data in a buffer that is just
    // large enough to hold 10 scan lines worth of data.
    //
    //	- open the file
    //	- allocate memory for the pixels
    //	- for each block of 10 scan lines,
    //	  describe the memory layout of the pixels,
    //	  read the pixels from the file,
    //	  process the pixels and discard them
    //

    RgbaInputFile file (fileName);
    Box2i dw = file.dataWindow();

    int width  = dw.max.x - dw.min.x + 1;
    int height = dw.max.y - dw.min.y + 1;
    Array2D<Rgba> pixels (10, width);

    while (dw.min.y <= dw.max.y)
    {
        file.setFrameBuffer (&pixels[0][0] - dw.min.x - dw.min.y * width,
                             1, width);

        file.readPixels (dw.min.y, min (dw.min.y + 9, dw.max.y));
        // processPixels (pixels)

        dw.min.y += 10;
    }
}
Ejemplo n.º 3
0
void Exr::readRgba(const string inf, Array2D<Rgba> &pix, int &w, int &h)
{
    RgbaInputFile file (inf.c_str());
    Box2i dw = file.dataWindow();
    w  = dw.max.x - dw.min.x + 1;
    h = dw.max.y - dw.min.y + 1;
    pix.resizeErase (h, w);
    file.setFrameBuffer (&pix[0][0] - dw.min.x - dw.min.y * w, 1, w);
    file.readPixels (dw.min.y, dw.max.y);
}
Ejemplo n.º 4
0
void
readRgba1 (const char fileName[],
    Array2D<Rgba> &pixels,
    int &width,
    int &height)
// from openexr-1.7.0/doc/ReadingAndWritingImageFiles.pdf page 6
{
  RgbaInputFile file (fileName);
  Box2i dw = file.dataWindow();
  width = dw.max.x - dw.min.x + 1;
  height = dw.max.y - dw.min.y + 1;
  pixels.resizeErase (height, width);
  file.setFrameBuffer (&pixels[0][0] - dw.min.x - dw.min.y * width, 1, width);
  file.readPixels (dw.min.y, dw.max.y);
}
Ejemplo n.º 5
0
bool ImageIO::readImage(const QString& filePath,
                        Array<Rgba>& pixels,
                        int& width,
                        int &height)
{
    RgbaInputFile* in;
    try
    {
        QString remapped = remapFilePath(filePath);
        in = new RgbaInputFile(remapped.toStdString().c_str());
    }
    catch (const std::exception &e)
    {
        qWarning() << e.what();
        return false;
    }

    Header header = in->header();

    Box2i &dataWindow = header.dataWindow();
    int dw = dataWindow.max.x - dataWindow.min.x + 1;
    int dh = dataWindow.max.y - dataWindow.min.y + 1;
    int dx = dataWindow.min.x;
    int dy = dataWindow.min.y;

    pixels.resizeErase(dw * dh);
    in->setFrameBuffer(pixels - dx - dy * dw, 1, dw);

    try
    {
        in->readPixels (dataWindow.min.y, dataWindow.max.y);
    }
    catch (const std::exception &e)
    {
        delete in;
        qWarning() << e.what();
        return false;
    }

    width = dw;
    height = dh;

    delete in;
    return true;
}
Ejemplo n.º 6
0
KDE_EXPORT void kimgio_exr_read( TQImageIO *io )
{
    try
    {
		int width, height;

		// This won't work if io is not TQFile !
		RgbaInputFile file (TQFile::encodeName(io->fileName()));
		Imath::Box2i dw = file.dataWindow();

        width  = dw.max.x - dw.min.x + 1;
        height = dw.max.y - dw.min.y + 1;

		Array2D<Rgba> pixels;
		pixels.resizeErase (height, width);

        file.setFrameBuffer (&pixels[0][0] - dw.min.x - dw.min.y * width, 1, width);
        file.readPixels (dw.min.y, dw.max.y);

		TQImage image(width, height, 32, 0, TQImage::BigEndian);
		if( image.isNull())
			return;

		// somehow copy pixels into image
		for ( int y=0; y < height; y++ ) {
			for ( int x=0; x < width; x++ ) {
				// copy pixels(x,y) into image(x,y)
				image.setPixel( x, y, RgbaToQrgba( pixels[y][x] ) );
			}
		}

		io->setImage( image );
		io->setStatus( 0 );
    }
    catch (const std::exception &exc)
    {
		kdDebug(399) << exc.what() << endl;
        return;
    }
}
Ejemplo n.º 7
0
// EXR Function Definitions
static RGBSpectrum *ReadImageEXR(const string &name, int *width, int *height) {
    try {
    RgbaInputFile file (name.c_str());
    Box2i dw = file.dataWindow();
    *width = dw.max.x - dw.min.x + 1;
    *height = dw.max.y - dw.min.y + 1;
    std::vector<Rgba> pixels(*width * *height);
    file.setFrameBuffer(&pixels[0] - dw.min.x - dw.min.y * *width, 1, *width);
    file.readPixels(dw.min.y, dw.max.y);

    RGBSpectrum *ret = new RGBSpectrum[*width * *height];
    for (int i = 0; i < *width * *height; ++i) {
        float frgb[3] = { pixels[i].r, pixels[i].g, pixels[i].b };
        ret[i] = RGBSpectrum::FromRGB(frgb);
    }
    Info("Read EXR image %s (%d x %d)", name.c_str(), *width, *height);
    return ret;
    } catch (const std::exception &e) {
        Error("Unable to read image file \"%s\": %s", name.c_str(),
            e.what());
        return NULL;
    }
}
Ejemplo n.º 8
0
void
exrCtlExr (const char inFileName[],
	   const char outFileName[],
	   const std::vector<std::string> &transformNames,
	   const AttrMap &extraAttrs,
	   int numThreads,
	   bool verbose)
{
    setGlobalThreadCount (numThreads);

    //
    // Read the input file
    //

    if (verbose)
	cout << "reading file " << inFileName << endl;

    RgbaInputFile in (inFileName);
    const Box2i &dw = in.dataWindow();
    int w = dw.max.x - dw.min.x + 1;
    int h = dw.max.y - dw.min.y + 1;

    Array2D<Rgba> inPixels (h, w);

    in.setFrameBuffer (&inPixels[0][0] - dw.min.x - dw.min.y * w, 1, w);
    in.readPixels (dw.min.y, dw.max.y);

    //
    // Apply the CTL transforms to the R, G and B channels of the input file
    //

    if (verbose)
    {
	cout << "applying CTL transforms:";

	for (int i = 0; i < transformNames.size(); ++i)
	    cout << " " << transformNames[i];

	cout << endl;
    }

    Header outHeader = in.header();
    Array2D<Rgba> outPixels (h, w);

    applyCtlExrToExr (in.header(), outHeader,
		      inPixels, outPixels,
		      w, h,
		      transformNames,
		      extraAttrs);

    //
    // Just in case one of the CTL transforms decided to mess
    // with the data window in the output header, avoid a crash
    // by resetting the data window to its original value.
    //

    outHeader.dataWindow() = in.header().dataWindow();

    //
    // If the input pixels have an A channel, copy it into
    // the output pixels.
    //

    if (in.channels() & WRITE_A)
    {
	const Rgba *inPtr = &inPixels[0][0];
	Rgba *outPtr = &outPixels[0][0];
	size_t numPixels = w * h;

	for (size_t i = 0; i < numPixels; ++i)
	    (outPtr++)->a = (inPtr++)->a;
    }

    //
    // Write the output file
    //

    if (verbose)
	cout << "writing file " << outFileName << endl;

    RgbaOutputFile out (outFileName, outHeader, in.channels());
    out.setFrameBuffer (&outPixels[0][0] - dw.min.x - dw.min.y * w, 1, w);
    out.writePixels (h);
}
Ejemplo n.º 9
0
static GstFlowReturn
gst_openexr_dec_handle_frame (GstVideoDecoder * decoder,
    GstVideoCodecFrame * frame)
{
  GstOpenEXRDec *self = GST_OPENEXR_DEC (decoder);
  GstFlowReturn ret = GST_FLOW_OK;
  gint64 deadline;
  GstMapInfo map;
  GstVideoFrame vframe;

  GST_DEBUG_OBJECT (self, "Handling frame");

  deadline = gst_video_decoder_get_max_decode_time (decoder, frame);
  if (deadline < 0) {
    GST_LOG_OBJECT (self, "Dropping too late frame: deadline %" G_GINT64_FORMAT,
        deadline);
    ret = gst_video_decoder_drop_frame (decoder, frame);
    return ret;
  }

  if (!gst_buffer_map (frame->input_buffer, &map, GST_MAP_READ)) {
    gst_video_codec_frame_unref (frame);

    GST_ELEMENT_ERROR (self, CORE, FAILED,
        ("Failed to map input buffer"), (NULL));
    return GST_FLOW_ERROR;
  }

  /* Now read the file and catch any exceptions */
  MemIStream *istr;
  RgbaInputFile *file;
  try {
    istr =
        new
        MemIStream (gst_pad_get_stream_id (GST_VIDEO_DECODER_SINK_PAD
            (decoder)), map.data, map.size);
  }
  catch (Iex::BaseExc e) {
    gst_video_codec_frame_unref (frame);

    GST_ELEMENT_ERROR (self, CORE, FAILED,
        ("Failed to create input stream"), (NULL));
    return GST_FLOW_ERROR;
  }
  try {
    file = new RgbaInputFile (*istr);
  }
  catch (Iex::BaseExc e) {
    delete istr;
    gst_video_codec_frame_unref (frame);

    GST_ELEMENT_ERROR (self, CORE, FAILED,
        ("Failed to read OpenEXR stream"), (NULL));
    return GST_FLOW_ERROR;
  }

  ret = gst_openexr_dec_negotiate (self, file);
  if (ret != GST_FLOW_OK) {
    delete file;
    delete istr;
    gst_buffer_unmap (frame->input_buffer, &map);
    gst_video_codec_frame_unref (frame);

    GST_ELEMENT_ERROR (self, CORE, NEGOTIATION,
        ("Failed to negotiate"), (NULL));
    return ret;
  }

  ret = gst_video_decoder_allocate_output_frame (decoder, frame);
  if (ret != GST_FLOW_OK) {
    delete file;
    delete istr;
    gst_buffer_unmap (frame->input_buffer, &map);
    gst_video_codec_frame_unref (frame);

    GST_ELEMENT_ERROR (self, CORE, FAILED,
        ("Failed to allocate output buffer"), (NULL));
    return ret;
  }

  if (!gst_video_frame_map (&vframe, &self->output_state->info,
          frame->output_buffer, GST_MAP_WRITE)) {
    delete file;
    delete istr;
    gst_buffer_unmap (frame->input_buffer, &map);
    gst_video_codec_frame_unref (frame);

    GST_ELEMENT_ERROR (self, CORE, FAILED,
        ("Failed to map output buffer"), (NULL));
    return GST_FLOW_ERROR;
  }

  /* Decode the file */
  Box2i dw = file->dataWindow ();
  int width = dw.max.x - dw.min.x + 1;
  int height = dw.max.y - dw.min.y + 1;
  Rgba *fb = new Rgba[width * height];

  try {
    file->setFrameBuffer (fb - dw.min.x - dw.min.y * width, 1, width);
    file->readPixels (dw.min.y, dw.max.y);
  } catch (Iex::BaseExc e) {
    delete[](fb);
    delete file;
    delete istr;
    gst_buffer_unmap (frame->input_buffer, &map);
    gst_video_frame_unmap (&vframe);

    GST_ELEMENT_ERROR (self, CORE, FAILED, ("Failed to read pixels"), (NULL));
    return GST_FLOW_ERROR;
  }

  /* And convert from ARGB64_F16 to ARGB64 */
  gint i, j;
  guint16 *dest = (guint16 *) GST_VIDEO_FRAME_PLANE_DATA (&vframe, 0);
  guint dstride = GST_VIDEO_FRAME_PLANE_STRIDE (&vframe, 0);
  Rgba *ptr = fb;

  /* TODO: Use displayWindow here and also support output of ARGB_F16
   * and add a conversion filter element that can change exposure and
   * other things */
  for (i = 0; i < height; i++) {
    for (j = 0; j < width; j++) {
      dest[4 * j + 0] = CLAMP (((float) ptr->a) * 65536, 0, 65535);
      dest[4 * j + 1] = CLAMP (((float) ptr->r) * 65536, 0, 65535);
      dest[4 * j + 2] = CLAMP (((float) ptr->g) * 65536, 0, 65535);
      dest[4 * j + 3] = CLAMP (((float) ptr->b) * 65536, 0, 65535);
      ptr++;
    }
    dest += dstride / 2;
  }

  delete[](fb);
  delete file;
  delete istr;
  gst_buffer_unmap (frame->input_buffer, &map);
  gst_video_frame_unmap (&vframe);

  ret = gst_video_decoder_finish_frame (decoder, frame);

  return ret;
}
Ejemplo n.º 10
0
void
exrToDpx (const char exrFileName[],
	  const char dpxFileName[],
	  std::vector<std::string> transformNames,
	  bool verbose)
{
    //
    // Read the OpenEXR file
    //

    if (verbose)
	cout << "reading file " << exrFileName << endl;

    RgbaInputFile in (exrFileName);
    Box2i dw = in.dataWindow();
    unsigned int width  = (unsigned int) (dw.max.x - dw.min.x + 1);
    unsigned int height = (unsigned int) (dw.max.y - dw.min.y + 1);

    Array2D<Rgba> pixels (height, width);
    in.setFrameBuffer (&pixels[0][0] - dw.min.x - dw.min.y * width, 1, width);
    in.readPixels (dw.min.y, dw.max.y);

    //
    // Apply the CTL transforms
    //

    if (verbose)
    {
	cout << "applyging CTL transforms:";

	for (int i = 0; i < transformNames.size(); ++i)
	    cout << " " << transformNames[i];

	cout << endl;
    }

    applyCtlExrToDpx (transformNames,
		      in.header(),
		      pixels,
		      width, height,
		      pixels);

    //
    // Write the DPX file
    //

    if (verbose)
	cout << "writing file " << dpxFileName << endl;

    ofstream out (dpxFileName, ios_base::binary);

    if (!out)
    {
	THROW_ERRNO ("Cannot open file " << dpxFileName << " "
		     "for writing (%T).");
    }

    writeHeader (out, dpxFileName, width, height);
    writePixels (out, dpxFileName, width, height, pixels);

    if (verbose)
	cout << "done" << endl;
}
Ejemplo n.º 11
0
int
main(int argc, char **argv)
{
short x,y,c,i;
char outfile[300], infile[300];
short num_chars;
int first, last, frame, first_out;
float s, t, tmp;
float red, grn, blu;
short argnm=0;



if (argc <= 4) {
  printf(" usage: %s infiles, outfiles, first_frame, last_frame, first_frame_out\n", argv[0]);
  exit(1);
}

 first = atoi(argv[3]);
 last  = atoi(argv[4]);
 if(argc > 5) {
   first_out = atoi(argv[5]);
 } else {
   first_out=first;
 }

 printf(" processing frames %d to %d to frames %d to %d\n", first, last, first_out, last + first_out - first);


/*******************************************************************************************************************************************************/
/* frame loop: */
   for (frame=first; frame <= last; frame++) {

     sprintf(infile, argv[1], frame);
     num_chars = strlen(infile); /* length of infile string */
     if ((!strcmp(&infile[num_chars-1], "r"))||(!strcmp(&infile[num_chars-1], "R"))) { /* EXR file ending in ".exr" */
      printf(" processing input file %s\n", infile);
     } else { /* not exr */
      printf(" unknown filetype for reading, since extension doesn't end in r, only exr reading supported, infile = %s, aborting\n", outfile);
      exit(1);
     } /* exr or not */

     sprintf(outfile, argv[2], frame + first_out - first);
     num_chars = strlen(outfile); /* length of outfile string */
       if ((!strcmp(&outfile[num_chars-1], "x"))  ||(!strcmp(&outfile[num_chars-1], "X")) || /* DPX floating point file ending in ".dpx" */
           (!strcmp(&outfile[num_chars-3], "x32"))||(!strcmp(&outfile[num_chars-3], "X32"))) { /* DPX32 */
        printf(" processing output file %s\n", outfile);
       } else { /* not exr */
        printf(" unknown filetype for writing, since extension doesn't end in x or x32, only dpx float writing supported, outfile = %s, aborting\n", outfile);
        exit(1);
       } /* exr or not */

    RgbaInputFile file (infile, 1 );

    Box2i dw = file.dataWindow();

    h_reso = dw.max.x - dw.min.x + 1;
    v_reso = dw.max.y - dw.min.y + 1;
    half_float_pixels.resizeErase (v_reso, h_reso);

    file.setFrameBuffer (&half_float_pixels[0][0] - dw.min.x - dw.min.y * h_reso, 1, h_reso);
    file.readPixels (dw.min.y, dw.max.y);

    if (pixels == NULL) { pixels = (float *) malloc(h_reso * v_reso * 12); /* 4-bytes/float * 3-colors */ }


    for(y=0; y< v_reso; y++) {
      for(x=0; x< h_reso; x++) {
        pixels[(0*v_reso + y) * h_reso + x] = half_float_pixels[y][x].r;
        pixels[(1*v_reso + y) * h_reso + x] = half_float_pixels[y][x].g;
        pixels[(2*v_reso + y) * h_reso + x] = half_float_pixels[y][x].b;
      } /* x */
    } /* y */

    dpx_write_float(outfile, pixels, h_reso, v_reso);

    printf(" finished writing %s at x_reso = %d y_reso = %d\n", outfile, h_reso, v_reso);

 } /* frame loop */

} /* main */