Skip to content

Commit 644df60

Browse files
dakerfloryst
authored andcommitted
feat(TGAReader): add vtkTGAReader
fixes #3155
1 parent 3027990 commit 644df60

File tree

7 files changed

+1201
-392
lines changed

7 files changed

+1201
-392
lines changed
Loading

Documentation/content/examples/index.md

+394-392
Large diffs are not rendered by default.
+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
const TYPE_INDEXED = 1;
2+
const TYPE_RGB = 2;
3+
const TYPE_GREY = 3;
4+
const TYPE_RLE_INDEXED = 9;
5+
const TYPE_RLE_RGB = 10;
6+
const TYPE_RLE_GREY = 11;
7+
const ORIGIN_MASK = 0x30;
8+
const ORIGIN_SHIFT = 0x04;
9+
const ORIGIN_BL = 0x00;
10+
const ORIGIN_BR = 0x01;
11+
const ORIGIN_UL = 0x02;
12+
const ORIGIN_UR = 0x03;
13+
14+
export default {
15+
TYPE_INDEXED,
16+
TYPE_RGB,
17+
TYPE_GREY,
18+
TYPE_RLE_INDEXED,
19+
TYPE_RLE_RGB,
20+
TYPE_RLE_GREY,
21+
ORIGIN_MASK,
22+
ORIGIN_SHIFT,
23+
ORIGIN_BL,
24+
ORIGIN_BR,
25+
ORIGIN_UL,
26+
ORIGIN_UR,
27+
};
+122
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
import '@kitware/vtk.js/favicon';
2+
3+
// Load the rendering pieces we want to use (for both WebGL and WebGPU)
4+
import '@kitware/vtk.js/Rendering/Profiles/Geometry';
5+
6+
import vtkFullScreenRenderWindow from '@kitware/vtk.js/Rendering/Misc/FullScreenRenderWindow';
7+
import vtkActor from '@kitware/vtk.js/Rendering/Core/Actor';
8+
import vtkMapper from '@kitware/vtk.js/Rendering/Core/Mapper';
9+
import vtkPlaneSource from '@kitware/vtk.js/Filters/Sources/PlaneSource';
10+
import vtkTGAReader from '@kitware/vtk.js/IO/Image/TGAReader';
11+
import vtkTexture from '@kitware/vtk.js/Rendering/Core/Texture';
12+
import vtkURLExtract from '@kitware/vtk.js/Common/Core/URLExtract';
13+
14+
// ----------------------------------------------------------------------------
15+
// Example code
16+
// ----------------------------------------------------------------------------
17+
const userParams = vtkURLExtract.extractURLParameters();
18+
19+
const reader = vtkTGAReader.newInstance();
20+
const texture = vtkTexture.newInstance();
21+
const planeSource = vtkPlaneSource.newInstance();
22+
const mapper = vtkMapper.newInstance();
23+
const actor = vtkActor.newInstance();
24+
mapper.setInputConnection(planeSource.getOutputPort());
25+
actor.setMapper(mapper);
26+
27+
// ----------------------------------------------------------------------------
28+
// Use a file reader to load a local file
29+
// ----------------------------------------------------------------------------
30+
31+
const myContainer = document.querySelector('body');
32+
const fileContainer = document.createElement('div');
33+
fileContainer.innerHTML =
34+
'<div>Select a tga file.<br/><input type="file" class="file"/></div>';
35+
myContainer.appendChild(fileContainer);
36+
37+
const fileInput = fileContainer.querySelector('input');
38+
39+
function zoomCameraToFitPlane(camera, planeWidth, planeHeight) {
40+
const fov = 60; // Field of view in degrees
41+
42+
// Calculate the distance needed to fit the plane in view
43+
const distance =
44+
Math.max(planeWidth, planeHeight) /
45+
(2 * Math.tan((fov * Math.PI) / 180 / 2));
46+
47+
// Set camera position
48+
camera.setPosition(planeWidth / 2, planeHeight / 2, distance);
49+
camera.setFocalPoint(planeWidth / 2, planeHeight / 2, 0);
50+
camera.setViewUp(0, 1, 0);
51+
52+
// Set parallel scale for orthographic projection
53+
camera.setParallelScale(planeHeight / 2);
54+
}
55+
56+
function update() {
57+
// Get the vtkImageData from the reader
58+
const imageData = reader.getOutputData(0);
59+
60+
// Set the vtkImageData as the texture input
61+
texture.setInputData(imageData);
62+
63+
// // Get the image's extent and spacing
64+
const [xMin, xMax, yMin, yMax] = imageData.getExtent();
65+
const [spacingX, spacingY] = imageData.getSpacing();
66+
67+
// // Calculate the plane's width and height based on the image's dimensions
68+
const planeWidth = (xMax - xMin + 1) * spacingX;
69+
const planeHeight = (yMax - yMin + 1) * spacingY;
70+
71+
// Set the plane's origin and corners based on calculated width and height
72+
planeSource.setOrigin(0, 0, 0);
73+
planeSource.setPoint1(planeWidth, 0, 0); // Horizontal edge
74+
planeSource.setPoint2(0, planeHeight, 0); // Vertical edge
75+
76+
actor.addTexture(texture);
77+
78+
const fullScreenRenderer = vtkFullScreenRenderWindow.newInstance();
79+
const renderer = fullScreenRenderer.getRenderer();
80+
const renderWindow = fullScreenRenderer.getRenderWindow();
81+
const camera = renderer.getActiveCamera();
82+
const interactor = renderWindow.getInteractor();
83+
84+
// Disable default interactor style
85+
interactor.setInteractorStyle(null);
86+
87+
renderer.addActor(actor);
88+
89+
// Adjust the camera to fit the plane in the view
90+
zoomCameraToFitPlane(camera, planeWidth, planeHeight);
91+
renderer.resetCameraClippingRange();
92+
93+
renderWindow.render();
94+
}
95+
96+
function handleFile(event) {
97+
event.preventDefault();
98+
const dataTransfer = event.dataTransfer;
99+
const files = event.target.files || dataTransfer.files;
100+
if (files.length === 1) {
101+
const file = files[0];
102+
const fileReader = new FileReader();
103+
fileReader.onload = () => {
104+
reader.parse(fileReader.result);
105+
update();
106+
};
107+
fileReader.readAsArrayBuffer(file);
108+
}
109+
}
110+
111+
fileInput.addEventListener('change', handleFile);
112+
113+
// ----------------------------------------------------------------------------
114+
// Use the reader to download a file
115+
// ----------------------------------------------------------------------------
116+
if (userParams.fileURL) {
117+
reader.setUrl(userParams.fileURL).then(() => {
118+
reader.loadData().then(() => {
119+
update();
120+
});
121+
});
122+
}

