comparison libinterp/corefcn/__tiff__.cc @ 31172:3f5f1404af8a

Tiff: added directory methods (currentDirectory, nextDirectory, ...) * __tiff__.cc: implemented the internal functions for the directory methods. * Tiff.m: added the methods to the class and added a test for the methods.
author magedrifaat <magedrifaat@gmail.com>
date Sun, 14 Aug 2022 21:02:58 +0200
parents 8bf3fa6b6977
children 0abc9779f751
comparison
equal deleted inserted replaced
31171:8bf3fa6b6977 31172:3f5f1404af8a
1179 // Since scalar tags are the last to be handled, any tag that 1179 // Since scalar tags are the last to be handled, any tag that
1180 // require a count to be passed is an unsupported tag. 1180 // require a count to be passed is an unsupported tag.
1181 if (TIFFFieldPassCount (fip)) 1181 if (TIFFFieldPassCount (fip))
1182 error ("Unsupported tag"); 1182 error ("Unsupported tag");
1183 1183
1184 TIFFDataType tag_datatype = TIFFFieldDataType (fip);
1185
1184 // According to matlab, the value must be a scalar double 1186 // According to matlab, the value must be a scalar double
1185 if (! tag_ov.is_scalar_type () || ! tag_ov.is_double_type ()) 1187 // except for strings
1188 if (tag_datatype == TIFF_ASCII)
1189 {
1190 if (! tag_ov.is_string ())
1191 error ("Expected string for ascii tag");
1192 }
1193 else if (! tag_ov.is_scalar_type () || ! tag_ov.is_double_type ())
1186 error ("Tag value must be a scalar double"); 1194 error ("Tag value must be a scalar double");
1187 1195
1188 TIFFDataType tag_datatype = TIFFFieldDataType (fip);
1189 switch (tag_datatype) 1196 switch (tag_datatype)
1190 { 1197 {
1191 case TIFF_BYTE: 1198 case TIFF_BYTE:
1192 case TIFF_UNDEFINED: 1199 case TIFF_UNDEFINED:
1193 TIFFSetField (tif, tag_id, tag_ov.uint8_scalar_value ()); 1200 TIFFSetField (tif, tag_id, tag_ov.uint8_scalar_value ());
2398 error ("Wrong number of arguments"); 2405 error ("Wrong number of arguments");
2399 2406
2400 TIFF *tif = (TIFF *)(args(0).uint64_value ()); 2407 TIFF *tif = (TIFF *)(args(0).uint64_value ());
2401 2408
2402 // TODO(maged): check matlab behavior for missing/ wrong/ out of bounds row 2409 // TODO(maged): check matlab behavior for missing/ wrong/ out of bounds row
2410 // matlab row must be double (scalar or array), and checks bounds
2403 uint32_t row = args (1).uint32_scalar_value (); 2411 uint32_t row = args (1).uint32_scalar_value ();
2404 2412
2405 tiff_image_data image_data (tif); 2413 tiff_image_data image_data (tif);
2406 if (image_data.is_tiled) 2414 if (image_data.is_tiled)
2407 error ("The image is tiled not stripped"); 2415 error ("The image is tiled not stripped");
2408 2416
2409 if (row < 1 || row > image_data.height) 2417 if (row < 1 || row > image_data.height)
2410 error ("Row out of bounds of the image"); 2418 error ("Row out of bounds of the image");
2411 2419
2412 // TODO(maged): check if matlab require the first row in strip as well
2413 // Convert from 1-based indexing to zero-based 2420 // Convert from 1-based indexing to zero-based
2414 row--; 2421 row--;
2415 2422
2416 uint32_t rows_in_strip; 2423 uint32_t rows_in_strip;
2417 if (! TIFFGetFieldDefaulted (tif, TIFFTAG_ROWSPERSTRIP, &rows_in_strip)) 2424 if (! TIFFGetFieldDefaulted (tif, TIFFTAG_ROWSPERSTRIP, &rows_in_strip))
2435 uint8NDArray strip_data (strip_dims); 2442 uint8NDArray strip_data (strip_dims);
2436 uint32_t *strip_ptr 2443 uint32_t *strip_ptr
2437 = reinterpret_cast <uint32_t *> (strip_data.fortran_vec ()); 2444 = reinterpret_cast <uint32_t *> (strip_data.fortran_vec ());
2438 2445
2439 // TODO(maged): check if matlab does anything with orientation tag 2446 // TODO(maged): check if matlab does anything with orientation tag
2447 // matlab uses the orientation tag to correct the data
2440 if (! TIFFReadRGBAStrip (tif, row, strip_ptr)) 2448 if (! TIFFReadRGBAStrip (tif, row, strip_ptr))
2441 error ("Failed to read strip"); 2449 error ("Failed to read strip");
2442 2450
2443 // Permute to the correct order of dimensions for Octave 2451 // Permute to the correct order of dimensions for Octave
2444 Array<octave_idx_type> perm (dim_vector (3, 1)); 2452 Array<octave_idx_type> perm (dim_vector (3, 1));
2880 #else 2888 #else
2881 err_disabled_feature ("computeTile", "Tiff"); 2889 err_disabled_feature ("computeTile", "Tiff");
2882 #endif 2890 #endif
2883 } 2891 }
2884 2892
2893 DEFUN (__tiff_current_directory__, args, ,
2894 "Get the index of the current directory")
2895 {
2896 #if defined (HAVE_TIFF)
2897 int nargin = args.length ();
2898
2899 if (nargin != 1)
2900 error ("Wrong number of arguments\n");
2901
2902 TIFF *tif = (TIFF *)(args(0).uint64_value ());
2903
2904 // TODO(maged): check matlab behavior
2905 uint16_t dir = TIFFCurrentDirectory (tif);
2906 if (dir == (uint16_t)-1)
2907 dir = 0;
2908
2909 return octave_value_list (octave_value (dir + 1));
2910 #else
2911 err_disabled_feature ("currentDirectory", "Tiff");
2912 #endif
2913 }
2914
2915 DEFUN (__tiff_last_directory__, args, ,
2916 "Get the whether the current directory is the last")
2917 {
2918 #if defined (HAVE_TIFF)
2919 int nargin = args.length ();
2920
2921 if (nargin != 1)
2922 error ("Wrong number of arguments\n");
2923
2924 TIFF *tif = (TIFF *)(args(0).uint64_value ());
2925
2926 bool is_last = TIFFLastDirectory (tif);
2927
2928 return octave_value_list (octave_value (is_last));
2929 #else
2930 err_disabled_feature ("lastDirectory", "Tiff");
2931 #endif
2932 }
2933
2934 DEFUN (__tiff_next_directory__, args, ,
2935 "Set the next IFD as the current IFD")
2936 {
2937 #if defined (HAVE_TIFF)
2938 int nargin = args.length ();
2939
2940 if (nargin != 1)
2941 error ("Wrong number of arguments\n");
2942
2943 TIFF *tif = (TIFF *)(args(0).uint64_value ());
2944
2945 // TODO(maged): check if matlab handles this case different from
2946 // an erronous next IFD
2947 bool is_last = TIFFLastDirectory (tif);
2948 if (is_last)
2949 error ("Current directory is the last directory");
2950
2951 if (! TIFFReadDirectory (tif))
2952 error ("Failed to read the next directory");
2953
2954 return octave_value_list ();
2955 #else
2956 err_disabled_feature ("nextDirectory", "Tiff");
2957 #endif
2958 }
2959
2960 DEFUN (__tiff_set_directory__, args, ,
2961 "Set the current IFD using the given index")
2962 {
2963 #if defined (HAVE_TIFF)
2964 int nargin = args.length ();
2965
2966 if (nargin != 2)
2967 error ("Wrong number of arguments\n");
2968
2969 TIFF *tif = (TIFF *)(args(0).uint64_value ());
2970
2971 // TODO(maged): check matlab behavior for wrong argument type
2972 // and out of bounds index
2973 uint16_t dir = args(1).uint16_scalar_value ();
2974 if (dir < 1 || dir > TIFFNumberOfDirectories (tif))
2975 error ("Directory index out of bounds");
2976
2977 dir--;
2978
2979 if (! TIFFSetDirectory(tif, dir))
2980 error ("Failed to read directory");
2981
2982 return octave_value_list ();
2983 #else
2984 err_disabled_feature ("setDirectory", "Tiff");
2985 #endif
2986 }
2987
2988 DEFUN (__tiff_write_directory__, args, ,
2989 "Write the current IFD to file and create a new one")
2990 {
2991 #if defined (HAVE_TIFF)
2992 int nargin = args.length ();
2993
2994 if (nargin != 1)
2995 error ("Wrong number of arguments\n");
2996
2997 TIFF *tif = (TIFF *)(args(0).uint64_value ());
2998 // TODO(maged): check if matlab errors for leaving a corrupt directory
2999 // Check if mtalab always writes directories at the end for both w and a (And r+)
3000 if (! TIFFWriteDirectory(tif))
3001 error ("Failed to write directory");
3002
3003 return octave_value_list ();
3004 #else
3005 err_disabled_feature ("writeDirectory", "Tiff");
3006 #endif
3007 }
3008
3009 DEFUN (__tiff_rewrite_directory__, args, ,
3010 "Rewrite modifications to the current IFD")
3011 {
3012 #if defined (HAVE_TIFF)
3013 int nargin = args.length ();
3014
3015 if (nargin != 1)
3016 error ("Wrong number of arguments\n");
3017
3018 TIFF *tif = (TIFF *)(args(0).uint64_value ());
3019 // TODO(maged): check if matlab errors for leaving a corrupt directory
3020 // check if matlab changes directory after the call or switches back
3021 if (! TIFFRewriteDirectory(tif))
3022 error ("Failed to rewrite directory");
3023
3024 return octave_value_list ();
3025 #else
3026 err_disabled_feature ("rewriteDirectory", "Tiff");
3027 #endif
3028 }
3029
3030 DEFUN (__tiff_set_sub_directory__, args, ,
3031 "Set the given offset directory as the current IFD")
3032 {
3033 #if defined (HAVE_TIFF)
3034 int nargin = args.length ();
3035
3036 if (nargin != 2)
3037 error ("Wrong number of arguments\n");
3038
3039 TIFF *tif = (TIFF *)(args(0).uint64_value ());
3040
3041 // TODO(maged): check if matlab requires scalar double
3042 uint64_t offset = args(1).uint64_scalar_value ();
3043 if (! TIFFSetSubDirectory (tif, offset))
3044 error ("Failed to switch to the sub directory");
3045
3046 return octave_value_list ();
3047 #else
3048 err_disabled_feature ("setSubDirectory", "Tiff");
3049 #endif
3050 }
3051
2885 DEFUN (__tiff_version__, , , 3052 DEFUN (__tiff_version__, , ,
2886 "Get the version stamp of LibTIFF") 3053 "Get the version stamp of LibTIFF")
2887 { 3054 {
2888 #if defined (HAVE_TIFF) 3055 #if defined (HAVE_TIFF)
2889 std::string version = std::string (TIFFGetVersion ()); 3056 std::string version = std::string (TIFFGetVersion ());