/* --------------------------------------------------------------------------- * Return a pointer to a given pixel. Accounts for image orientation (T_TO_B, * R_TO_L, etc). Returns NULL if the pixel is out of range. */ uint8_t *tga_find_pixel(const tga_image *img, uint16_t x, uint16_t y) { if (x >= img->width || y >= img->height) return NULL; if (!tga_is_top_to_bottom(img)) y = img->height - 1 - y; if (tga_is_right_to_left(img)) x = img->width - 1 - x; return img->image_data + (x + y * img->width) * img->pixel_depth/8; }
void load_tga(tga_image *tga, const char *fn) { DONTFAIL( tga_read(tga, fn) ); printf("Loaded %dx%dx%dbpp targa (\"%s\").\n", tga->width, tga->height, tga->pixel_depth, fn); if (!tga_is_mono(tga)) DONTFAIL( tga_desaturate_rec_601_1(tga) ); if (!tga_is_top_to_bottom(tga)) DONTFAIL( tga_flip_vert(tga) ); if (tga_is_right_to_left(tga)) DONTFAIL( tga_flip_horiz(tga) ); if ((tga->width % 8 != 0) || (tga->height % 8 != 0)) { printf("Width and height must be multiples of 8\n"); exit(EXIT_FAILURE); } }
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; }
/* --------------------------------------------------------------------------- * Horizontally flip the image in place. Reverses the right-to-left bit in * the image descriptor. */ tga_result tga_flip_horiz(tga_image *img) { uint16_t row; size_t bpp; uint8_t *left, *right; int r_to_l; if (!SANE_DEPTH(img->pixel_depth)) return TGAERR_PIXEL_DEPTH; bpp = (size_t)(img->pixel_depth / 8); /* bytes per pixel */ for (row=0; row<img->height; row++) { left = img->image_data + row * img->width * bpp; right = left + (img->width - 1) * bpp; /* reverse from left to right */ while (left < right) { uint8_t buffer[4]; /* swap */ memcpy(buffer, left, bpp); memcpy(left, right, bpp); memcpy(right, buffer, bpp); left += bpp; right -= bpp; } } /* Correct image_descriptor's left-to-right-ness. */ r_to_l = tga_is_right_to_left(img); img->image_descriptor &= ~TGA_R_TO_L_BIT; /* mask out r-to-l bit */ if (!r_to_l) /* was l-to-r, need to set r_to_l */ img->image_descriptor |= TGA_R_TO_L_BIT; /* else bit is already rubbed out */ return TGA_NOERR; }