Sources/IO/Image/TGAReader/index.d.ts

+121
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
import { vtkAlgorithm, vtkObject } from '../../../interfaces';
2+
import HtmlDataAccessHelper from '../../Core/DataAccessHelper/HtmlDataAccessHelper';
3+
import HttpDataAccessHelper from '../../Core/DataAccessHelper/HttpDataAccessHelper';
4+
import JSZipDataAccessHelper from '../../Core/DataAccessHelper/JSZipDataAccessHelper';
5+
import LiteHttpDataAccessHelper from '../../Core/DataAccessHelper/LiteHttpDataAccessHelper';
6+
7+
interface ITGAReaderOptions {
8+
compression?: string;
9+
progressCallback?: any;
10+
}
11+
12+
/**
13+
*
14+
*/
15+
export interface ITGAReaderInitialValues {}
16+
17+
type vtkTGAReaderBase = vtkObject &
18+
Omit<
19+
vtkAlgorithm,
20+
| 'getInputData'
21+
| 'setInputData'
22+
| 'setInputConnection'
23+
| 'getInputConnection'
24+
| 'addInputConnection'
25+
| 'addInputData'
26+
>;
27+
28+
export interface vtkTGAReader extends vtkTGAReaderBase {
29+
/**
30+
* Get the base url.
31+
*/
32+
getBaseURL(): string;
33+
34+
/**
35+
* Get the dataAccess helper.
36+
*/
37+
getDataAccessHelper():
38+
| HtmlDataAccessHelper
39+
| HttpDataAccessHelper
40+
| JSZipDataAccessHelper
41+
| LiteHttpDataAccessHelper;
42+
43+
/**
44+
* Get the url of the object to load.
45+
*/
46+
getUrl(): string;
47+
48+
/**
49+
* Load the object data.
50+
* @param {ITGAReaderOptions} [options]
51+
*/
52+
loadData(options?: ITGAReaderOptions): Promise<any>;
53+
54+
/**
55+
* Parse data.
56+
* @param {ArrayBuffer} content The content to parse.
57+
*/
58+
parse(content: ArrayBuffer): void;
59+
60+
/**
61+
* Parse data as ArrayBuffer.
62+
* @param {ArrayBuffer} content The content to parse.
63+
*/
64+
parseAsArrayBuffer(content: ArrayBuffer): void;
65+
66+
/**
67+
*
68+
* @param inData
69+
* @param outData
70+
*/
71+
requestData(inData: any, outData: any): void;
72+
73+
/**
74+
*
75+
* @param dataAccessHelper
76+
*/
77+
setDataAccessHelper(
78+
dataAccessHelper:
79+
| HtmlDataAccessHelper
80+
| HttpDataAccessHelper
81+
| JSZipDataAccessHelper
82+
| LiteHttpDataAccessHelper
83+
): boolean;
84+
85+
/**
86+
* Set the url of the object to load.
87+
* @param {String} url the url of the object to load.
88+
* @param {ITGAReaderOptions} [option] The PLY reader options.
89+
*/
90+
setUrl(url: string, option?: ITGAReaderOptions): Promise<string | any>;
91+
}
92+
93+
/**
94+
* Method used to decorate a given object (publicAPI+model) with vtkTGAReader characteristics.
95+
*
96+
* @param publicAPI object on which methods will be bounds (public)
97+
* @param model object on which data structure will be bounds (protected)
98+
* @param {ITGAReaderInitialValues} [initialValues] (default: {})
99+
*/
100+
export function extend(
101+
publicAPI: object,
102+
model: object,
103+
initialValues?: ITGAReaderInitialValues
104+
): void;
105+
106+
/**
107+
* Method used to create a new instance of vtkTGAReader
108+
* @param {ITGAReaderInitialValues} [initialValues] for pre-setting some of its content
109+
*/
110+
export function newInstance(
111+
initialValues?: ITGAReaderInitialValues
112+
): vtkTGAReader;
113+
114+
/**
115+
* vtkTGAReader is a source object that reads Truevision Targa(TGA) files.
116+
*/
117+
export declare const vtkTGAReader: {
118+
newInstance: typeof newInstance;
119+
extend: typeof extend;
120+
};
121+
export default vtkTGAReader;

0 commit comments

Comments
 (0)