⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 tiffdirectory.java

📁 源码为Eclipse开源开发平台桌面开发工具SWT的源代码,
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
				break;			}			case TAG_Compression: {				compression = getEntryValue(type, buffer, offset);				break;			}			case TAG_PhotometricInterpretation: {				photometricInterpretation = getEntryValue(type, buffer, offset);				break;			}			case TAG_StripOffsets: {				if (type != TYPE_LONG && type != TYPE_SHORT) SWT.error(SWT.ERROR_INVALID_IMAGE);				stripOffsets = new int[count];				getEntryValue(type, buffer, offset, stripOffsets);				break;			}			case TAG_SamplesPerPixel: {				if (type != TYPE_SHORT) SWT.error(SWT.ERROR_INVALID_IMAGE);				samplesPerPixel = getEntryValue(type, buffer, offset);				/* Only the basic 1 and 3 values are supported */				if (samplesPerPixel != 1 && samplesPerPixel != 3) SWT.error(SWT.ERROR_UNSUPPORTED_DEPTH);				break;			}			case TAG_RowsPerStrip: {				rowsPerStrip = getEntryValue(type, buffer, offset);				break;			}			case TAG_StripByteCounts: {				stripByteCounts = new int[count];				getEntryValue(type, buffer, offset, stripByteCounts);				break;			}			case TAG_XResolution: {				/* Ignored */				break;			}			case TAG_YResolution: {				/* Ignored */				break;			}			case TAG_T4Options: {				if (type != TYPE_LONG) SWT.error(SWT.ERROR_INVALID_IMAGE);				t4Options = getEntryValue(type, buffer, offset);				if ((t4Options & 0x1) == 1) {					/* 2-dimensional coding is not supported */					SWT.error(SWT.ERROR_UNSUPPORTED_FORMAT);				}				break;			}			case TAG_ResolutionUnit: {				/* Ignored */				break;			}			case TAG_ColorMap: {				if (type != TYPE_SHORT) SWT.error(SWT.ERROR_INVALID_IMAGE);				/* Get the offset of the colorMap (use TYPE_LONG) */				colorMapOffset = getEntryValue(TYPE_LONG, buffer, offset);				break;			}		}	}}public ImageData read() throws IOException {	/* Set TIFF default values */	bitsPerSample = new int[] {1};	colorMapOffset = NO_VALUE;	compression = 1;	imageLength = NO_VALUE;	imageWidth = NO_VALUE;	photometricInterpretation = NO_VALUE;	rowsPerStrip = Integer.MAX_VALUE;	samplesPerPixel = 1;	stripByteCounts = null;	stripOffsets = null;		byte[] buffer = new byte[2];	file.read(buffer);	int numberEntries = toInt(buffer, 0, TYPE_SHORT);	buffer = new byte[IFD_ENTRY_SIZE * numberEntries];	file.read(buffer);	parseEntries(buffer);		PaletteData palette = null;	depth = 0;	switch (photometricInterpretation) {		case 0:		case 1: {			/* Bilevel or Grayscale image */			palette = getGrayPalette();			depth = bitsPerSample[0];			break;		}		case 2: {			/* RGB image */			if (colorMapOffset != NO_VALUE) SWT.error(SWT.ERROR_INVALID_IMAGE);			/* SamplesPerPixel 3 is the only value supported */			palette = getRGBPalette(bitsPerSample[0], bitsPerSample[1], bitsPerSample[2]);			depth = bitsPerSample[0] + bitsPerSample[1] + bitsPerSample[2];			break;				}		case 3: {			/* Palette Color image */			if (colorMapOffset == NO_VALUE) SWT.error(SWT.ERROR_INVALID_IMAGE);			palette = getColorMap();			depth = bitsPerSample[0];			break;		}		default: {			SWT.error(SWT.ERROR_INVALID_IMAGE);		}	}	ImageData image = ImageData.internal_new(			imageWidth,			imageLength, 			depth,			palette,			1,			null,			0,			null,			null,			-1,			-1,			SWT.IMAGE_TIFF,			0,			0,			0,			0);	decodePixels(image);	return image;}int toInt(byte[] buffer, int i, int type) {	if (type == TYPE_LONG) {		return isLittleEndian ? 		(buffer[i] & 0xFF) | ((buffer[i + 1] & 0xFF) << 8) | ((buffer[i + 2] & 0xFF) << 16) | ((buffer[i + 3] & 0xFF) << 24) :		(buffer[i + 3] & 0xFF) | ((buffer[i + 2] & 0xFF) << 8) | ((buffer[i + 1] & 0xFF) << 16) | ((buffer[i] & 0xFF) << 24);	}	if (type == TYPE_SHORT) {		return isLittleEndian ? 		(buffer[i] & 0xFF) | ((buffer[i + 1] & 0xFF) << 8) :		(buffer[i + 1] & 0xFF) | ((buffer[i] & 0xFF) << 8);			}	/* Invalid type */	SWT.error(SWT.ERROR_INVALID_IMAGE);	return -1;}void write(int photometricInterpretation) throws IOException {	boolean isRGB = photometricInterpretation == 2;	boolean isColorMap = photometricInterpretation == 3;	boolean isBiLevel = photometricInterpretation == 0 || photometricInterpretation == 1;	int imageWidth = image.width;	int imageLength = image.height;	int rowByteSize = image.bytesPerLine;		int numberEntries = isBiLevel ? 9 : 11;	int lengthDirectory = 2 + 12 * numberEntries + 4;	/* Offset following the header and the directory */	int nextOffset = 8 + lengthDirectory;	/* Extra space used by XResolution and YResolution values */	int extraBytes = 16;	int[] colorMap = null;	if (isColorMap) {			PaletteData palette = image.palette;		RGB[] rgbs = palette.getRGBs();		colorMap = formatColorMap(rgbs);		/* The number of entries of the Color Map must match the bitsPerSample field */		if (colorMap.length != 3 * 1 << image.depth) SWT.error(SWT.ERROR_UNSUPPORTED_FORMAT);		/* Extra space used by ColorMap values */		extraBytes += colorMap.length * 2;	}	if (isRGB) {		/* Extra space used by BitsPerSample values */		extraBytes += 6;	} 	/* TIFF recommends storing the data in strips of no more than 8 Ko */	byte[] data = image.data;	int[][] strips = new int[2][];	int nbrRowsPerStrip = formatStrips(rowByteSize, imageLength, data, 8192, nextOffset, extraBytes, strips);	int[] stripOffsets = strips[0];	int[] stripByteCounts = strips[1];	int bitsPerSampleOffset = NO_VALUE;	if (isRGB) {		bitsPerSampleOffset = nextOffset;		nextOffset += 6;	}	int stripOffsetsOffset = NO_VALUE, stripByteCountsOffset = NO_VALUE;	int xResolutionOffset, yResolutionOffset, colorMapOffset = NO_VALUE;	int cnt = stripOffsets.length;	if (cnt > 1) {		stripOffsetsOffset = nextOffset;		nextOffset += 4 * cnt;		stripByteCountsOffset = nextOffset;		nextOffset += 4 * cnt;	}	xResolutionOffset = nextOffset;	nextOffset += 8;	yResolutionOffset = nextOffset;	nextOffset += 8;	if (isColorMap) {		colorMapOffset = nextOffset;		nextOffset += colorMap.length * 2;	}	/* TIFF header */	writeHeader();		/* Image File Directory */	out.writeShort(numberEntries);		writeEntry(TAG_ImageWidth, TYPE_LONG, 1, imageWidth);		writeEntry(TAG_ImageLength, TYPE_LONG, 1, imageLength);	if (isColorMap) writeEntry(TAG_BitsPerSample, TYPE_SHORT, 1, image.depth);	if (isRGB) writeEntry(TAG_BitsPerSample, TYPE_SHORT, 3, bitsPerSampleOffset);	writeEntry(TAG_Compression, TYPE_SHORT, 1, COMPRESSION_NONE);	writeEntry(TAG_PhotometricInterpretation, TYPE_SHORT, 1, photometricInterpretation);	writeEntry(TAG_StripOffsets, TYPE_LONG, cnt, cnt > 1 ? stripOffsetsOffset : stripOffsets[0]);	if (isRGB) writeEntry(TAG_SamplesPerPixel, TYPE_SHORT, 1, 3);	writeEntry(TAG_RowsPerStrip, TYPE_LONG, 1, nbrRowsPerStrip);	writeEntry(TAG_StripByteCounts, TYPE_LONG, cnt, cnt > 1 ? stripByteCountsOffset : stripByteCounts[0]);	writeEntry(TAG_XResolution, TYPE_RATIONAL, 1, xResolutionOffset);	writeEntry(TAG_YResolution, TYPE_RATIONAL, 1, yResolutionOffset);	if (isColorMap) writeEntry(TAG_ColorMap, TYPE_SHORT, colorMap.length, colorMapOffset);	/* Offset of next IFD (0 for last IFD) */	out.writeInt(0);		/* Values longer than 4 bytes Section */		/* BitsPerSample 8,8,8 */	if (isRGB) for (int i = 0; i < 3; i++) out.writeShort(8);	if (cnt > 1) {		for (int i = 0; i < cnt; i++) out.writeInt(stripOffsets[i]);		for (int i = 0; i < cnt; i++) out.writeInt(stripByteCounts[i]);	}	/* XResolution and YResolution set to 300 dpi */	for (int i = 0; i < 2; i++) {		out.writeInt(300);		out.writeInt(1);	}	/* ColorMap */	if (isColorMap) for (int i = 0; i < colorMap.length; i++) out.writeShort(colorMap[i]);		/* Image Data */	out.write(data);}void writeEntry(short tag, int type, int count, int value) throws IOException {	out.writeShort(tag);	out.writeShort(type);	out.writeInt(count);	out.writeInt(value);}void writeHeader() throws IOException {	/* little endian */	out.writeByte((byte)0x49);	out.writeByte((byte)0x49);	/* TIFF identifier */	out.writeShort(42);	/* 	* Offset of the first IFD is chosen to be 8.	* It is word aligned and immediately after this header.	*/	out.writeInt(8);}void writeToStream(LEDataOutputStream byteStream) throws IOException {	out = byteStream;	int photometricInterpretation = -1;		/* Scanline pad must be 1 */	if (image.scanlinePad != 1) SWT.error(SWT.ERROR_UNSUPPORTED_FORMAT);	switch (image.depth) {		case 1: {			/* Palette must be black and white or white and black */			PaletteData palette = image.palette;			RGB[] rgbs = palette.colors;			if (palette.isDirect || rgbs == null || rgbs.length != 2) SWT.error(SWT.ERROR_UNSUPPORTED_FORMAT);			RGB rgb0 = rgbs[0];			RGB rgb1 = rgbs[1];			if (!(rgb0.red == rgb0.green && rgb0.green == rgb0.blue &&				rgb1.red == rgb1.green && rgb1.green == rgb1.blue &&				((rgb0.red == 0x0 && rgb1.red == 0xFF) || (rgb0.red == 0xFF && rgb1.red == 0x0)))) {				SWT.error(SWT.ERROR_UNSUPPORTED_FORMAT); 			}			/* 0 means a color index of 0 is imaged as white */			photometricInterpretation = image.palette.colors[0].red == 0xFF ? 0 : 1;			break;		}		case 4:		case 8: {			photometricInterpretation = 3;			break;		}		case 24: {			photometricInterpretation = 2;			break;		}		default: {			SWT.error(SWT.ERROR_UNSUPPORTED_FORMAT);		}	}	write(photometricInterpretation);}}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -