Alien-FreeImage

 view release on metacpan or  search on metacpan

src/Source/FreeImage/PluginRAS.cpp  view on Meta::CPAN

	}

	BOOL header_only = (flags & FIF_LOAD_NOPIXELS) == FIF_LOAD_NOPIXELS;

	try {
		// Read SUN raster header

		io->read_proc(&header, sizeof(SUNHEADER), 1, handle);

#ifndef FREEIMAGE_BIGENDIAN
		// SUN rasterfiles are big endian only

		SwapLong(&header.magic);
		SwapLong(&header.width);
		SwapLong(&header.height);
		SwapLong(&header.depth);
		SwapLong(&header.length);
		SwapLong(&header.type);
		SwapLong(&header.maptype);
		SwapLong(&header.maplength);
#endif

		// Verify SUN identifier

		if (header.magic != RAS_MAGIC) {
			throw FI_MSG_ERROR_MAGIC_NUMBER;
		}

		// Allocate a new DIB

		switch(header.depth) {
			case 1:
			case 8:
				dib = FreeImage_AllocateHeader(header_only, header.width, header.height, header.depth);
				break;

			case 24:
				dib = FreeImage_AllocateHeader(header_only, header.width, header.height, header.depth, FI_RGBA_RED_MASK, FI_RGBA_GREEN_MASK, FI_RGBA_BLUE_MASK);
				break;

			case 32:
				dib = FreeImage_AllocateHeader(header_only, header.width, header.height, header.depth, FI_RGBA_RED_MASK, FI_RGBA_GREEN_MASK, FI_RGBA_BLUE_MASK);
				break;
		}

		if (dib == NULL) {
			throw FI_MSG_ERROR_DIB_MEMORY;
		}

		// Check the file format

		rle = FALSE;
		isRGB = FALSE;

		switch(header.type) {
			case RT_OLD:
			case RT_STANDARD:
			case RT_FORMAT_TIFF: // I don't even know what these format are...
			case RT_FORMAT_IFF: //The TIFF and IFF format types indicate that the raster
				//file was originally converted from either of these file formats.
				//so lets at least try to process them as RT_STANDARD
				break;

			case RT_BYTE_ENCODED:
				rle = TRUE;
				break;

			case RT_FORMAT_RGB:
				isRGB = TRUE;
				break;

			default:
				throw FI_MSG_ERROR_UNSUPPORTED_FORMAT;
		}

		// set up the colormap if needed

		switch(header.maptype) {
			case RMT_NONE :
			{				
				if (header.depth < 24) {
					// Create linear color ramp

					RGBQUAD *pal = FreeImage_GetPalette(dib);

					int numcolors = 1 << header.depth;

					for (int i = 0; i < numcolors; i++) {
						pal[i].rgbRed	= (BYTE)((255 * i) / (numcolors - 1));
						pal[i].rgbGreen = (BYTE)((255 * i) / (numcolors - 1));
						pal[i].rgbBlue	= (BYTE)((255 * i) / (numcolors - 1));
					}
				}

				break;
			}

			case RMT_EQUAL_RGB:
			{
				BYTE *r, *g, *b;

				// Read SUN raster colormap

				int numcolors = 1 << header.depth;
				if((DWORD)(3 * numcolors) > header.maplength) {
					// some RAS may have less colors than the full palette
					numcolors = header.maplength / 3;
				} else {
					throw "Invalid palette";
				}

				r = (BYTE*)malloc(3 * numcolors * sizeof(BYTE));
				g = r + numcolors;
				b = g + numcolors;

				RGBQUAD *pal = FreeImage_GetPalette(dib);

				io->read_proc(r, 3 * numcolors, 1, handle);

				for (int i = 0; i < numcolors; i++) {
					pal[i].rgbRed	= r[i];



( run in 0.685 second using v1.01-cache-2.11-cpan-ceb78f64989 )