bool LoadTarga( CImage32* pImage, const char* pNameFile ) { if (!(pImage && pNameFile)) { return false; } tga_image tgaImage; tga_result result = tga_read( &tgaImage, pNameFile ); if (result != TGA_NOERR) { return false; } if (tga_is_colormapped( &tgaImage )) { tga_color_unmap( &tgaImage ); } UINT width = tgaImage.width; UINT height = tgaImage.height; UINT bitsPerPixel = tgaImage.pixel_depth; pImage->Resize( width, height ); for (UINT y = 0; y < height; y ++) { for (UINT x = 0; x < width; x ++) { BYTE* pPixel = tga_find_pixel( &tgaImage, x, y ); TARGB color; if (bitsPerPixel == 8) { tga_unpack_pixel( pPixel, tgaImage.pixel_depth, &color.B, NULL, NULL, NULL ); color.G = color.R = color.B; color.A = 255; } else if (bitsPerPixel == 16 || bitsPerPixel == 24) { tga_unpack_pixel( pPixel, tgaImage.pixel_depth, &color.B, &color.G, &color.R, NULL ); color.A = 255; } else { // bitsPerPixel == 32 tga_unpack_pixel( pPixel, tgaImage.pixel_depth, &color.B, &color.G, &color.R, &color.A ); } pImage->PixelSet( x, y, color.Value ); } } tga_free_buffers( &tgaImage ); return true; }
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; }
//**************************************************************************** int convert_image( const char* colladapath, dae_COLLADA* collada, dae_image_type* daeimage, taa_scene* scene, objmap* imagemap) { int32_t err = 0; dae_image_type_init_from* daeinitfrom = daeimage->el_init_from; char path[taa_PATH_SIZE]; const char* name; void* data; size_t size; data = NULL; size = 0; path[0] = '\0'; // determine name name = NULL; if(daeimage->at_name != NULL) { name = *daeimage->at_name; } if(name == NULL && daeimage->at_id != NULL) { name = *daeimage->at_id; } if(name == NULL) { name = ""; } // determine if image is external file if(daeinitfrom != NULL) { if(daeinitfrom->el_ref) { char* uri = *daeinitfrom->el_ref; if(uri != NULL) { char urischeme[16]; char uripath[taa_PATH_SIZE]; taa_uri_get_scheme(uri, urischeme, sizeof(urischeme)); if(urischeme[0] != '\0') { // valid uri taa_uri_get_path(uri, uripath, sizeof(uripath)); taa_path_set(path, sizeof(path), uripath); } else { // assume it's just a file path taa_path_set(path, sizeof(path), uri); } } } } // load external file if(*path != '\0') { FILE* fp = fopen(path, "rb"); if(fp == NULL) { // if could not find image relative to cwd, // try making it relative to document path char relpath[taa_PATH_SIZE]; taa_path_get_dir(colladapath, relpath, sizeof(relpath)); taa_path_append(relpath, sizeof(relpath), path); fp = fopen(relpath, "rb"); } if(fp == NULL) { // TODO: reattempt using local dir } if(fp != NULL) { fseek(fp, 0, SEEK_END); size = ftell(fp); fseek(fp, 0, SEEK_SET); data = malloc(size); if(fread(data, 1, size, fp) != size) { taa_LOG_WARN("error reading image file %s", path); free(data); size = 0; err = -1; } fclose(fp); } else { taa_LOG_WARN("could not open image file %s", path); err = -1; } } // process image if(data != NULL) { tga_header tga; size_t imageoff; if(err == 0) { if(size > tga_HEADER_SIZE) { tga_read(data, &tga, &imageoff); } else { taa_LOG_WARN("invalid image size %s", path); err = -1; } } if(err == 0) { if( tga.imagetype!=tga_TYPE_TRUECOLOR && tga.imagetype!=tga_TYPE_GREY) { taa_LOG_WARN("unsupported image color type for %s", path); err = -1; // only support truecolor uncompressed or grey scale } if(tga.colourmaptype != 0 || tga.colourmaplength != 0) { taa_LOG_WARN("colour maps not supported for %s", path); err = -1; // do not support colour maps } if((tga.descriptor & 0xC0) != 0) { taa_LOG_WARN("interleaved data not supported for %s", path); err = -1; // do not support interleaved data } if(imageoff + tga.width*tga.height*tga.bitsperpixel/8 > size) { taa_LOG_WARN("image buffer overflow for %s", path); err = -1; // check for buffer overflow } } if(err == 0) { taa_scenetexture* tex; taa_scenetexture_origin origin; taa_scenetexture_format format; int32_t texid; origin = taa_SCENETEXTURE_BOTTOMLEFT; if(tga_GET_VFLIP(&tga)) { origin = taa_SCENETEXTURE_TOPLEFT; } switch(tga.bitsperpixel) { case 8: format = taa_SCENETEXTURE_LUM8; break; case 24: format = taa_SCENETEXTURE_BGR8; break; case 32: format = taa_SCENETEXTURE_BGRA8; break; default: taa_LOG_WARN("invalid pixel format for image %s", path); err = -1; break; } if(err == 0) { // add the texture to the scene texid = taa_scene_add_texture( scene, name, path, origin); tex = scene->textures + texid; taa_scenetexture_resize( tex, 1, tga.width, tga.height, 1, format); memcpy( tex->images[0], ((unsigned char*) data) + imageoff, tga.width * tga.height * tga.bitsperpixel/8); // insert the image into the conversion map objmap_insert(imagemap, daeimage, texid); } } free(data); } return err; }
bool loadDepth(const char* pNameFile, ImageBase** ppImage) { if (!pNameFile || !ppImage || *ppImage) return false; tga_image tgaImage; tga_result result = tga_read(&tgaImage, pNameFile); if (result != TGA_NOERR) return false; if (tga_is_colormapped(&tgaImage)) { tga_color_unmap(&tgaImage); } unsigned int width = tgaImage.width; unsigned int height = tgaImage.height; unsigned int bitsPerPixel = tgaImage.pixel_depth; if (bitsPerPixel != 32) return false; // depth の最大値・最小値を取得 unsigned long minValueDepth = ULONG_MAX; unsigned long maxValueDepth = 0; for (unsigned int y = 0; y < tgaImage.height; y ++) { for (unsigned int x = 0; x < tgaImage.width; x ++) { unsigned char* pPixel = tga_find_pixel(&tgaImage, x, y); unsigned char valueR = 0, valueG = 0, valueB = 0, valueA = 0; tga_unpack_pixel(pPixel, tgaImage.pixel_depth, &valueB, &valueG, &valueR, &valueA); unsigned long valueDepth = ((unsigned int)(valueA) << 24) | ((unsigned int)(valueR) << 16) | ((unsigned int)(valueG) << 8) | (unsigned int)(valueB); if (valueDepth < minValueDepth) minValueDepth = valueDepth; if (maxValueDepth < valueDepth) maxValueDepth = valueDepth; } } // 最大値・最小値をもとに正規化 unsigned char maxValueElement = ElementUtil::getMaxValue<ImageGray8::TypeElement>(); unsigned char minValueElement = ElementUtil::getMinValue<ImageGray8::TypeElement>(); ImageGray8 *pImage = ImageFactory::createImage<ImageGray8>(tgaImage.width, tgaImage.height); for (unsigned int y = 0; y < tgaImage.height; y ++) { for (unsigned int x = 0; x < tgaImage.width; x ++) { unsigned char* pPixel = tga_find_pixel(&tgaImage, x, y); unsigned char valueR = 0, valueG = 0, valueB = 0, valueA = 0; tga_unpack_pixel(pPixel, tgaImage.pixel_depth, &valueB, &valueG, &valueR, &valueA); unsigned long valueDepth = ((unsigned int)(valueA) << 24) | ((unsigned int)(valueR) << 16) | ((unsigned int)(valueG) << 8) | (unsigned int)(valueB); unsigned char valueNormalized = cropValue<unsigned char>((unsigned char)(float(maxValueElement) * float(valueDepth - minValueDepth) / float(maxValueDepth - minValueDepth)), minValueElement, maxValueElement); unsigned char value = maxValueElement - valueNormalized; pImage->setPixel(x, y, ImageGray8::TypePixel(value)); } } tga_free_buffers(&tgaImage); *ppImage = static_cast<ImageBase*>(pImage); return pImage ? true : false; }
int main() { uint8_t alpha[NUM_IMAGES], r[NUM_IMAGES], g[NUM_IMAGES], b[NUM_IMAGES]; tga_image image[NUM_IMAGES]; int *pixels_addr[NUM_IMAGES]; int *pixels_addr_new; pixels_addr_new = malloc(sizeof(int)); int height[NUM_IMAGES], width[NUM_IMAGES]; int i = 0; tga_result result; char addr[256]; for(i=0;i<NUM_IMAGES;i++) { char path[256]; if(i<10) { sprintf(addr,"%s%d%s","/home/ankit/Work/Ankit/BlockColors_WithoutRLE/BlockColors_WithoutRLE_0000",i,".tga"); } else if(i>=10 && i<100) { sprintf(addr,"%s%d%s","/home/ankit/Work/Ankit/BlockColors_WithoutRLE/BlockColors_WithoutRLE_000",i,".tga"); } else if(i>=100 && i<1000) { sprintf(addr,"%s%d%s","/home/ankit/Work/Ankit/BlockColors_WithoutRLE/BlockColors_WithoutRLE_00",i,".tga"); } else if(i>=1000) { sprintf(addr,"%s%d%s","/home/ankit/Work/Ankit/BlockColors_WithoutRLE/BlockColors_WithoutRLE_0",i,".tga"); } printf("The image being opened is %s\n",addr); result = tga_read(&image[i],addr); if(result != TGA_NOERR) { printf("Error opening the image\n"); } else { pixels_addr[i] = (int *)image[i].image_data; height[i] = (int)image[i].height; width[i] = (int)image[i].width; } printf("The Height is %d\n",width[i]); } int j = 0, k = 0; tga_image img_new; int img[NUM_IMAGES]; char output[256]; for(k=0;k<NUM_IMAGES-1;k++) { for(i=0;i<height[k];i++) // for(i=0;i<18;i++) { for(j=0;j<width[k];j++) // for(j=0;j<86;j++) { img[k] = *(pixels_addr[k] + (i * width[k]) + j); img[k+1] = *(pixels_addr[k+1] + (i * width[k]) + j); *(pixels_addr[k] + (i *width[k]) + j ) = img[k] ^ img[k+1]; // printf("This is printing %d %d\n",i,j); } } // img_new.image_data = pixels_addr_new; sprintf(output,"%s%s%d%s","/home/ankit/Work/Ankit/BlockColors_WithoutRLE/","new",k+1,".tga"); result = tga_write(output,&image[k]); memset(pixels_addr[k],0,height[k]*width[k]); // tga_close(); } }