comparison scripts/io/Tiff.m @ 31177:c7c79973007f

Tiff: added octave_tiff_handle class to wrap the Tiff file pointer * __tiff__.cc (octave_tiff_handle): implemented a new class octave_tiff_handle as a child of octave_base_value to be a wrapper for the Tiff file handle to be passed to Octave instead of passing a pointer around. * __tiff__.cc (F__open_tiff__, F__tiff_close__): modified handling TIFF file pointer to use the new class (Other internal functions were modified as well). * Tiff.m: removed checking for closed file, it is now handled internally.
author magedrifaat <magedrifaat@gmail.com>
date Wed, 17 Aug 2022 18:51:16 +0200
parents 0abc9779f751
children 14edd6b09efe
comparison
equal deleted inserted replaced
31176:c07461ca34d6 31177:c7c79973007f
1 ## PKG_ADD: __tiff_set_errors_enabled__ (false); 1 ## PKG_ADD: __tiff_set_errors_enabled__ (false);
2 2
3 classdef Tiff < handle 3 classdef Tiff
4 properties (Constant = true) 4 properties (Constant = true)
5 TagID = struct ( 5 TagID = struct (
6 "SubFileType", 254, 6 "SubFileType", 254,
7 "ImageWidth", 256, 7 "ImageWidth", 256,
8 "ImageLength", 257, 8 "ImageLength", 257,
196 ); 196 );
197 endproperties 197 endproperties
198 198
199 properties (Access = private) 199 properties (Access = private)
200 tiff_handle; 200 tiff_handle;
201 closed=false;
202 endproperties 201 endproperties
203 202
204 methods 203 methods
205 function t = Tiff (filename, mode="r") 204 function t = Tiff (filename, mode="r")
206 if (nargin == 0 || nargin > 2) 205 if (nargin == 0 || nargin > 2)
210 209
211 t.tiff_handle = __open_tiff__ (filename, mode); 210 t.tiff_handle = __open_tiff__ (filename, mode);
212 endfunction 211 endfunction
213 212
214 function close (t) 213 function close (t)
215 if (! t.closed) 214 __close_tiff__ (t.tiff_handle);
216 __close_tiff__ (t.tiff_handle);
217 t.closed = true;
218 endif
219 endfunction 215 endfunction
220 216
221 function tag = getTag (t, tag_name) 217 function tag = getTag (t, tag_name)
222 if (t.closed)
223 error ("Image file was closed");
224 endif
225 tag = __tiff_get_tag__ (t.tiff_handle, tag_name); 218 tag = __tiff_get_tag__ (t.tiff_handle, tag_name);
226 endfunction 219 endfunction
227 220
228 function setTag (t, varargin) 221 function setTag (t, varargin)
229 if (t.closed)
230 error ("Image file was closed");
231 endif
232 __tiff_set_tag__ (t.tiff_handle, varargin{:}); 222 __tiff_set_tag__ (t.tiff_handle, varargin{:});
233 endfunction 223 endfunction
234 224
235 function argout = read (t) 225 function argout = read (t)
236 if (t.closed)
237 error ("Image file was closed");
238 endif
239 argout = __tiff_read__ (t.tiff_handle); 226 argout = __tiff_read__ (t.tiff_handle);
240 endfunction 227 endfunction
241 228
242 function stripData = readEncodedStrip (t, stripNumber) 229 function stripData = readEncodedStrip (t, stripNumber)
243 if (t.closed)
244 error ("Image file was closed");
245 endif
246 stripData = __tiff_read_encoded_strip__ (t.tiff_handle, stripNumber); 230 stripData = __tiff_read_encoded_strip__ (t.tiff_handle, stripNumber);
247 endfunction 231 endfunction
248 232
249 function tileData = readEncodedTile (t, tileNumber) 233 function tileData = readEncodedTile (t, tileNumber)
250 if (t.closed)
251 error ("Image file was closed");
252 endif
253 tileData = __tiff_read_encoded_tile__ (t.tiff_handle, tileNumber); 234 tileData = __tiff_read_encoded_tile__ (t.tiff_handle, tileNumber);
254 endfunction 235 endfunction
255 236
256 function [RGB, alpha] = readRGBAImage (t) 237 function [RGB, alpha] = readRGBAImage (t)
257 if (t.closed)
258 error ("Image file was closed");
259 endif
260 [RGB, alpha] = __tiff_read_rgba_image__ (t.tiff_handle); 238 [RGB, alpha] = __tiff_read_rgba_image__ (t.tiff_handle);
261 endfunction 239 endfunction
262 240
263 function [RGB, alpha] = readRGBAStrip (t, row) 241 function [RGB, alpha] = readRGBAStrip (t, row)
264 if (t.closed)
265 error ("Image file was closed");
266 endif
267 [RGB, alpha] = __tiff_read_rgba_strip__ (t.tiff_handle, row); 242 [RGB, alpha] = __tiff_read_rgba_strip__ (t.tiff_handle, row);
268 endfunction 243 endfunction
269 244
270 function [RGB, alpha] = readRGBATile (t, row, col) 245 function [RGB, alpha] = readRGBATile (t, row, col)
271 if (t.closed)
272 error ("Image file was closed");
273 endif
274 [RGB, alpha] = __tiff_read_rgba_tile__ (t.tiff_handle, row, col); 246 [RGB, alpha] = __tiff_read_rgba_tile__ (t.tiff_handle, row, col);
275 endfunction 247 endfunction
276 248
277 function write (t, imageData) 249 function write (t, imageData)
278 if (t.closed)
279 error ("Image file was closed");
280 endif
281 __tiff_write__ (t.tiff_handle, imageData); 250 __tiff_write__ (t.tiff_handle, imageData);
282 endfunction 251 endfunction
283 252
284 function writeEncodedStrip (t, stripNumber, imageData) 253 function writeEncodedStrip (t, stripNumber, imageData)
285 if (t.closed)
286 error ("Image file was closed");
287 endif
288 __tiff_write_encoded_strip__ (t.tiff_handle, stripNumber, imageData); 254 __tiff_write_encoded_strip__ (t.tiff_handle, stripNumber, imageData);
289 endfunction 255 endfunction
290 256
291 function writeEncodedTile (t, tileNumber, imageData) 257 function writeEncodedTile (t, tileNumber, imageData)
292 if (t.closed)
293 error ("Image file was closed");
294 endif
295 __tiff_write_encoded_tile__ (t.tiff_handle, tileNumber, imageData); 258 __tiff_write_encoded_tile__ (t.tiff_handle, tileNumber, imageData);
296 endfunction 259 endfunction
297 260
298 function tf = isTiled (t) 261 function tf = isTiled (t)
299 if (t.closed)
300 error ("Image file was closed");
301 endif
302 tf = __tiff_is_tiled__ (t.tiff_handle); 262 tf = __tiff_is_tiled__ (t.tiff_handle);
303 endfunction 263 endfunction
304 264
305 function numStrips = numberOfStrips (t) 265 function numStrips = numberOfStrips (t)
306 if (t.closed)
307 error ("Image file was closed");
308 endif
309 numStrips = __tiff_number_of_strips__ (t.tiff_handle); 266 numStrips = __tiff_number_of_strips__ (t.tiff_handle);
310 endfunction 267 endfunction
311 268
312 function numTiles = numberOfTiles (t) 269 function numTiles = numberOfTiles (t)
313 if (t.closed)
314 error ("Image file was closed");
315 endif
316 numTiles = __tiff_number_of_tiles__ (t.tiff_handle); 270 numTiles = __tiff_number_of_tiles__ (t.tiff_handle);
317 endfunction 271 endfunction
318 272
319 function stripNumber = computeStrip (t, varargin) 273 function stripNumber = computeStrip (t, varargin)
320 if (t.closed)
321 error ("Image file was closed");
322 endif
323 stripNumber = __tiff_compute_strip__ (t.tiff_handle, varargin{:}); 274 stripNumber = __tiff_compute_strip__ (t.tiff_handle, varargin{:});
324 endfunction 275 endfunction
325 276
326 function tileNumber = computeTile (t, varargin) 277 function tileNumber = computeTile (t, varargin)
327 if (t.closed)
328 error ("Image file was closed");
329 endif
330 tileNumber = __tiff_compute_tile__ (t.tiff_handle, varargin{:}); 278 tileNumber = __tiff_compute_tile__ (t.tiff_handle, varargin{:});
331 endfunction 279 endfunction
332 280
333 function dirNum = currentDirectory (t) 281 function dirNum = currentDirectory (t)
334 if (t.closed)
335 error ("Image file was closed");
336 endif
337 dirNum = __tiff_current_directory__ (t.tiff_handle); 282 dirNum = __tiff_current_directory__ (t.tiff_handle);
338 endfunction 283 endfunction
339 284
340 function isLast = lastDirectory (t) 285 function isLast = lastDirectory (t)
341 if (t.closed)
342 error ("Image file was closed");
343 endif
344 isLast = __tiff_last_directory__ (t.tiff_handle); 286 isLast = __tiff_last_directory__ (t.tiff_handle);
345 endfunction 287 endfunction
346 288
347 function nextDirectory (t) 289 function nextDirectory (t)
348 if (t.closed)
349 error ("Image file was closed");
350 endif
351 __tiff_next_directory__ (t.tiff_handle); 290 __tiff_next_directory__ (t.tiff_handle);
352 endfunction 291 endfunction
353 292
354 function setDirectory (t, dirNum) 293 function setDirectory (t, dirNum)
355 if (t.closed)
356 error ("Image file was closed");
357 endif
358 __tiff_set_directory__ (t.tiff_handle, dirNum); 294 __tiff_set_directory__ (t.tiff_handle, dirNum);
359 endfunction 295 endfunction
360 296
361 function writeDirectory (t) 297 function writeDirectory (t)
362 if (t.closed)
363 error ("Image file was closed");
364 endif
365 __tiff_write_directory__ (t.tiff_handle); 298 __tiff_write_directory__ (t.tiff_handle);
366 endfunction 299 endfunction
367 300
368 function rewriteDirectory (t) 301 function rewriteDirectory (t)
369 if (t.closed)
370 error ("Image file was closed");
371 endif
372 __tiff_rewrite_directory__ (t.tiff_handle); 302 __tiff_rewrite_directory__ (t.tiff_handle);
373 endfunction 303 endfunction
374 304
375 function setSubDirectory (t, offset) 305 function setSubDirectory (t, offset)
376 if (t.closed)
377 error ("Image file was closed");
378 endif
379 __tiff_set_sub_directory__ (t.tiff_handle, offset); 306 __tiff_set_sub_directory__ (t.tiff_handle, offset);
380 endfunction 307 endfunction
381 308
382 % TODO(maged): add documentation and make print_usage work 309 % TODO(maged): add documentation and make print_usage work
383 endmethods 310 endmethods