Beispiel #1
0
static RGBSpectrum *ReadImageTGA(const std::string &name, int *width,
                                 int *height) {
    tga_image img;
    tga_result result;
    if ((result = tga_read(&img, name.c_str())) != TGA_NOERR) {
        Error("Unable to read from TGA file \"%s\" (%s)", name.c_str(),
              tga_error(result));
        return nullptr;
    }

    if (tga_is_right_to_left(&img)) tga_flip_horiz(&img);
    if (!tga_is_top_to_bottom(&img)) tga_flip_vert(&img);
    if (tga_is_colormapped(&img)) tga_color_unmap(&img);

    *width = img.width;
    *height = img.height;

    // "Unpack" the pixels (origin in the lower left corner).
    // TGA pixels are in BGRA format.
    RGBSpectrum *ret = new RGBSpectrum[*width * *height];
    RGBSpectrum *dst = ret;
    for (int y = *height - 1; y >= 0; y--)
        for (int x = 0; x < *width; x++) {
            uint8_t *src = tga_find_pixel(&img, x, y);
            if (tga_is_mono(&img))
                *dst++ = RGBSpectrum(*src / 255.f);
            else {
                Float c[3];
                c[2] = src[0] / 255.f;
                c[1] = src[1] / 255.f;
                c[0] = src[2] / 255.f;
                *dst++ = RGBSpectrum::FromRGB(c);
            }
        }

    tga_free_buffers(&img);
    Info("Read TGA image %s (%d x %d)", name.c_str(), *width, *height);

    return ret;
}
Beispiel #2
0
// TGA Function Definitions
void WriteImageTGA(const std::string &name, const uint8_t *pixels, int xRes,
                   int yRes, int totalXRes, int totalYRes, int xOffset,
                   int yOffset) {
    // Reformat to BGR layout.
    std::unique_ptr<uint8_t> outBuf(new uint8_t[3 * xRes * yRes]);
    uint8_t *dst = outBuf.get();
    const uint8_t *src = pixels;
    for (int y = 0; y < yRes; ++y) {
        for (int x = 0; x < xRes; ++x) {
            dst[0] = src[2];
            dst[1] = src[1];
            dst[2] = src[0];
            dst += 3;
            src += 3;
        }
    }

    tga_result result;
    if ((result = tga_write_bgr(name.c_str(), outBuf.get(), xRes, yRes, 24)) !=
        TGA_NOERR)
        Error("Unable to write output file \"%s\" (%s)", name.c_str(),
              tga_error(result));
}
Beispiel #3
0
static int write_image_tga(void * priv, gavl_video_frame_t * frame)
  {
  tga_t * tga = priv;
  gavl_video_frame_t * tmp_frame;
  int result, ret = 1;

  errno = 0;

  if(tga->format.pixelformat == GAVL_RGBA_32)
    {
    tmp_frame = gavl_video_frame_create(&tga->format);
    gavl_video_frame_copy(&tga->format, tmp_frame, frame);
    if(tga->rle)
      {
      result = tga_write_rgb(tga->filename, tmp_frame->planes[0],
                             tga->format.image_width,
                             tga->format.image_height, 32,
                             frame->strides[0]);
      }
    else
      {
      result =tga_write_rgb_rle(tga->filename, tmp_frame->planes[0],
                                tga->format.image_width,
                                tga->format.image_height, 32,
                                frame->strides[0]);
      }
    gavl_video_frame_destroy(tmp_frame);
    }
  else
    {
    if(tga->rle)
      {
      result = tga_write_bgr(tga->filename, frame->planes[0],
                             tga->format.image_width,
                             tga->format.image_height, 24,
                             frame->strides[0]);
      }
    else
      {
      result = tga_write_bgr_rle(tga->filename, frame->planes[0],
                                 tga->format.image_width,
                                 tga->format.image_height, 24,
                                 frame->strides[0]);
      }
    }

  if(result != TGA_NOERR)
    {
    if(errno)
      bg_log(BG_LOG_ERROR, LOG_DOMAIN, "Cannot save %s: %s",
             tga->filename, strerror(errno));
    else
      bg_log(BG_LOG_ERROR, LOG_DOMAIN, "Cannot save %s: %s",
             tga->filename, tga_error(result));
    ret = 0;
    }

  free(tga->filename);
  tga->filename = NULL;
  
  return ret;
  }