forked from siliconrhino/tiff.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tiff_api.ts
125 lines (107 loc) · 3.5 KB
/
tiff_api.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
class Tiff {
private static uniqueIdForFileName = 0;
private _filename: string;
private _tiffPtr: number;
constructor(params: Tiff.Params) {
if (!params.url && !params.buffer) {
throw new Tiff.Exception('Invalid parameter, need either .buffer or .url');
}
if (params.url) {
this._filename = Tiff.createFileSystemObjectFromURL(params.url);
} else {
this._filename = Tiff.createFileSystemObjectFromBuffer(params.buffer);
}
this._tiffPtr = TIFFOpen(this._filename, 'r');
if (this._tiffPtr === 0) {
throw new Tiff.Exception('The function TIFFOpen returns NULL')
}
}
width(): number {
return this.getField(Tiff.Tag.IMAGEWIDTH);
}
height(): number {
return this.getField(Tiff.Tag.IMAGELENGTH);
}
currentDirectory(): number {
return TIFFCurrentDirectory(this._tiffPtr);
}
lastDirectory(): number {
return TIFFLastDirectory(this._tiffPtr);
}
setDirectory(index: number): void {
TIFFSetDirectory(this._tiffPtr, index);
}
getField(tag: number): number {
var value: number = GetField(this._tiffPtr, tag);
return value;
}
readRGBAImage(): ArrayBuffer {
var width = this.width();
var height = this.height();
var raster = _TIFFmalloc(width * height * 4);
var result = TIFFReadRGBAImageOriented(
this._tiffPtr, width, height, raster, 1, 0);
if (result === 0) {
throw new Tiff.Exception('The function TIFFReadRGBAImageOriented returns NULL');
}
var data = <ArrayBuffer>(<any>HEAP8.buffer).slice(
raster,
raster + width * height * 4
);
_TIFFfree(raster);
return data;
}
close(): void {
TIFFClose(this._tiffPtr);
}
private static createUniqueFileName(): string {
Tiff.uniqueIdForFileName += 1;
return String(Tiff.uniqueIdForFileName) + '.tiff';
}
private static createFileSystemObjectFromURL(url: string): string {
var filename = Tiff.createUniqueFileName();
FS.createLazyFile('/', url, filename, true, false);
return filename;
}
private static createFileSystemObjectFromBuffer(buffer: ArrayBuffer): string {
var filename = Tiff.createUniqueFileName();
FS.createDataFile('/', filename, new Uint8Array(buffer), true, false);
return filename;
}
}
module Tiff {
export interface Params {
url?: string;
buffer?: ArrayBuffer;
}
export class Exception {
name: string = 'Tiff.Exception';
constructor(public message: string) {}
}
export var Tag: typeof TiffTag = TiffTag;
}
// for closure compiler
Tiff.prototype['width'] = Tiff.prototype.width;
Tiff.prototype['height'] = Tiff.prototype.height;
Tiff.prototype['currentDirectory'] = Tiff.prototype.currentDirectory;
Tiff.prototype['lastDirectory'] = Tiff.prototype.lastDirectory;
Tiff.prototype['setDirectory'] = Tiff.prototype.setDirectory;
Tiff.prototype['getField'] = Tiff.prototype.getField;
Tiff.prototype['readRGBAImage'] = Tiff.prototype.readRGBAImage;
Tiff.prototype['close'] = Tiff.prototype.close;
Tiff['Exception'] = Tiff.Exception;
// export to node, amd, window or worker
declare var process: any;
declare var require: any;
declare var module: any;
declare var define: any;
declare var self: any;
if (typeof process === 'object' && typeof require === 'function') { // NODE
module['exports'] = Tiff;
} else if (typeof define === "function" && define.amd) { // AMD
define('tiff', <any>[], () => Tiff);
} else if (typeof window === 'object') { // WEB
window['Tiff'] = Tiff;
} else if (typeof importScripts === 'function') { // WORKER
self['Tiff'] = Tiff;
}