Hexagon Geospatial
MENU

ERDAS APOLLO & ECW/JP2

Wondering how others have configured their ERDAS APOLLO server or what data they are crawling? The ERDAS APOLLO Discussion board is a place to find information, share ideas and more. Join the community, connect, contribute and share.
Showing results for 
Search instead for 
Do you mean 
Reply
Highlighted
Occasional Contributor
Posts: 8
Registered: ‎02-21-2019
Accepted Solution

ECW SDK 5.4.0 Exception with macos mono, fine with windows

Hello,

we are using the ecw sdk with c# for a project targeting windows and xamarin.mac with these pinvoke-calls:

 

 

#if WINDOWS
internal const string DllPath = "NCSEcw.dll";
#elif OSX
internal const string DllPath = "libNCSEcw.dylib";
#endif
[DllImport(DllPath)]
public static extern void NCSGetLibVersion(out int major, out int minor);

[DllImport(DllPath, CharSet = CharSet.Auto)]
public static extern NCSError NCSOpenFileView(string fileName, out IntPtr pNCSFileView, IntPtr callBack);

[DllImport(DllPath)]
public static extern NCSError NCSCloseFileViewEx(IntPtr pNCSFileView, bool clearCache);

[DllImport(DllPath)]
public static extern NCSError NCSGetViewFileInfo(IntPtr pNCSFileView, out IntPtr ppNCSFileViewFileInfoPointer);

//// https://docs.microsoft.com/de-de/dotnet/framework/interop/marshaling-different-types-of-arrays
[DllImport(DllPath)]
public static extern NCSError NCSSetFileView(IntPtr pNCSFileView, uint nBands, [In, Out] uint[] pBandList, uint nTLX, uint nTLY, uint nBRX, uint nBRY, uint nSizeX, uint nSizeY);

[DllImport(DllPath)]
public static extern NCSError NCSReadViewLineBGR(IntPtr pNCSFileView, IntPtr pBGRTriplets);

// extern NCSReadStatus NCS_CALL NCSReadViewLineBGRA( NCSFileView *pNCSFileView, UINT32 *pBGRA);
[DllImport(DllPath)]
public static extern NCSError NCSReadViewLineBGRA(IntPtr pNCSFileView, IntPtr pBGRTriplets);

[DllImport(DllPath, CharSet = CharSet.Auto)]
public static extern NCSError NCSGetEPSGCode(string szDatum, string szProjection, out int epsg);

 

 

 

Purpose is just to decompress ecw-images in the most simplest way into a bitmap:

var bitmap = new Image32(destrect.Width, destrect.Height);

var r2 = new Rectangle(new Point(_sourceRect.Left, _sourceRect.Top),
                       new Point(_sourceRect.Right - 1, _sourceRect.Bottom - 1));
var fx = (float)r2.Width / (float)_sourceRect.Width;
var fy = (float)r2.Height / (float)_sourceRect.Height;
var destwidht2 = (int)(destrect.Width * fx);
var desthight2 = (int)(destrect.Height * fy);

NCSError result = NCSFunctions.NCSSetFileView(_fileViewPointer,
                                             (uint)_fileInfo.nBands,
                                             _bands,                    
                                             (uint)r2.Left,
                                             (uint)r2.Top,
                                             (uint)r2.Right - 1,
                                             (uint)r2.Bottom -1,
                                             (uint)destwidht2,
                                             (uint)desthight2);
using (var bmdata = bitmap.Pixels .Lock())
{
    var scanwidth = bmdata.Width * bitmap.BytesPerPixel;
    var ptrdest = (IntPtr)bmdata.ImageData ;
    for (int i = 0; i < desthight2; i++)
    {
        result = NCSFunctions.NCSReadViewLineBGRA(_fileViewPointer, ptrdest);
        ptrdest += scanwidth;
    }
}

 

 

On Windows all works fine, all our ecws and also the sdk-testdata works perfect.

 

On Mac its different. It also all works as long as the area in NCSSetFileView is big enough. When we zoom in, requesting smaller areas, from a certain point on, we get an exception.

We compared simultanoiusly same ecw, same parameter on windows and mac. no error on Windows, this exception on Mac.

I have no idea, where to start, maybe something with pinvoke?

Different behaviour of sdk on win and mac?

Dependency missing on mac?

 

Any help would be highly appreceated.

Thanks

Tom

 

 

 

Stacktrace:

  at <unknown> <0xffffffff>
  at (wrapper managed-to-native) QvxMapShared.Maps.Source.Ecw.NCSFunctions.NCSReadViewLineBGRA (intptr,intptr) [0x0000a] in <32e9436c55e040d495813fce28f77c70>:0
  at QvxMapShared.Maps.Source.EcwMapReader/<RenderAsync>d__7.MoveNext () [0x00430] in /Users/tomflemming/Desktop/QV/Qvx1/QvxMapShared/Maps/Source/Raster/Ecw/EcwMapReader.cs:154
  at System.Runtime.CompilerServices.AsyncTaskMethodBuilder.Start<TStateMachine_REF> (TStateMachine_REF&) [0x0002c] in /Library/Frameworks/Xamarin.Mac.framework/Versions/5.2.1.15/src/Xamarin.Mac/mcs/class/referencesource/mscorlib/system/runtime/compilerservices/AsyncMethodBuilder.cs:316
  at QvxMapShared.Maps.Source.EcwMapReader.RenderAsync (QvX.Geo.LocationArea,QvxMapShared.Maps.Scene.IGraphicsRendererImg32,QvX.Maps.Source.IMapSource,System.Threading.CancellationToken) [0x00047] in <32e9436c55e040d495813fce28f77c70>:0
  at QvX.Maps.Source.TilePreparer/<DoJob>d__10.MoveNext () [0x00142] in /Users/tomflemming/Desktop/QV/Qvx1/QvxMapShared/Maps/Source/Tiles/TilePreparer.cs:96
  at System.Runtime.CompilerServices.AsyncTaskMethodBuilder.Start<TStateMachine_REF> (TStateMachine_REF&) [0x0002c] in /Library/Frameworks/Xamarin.Mac.framework/Versions/5.2.1.15/src/Xamarin.Mac/mcs/class/referencesource/mscorlib/system/runtime/compilerservices/AsyncMethodBuilder.cs:316
  at QvX.Maps.Source.TilePreparer.DoJob (QvX.Maps.Source.TileJob) [0x00031] in <32e9436c55e040d495813fce28f77c70>:0
  at QvX.Maps.Source.TilePreparerLoop/<>c__DisplayClass6_0/<<RunLoop>b__0>d.MoveNext () [0x0011d] in /Users/tomflemming/Desktop/QV/Qvx1/QvxMapShared/Maps/Source/Tiles/TilePreparerLoop.cs:87
  at System.Runtime.CompilerServices.AsyncTaskMethodBuilder.Start<TStateMachine_REF> (TStateMachine_REF&) [0x0002c] in /Library/Frameworks/Xamarin.Mac.framework/Versions/5.2.1.15/src/Xamarin.Mac/mcs/class/referencesource/mscorlib/system/runtime/compilerservices/AsyncMethodBuilder.cs:316
  at QvX.Maps.Source.TilePreparerLoop/<>c__DisplayClass6_0.<RunLoop>b__0 () [0x0002a] in <32e9436c55e040d495813fce28f77c70>:0
  at System.Threading.Tasks.Task`1<TResult_REF>.InnerInvoke () [0x0000f] in /Library/Frameworks/Xamarin.Mac.framework/Versions/5.2.1.15/src/Xamarin.Mac/external/corert/src/System.Private.CoreLib/src/System/Threading/Tasks/Future.cs:545
  at System.Threading.Tasks.Task.Execute () [0x00000] in /Library/Frameworks/Xamarin.Mac.framework/Versions/5.2.1.15/src/Xamarin.Mac/external/corert/src/System.Private.CoreLib/src/System/Threading/Tasks/Task.cs:2344
  at System.Threading.Tasks.Task.ExecutionContextCallback (object) [0x00006] in /Library/Frameworks/Xamarin.Mac.framework/Versions/5.2.1.15/src/Xamarin.Mac/external/corert/src/System.Private.CoreLib/src/System/Threading/Tasks/Task.cs:2488
  at System.Threading.ExecutionContext.RunInternal (System.Threading.ExecutionContext,System.Threading.ContextCallback,object,bool) [0x00071] in /Library/Frameworks/Xamarin.Mac.framework/Versions/5.2.1.15/src/Xamarin.Mac/mcs/class/referencesource/mscorlib/system/threading/executioncontext.cs:961
  at System.Threading.ExecutionContext.Run (System.Threading.ExecutionContext,System.Threading.ContextCallback,object,bool) [0x00000] in /Library/Frameworks/Xamarin.Mac.framework/Versions/5.2.1.15/src/Xamarin.Mac/mcs/class/referencesource/mscorlib/system/threading/executioncontext.cs:908
  at System.Threading.Tasks.Task.ExecuteWithThreadLocal (System.Threading.Tasks.Task&) [0x00034] in /Library/Frameworks/Xamarin.Mac.framework/Versions/5.2.1.15/src/Xamarin.Mac/mcs/class/corlib/corert/Task.cs:72
  at System.Threading.Tasks.Task.ExecuteEntry (bool) [0x0004a] in /Library/Frameworks/Xamarin.Mac.framework/Versions/5.2.1.15/src/Xamarin.Mac/external/corert/src/System.Private.CoreLib/src/System/Threading/Tasks/Task.cs:2410
  at System.Threading.Tasks.Task.System.Threading.IThreadPoolWorkItem.ExecuteWorkItem () [0x00000] in /Library/Frameworks/Xamarin.Mac.framework/Versions/5.2.1.15/src/Xamarin.Mac/external/corert/src/System.Private.CoreLib/src/System/Threading/Tasks/Task.cs:2359
  at System.Threading.ThreadPoolWorkQueue.Dispatch () [0x00074] in /Library/Frameworks/Xamarin.Mac.framework/Versions/5.2.1.15/src/Xamarin.Mac/mcs/class/referencesource/mscorlib/system/threading/threadpool.cs:858
  at System.Threading._ThreadPoolWaitCallback.PerformWaitCallback () [0x0001c] in /Library/Frameworks/Xamarin.Mac.framework/Versions/5.2.1.15/src/Xamarin.Mac/mcs/class/referencesource/mscorlib/system/threading/threadpool.cs:1213
  at (wrapper runtime-invoke) <Module>.runtime_invoke_bool (object,intptr,intptr,intptr) [0x0001f] in <ab389cc6b9734134b7106e0afe997556>:0

Native stacktrace:

	0   QvxMac                              0x00000001001bcb38 mono_handle_native_crash + 264
	1   QvxMac                              0x00000001001399b6 altstack_handle_and_restore + 70
	2   libNCSEcw.dylib                     0x0000000102e4c1af _ZN3NCS3ECW5CFile9CPrecinct4ReadEyPNS_3SDK9CBuffer2DEt + 937
	3   libNCSEcw.dylib                     0x0000000102e62bda _ZN3NCS3SDK13NodeTiler2D_TINS0_10HashList_TINS_9CPacketIdENS_3ECW5CFile9CPrecinctENS0_18ItemsDefaultHash_TIS3_EEE13read_accessorENS9_14write_accessorEE4ReadEyPNS0_9CBuffer2DEt + 1142
	4   libNCSEcw.dylib                     0x0000000102e41b44 _ZN3NCS3ECW10CInputNode4ReadEyPNS_3SDK9CBuffer2DEt + 2372
	5   libNCSEcw.dylib                     0x0000000102e60ace _ZN3NCS3ECW7CReader8ReadLineE11NCSCellTypetPPvS2_Pj + 640
	6   libNCSEcw.dylib                     0x000000010307051f _ZN3NCS5CView19ReadLineBILInternalE11NCSCellTypetPPvS1_Pj + 989
	7   libNCSEcw.dylib                     0x0000000103070fb6 _ZN3NCS5CView20ReadLineBGRAInternalEPj + 414
	8   libNCSEcw.dylib                     0x0000000103070dd4 _ZN3NCS5CView12ReadLineBGRAEPj + 54
	9   ???                                 0x0000000119111e51 0x0 + 4715519569
	10  ???                                 0x000000010d5d151f 0x0 + 4519171359
	11  ???                                 0x0000000116094d5b 0x0 + 4664675675
	12  ???                                 0x000000010d5d151f 0x0 + 4519171359
	13  ???                                 0x0000000116090543 0x0 + 4664657219
	14  ???                                 0x000000010d5d151f 0x0 + 4519171359
	15  ???                                 0x000000010e271333 0x0 + 4532409139
	16  ???                                 0x000000010ccd384b 0x0 + 4509743179
	17  ???                                 0x000000010c802813 0x0 + 4504692755
	18  ???                                 0x000000010ccd197b 0x0 + 4509735291
	19  ???                                 0x000000010cccdb4b 0x0 + 4509719371
	20  QvxMac                              0x00000001001d0e31 mono_jit_runtime_invoke + 1441
	21  QvxMac                              0x00000001002ce234 mono_runtime_try_invoke + 148
	22  QvxMac                              0x000000010031a4ae worker_callback + 1102
	23  QvxMac                              0x000000010031825c worker_thread + 348
	24  QvxMac                              0x0000000100322ec9 start_wrapper_internal + 377
	25  QvxMac                              0x0000000100322d27 start_wrapper + 71
	26  libsystem_pthread.dylib             0x00007fff615a1305 _pthread_body + 126
	27  libsystem_pthread.dylib             0x00007fff615a426f _pthread_start + 70
	28  libsystem_pthread.dylib             0x00007fff615a0415 thread_start + 13

Debug info from gdb:

 

Occasional Contributor
Posts: 8
Registered: ‎02-21-2019

Re: ECW SDK 5.4.0 Exception with macos mono, fine with windows

Can nobody decode this errors?

Staff
Posts: 5
Registered: ‎02-28-2019

Re: ECW SDK 5.4.0 Exception with macos mono, fine with windows

Hi Tom,

 

What is the return value of NCSSetFileView and NCSReadViewLineBGRA? If the former is not NCS_SUCCESS you should not proceed to the NCSReadViewLineBGRA call. Also if that call fails, you need to abort reading.

 

Can you reproduce this with some sample C/C++ code without the mono wrappers (e.g. a modified dexample1 from the SDK examples)? Additionally it would help if we had the dataset and the parameters to the SetView when it fails so that we can debug it.

 

Thanks,

Mark

Occasional Contributor
Posts: 8
Registered: ‎02-21-2019

Re: ECW SDK 5.4.0 Exception with macos mono, fine with windows

[ Edited ]

Hi Mark,

 

thanks for answering.

 

I"m testing with the RGB_8bit.ecw from the sdk-testdata. 

Trying to decompress into a bgra-bitmap of 512x256.


The following parameters are working, NCSSetFileView and NCSReadViewLineBGRA both are returning NCS_SUCCESS:

NCSSetFileView: nBands=3, nTLX=0, nTLY=0, nBRX=4319, nBRY=2159, nSizeX=512, nSizeY=256

 

This call for example to NCSSetFileView also returns NCS_SUCCESS, but the following NCSReadViewLineBGRA gives the crash:

NCSSetFileView: nBands=3, nTLX=2160, nTLY=0, nBRX=4319, nBRY=1079, nSizeX=512, nSizeY=256

 

Its only with mono-mac.

Windows c# with the same pinvokes all fine.

And of course also the sdk/samples are fine.

 

Thanks

Tom

 

Occasional Contributor
Posts: 8
Registered: ‎02-21-2019

Re: ECW SDK 5.4.0 Exception with macos mono, fine with windows

Sorry ?

Staff
Posts: 5
Registered: ‎02-28-2019

Re: ECW SDK 5.4.0 Exception with macos mono, fine with windows

Hi Tom,

 

We investigated this on the Mac, the SDK is not throwing any exceptions. We tested with the same input file and set view parameters as above also via a Mono wrapper and could not reproduce the problem. We created our own sample, as the code above was not complete. Using Mono version 5.16.1.0 the images decode as expected with both ECW SDK 5.4 and 5.4 Update 1.

 

I would suggest ensuring you are using the latest Mono, if the issue remains, please supply a fully working example demonstrating the problem.

 

Thanks

Mark

Occasional Contributor
Posts: 8
Registered: ‎02-21-2019

Re: ECW SDK 5.4.0 Exception with macos mono, fine with windows

[ Edited ]

Thanks Mark for testing.

 

I'm also using using Mono version 5.16.1.0.and ECW SDK 5.4 


Could you please post your ecw-mono test-project ?

There must be something different, because I still have that issue.

 

Thank you

Tom

Staff
Posts: 5
Registered: ‎02-28-2019

Re: ECW SDK 5.4.0 Exception with macos mono, fine with windows

Basic sample that works with those parameters:

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Drawing;

namespace MonoEc2099
{
    class Program
    {
        //internal const string DllPath = "NCSEcw.dll";
        internal const string DllPath = "libNCSEcw.dylib";

        [DllImport(DllPath)]
        public static extern void NCSInit();

        [DllImport(DllPath)]
        public static extern void NCSShutdown();

        [DllImport(DllPath)]
        public static extern void NCSGetLibVersion(out int major, out int minor);
        [DllImport(DllPath, CharSet = CharSet.Auto)]
        public static extern NCSError NCSOpenFileView(string fileName, out IntPtr pNCSFileView, IntPtr callBack);
        [DllImport(DllPath)]
        public static extern NCSError NCSCloseFileViewEx(IntPtr pNCSFileView, bool clearCache);
        [DllImport(DllPath)]
        public static extern NCSError NCSGetViewFileInfo(IntPtr pNCSFileView, out IntPtr ppNCSFileViewFileInfoPointer);
        //// https://docs.microsoft.com/de-de/dotnet/framework/interop/marshaling-different-types-of-arrays
        [DllImport(DllPath)]
        public static extern NCSError NCSSetFileView(IntPtr pNCSFileView, uint nBands, [In, Out] uint[] pBandList, uint nTLX, uint nTLY, uint nBRX, uint nBRY, uint nSizeX, uint nSizeY);
        [DllImport(DllPath)]
        public static extern NCSError NCSReadViewLineBGR(IntPtr pNCSFileView, IntPtr pBGRTriplets);
        // extern NCSReadStatus NCS_CALL NCSReadViewLineBGRA( NCSFileView *pNCSFileView, UINT32 *pBGRA);
        [DllImport(DllPath)]
        public static extern NCSError NCSReadViewLineBGRA(IntPtr pNCSFileView, IntPtr pBGRTriplets);
        [DllImport(DllPath, CharSet = CharSet.Auto)]
        public static extern NCSError NCSGetEPSGCode(string szDatum, string szProjection, out int epsg);

        static void Main(string[] args)
        {
            try
            {
                NCSInit();

                int major, minor = 0;
                NCSGetLibVersion(out major, out minor);
		Console.WriteLine("ECWJP2 SDK v{0}.{1}", major, minor);
                IntPtr pNCSFileView = IntPtr.Zero;
 
                NCSError error = NCSOpenFileView("<path_to_ssk>/testdata/RGB_8bit.ecw", out pNCSFileView, IntPtr.Zero);
                
                uint[] bandList = new uint[3] {0, 1, 2};
		
		// nTLX=2160, nTLY=0, nBRX=4319, nBRY=1079, nSizeX=512, nSizeY=256

		int tileWidth = 512;
		int tileHeight = 256;

                NCSError result = NCSSetFileView(pNCSFileView,
                    3,
                    bandList,
                    (uint)2160,
                    (uint)0,
                    (uint)4319,
                    (uint)1079,
                    (uint)tileWidth,
                    (uint)tileHeight);

                var bitmap = new Bitmap(tileWidth, tileHeight);

                var bmdata = bitmap.LockBits(new Rectangle(0, 0, bitmap.Width, bitmap.Height), System.Drawing.Imaging.ImageLockMode.ReadWrite, bitmap.PixelFormat);
		try
                {
			Console.WriteLine("Stride: {0}", bmdata.Stride);
			var ptrdest = (IntPtr)bmdata.Scan0; // bmdata.ImageData;
			for (int i = 0; i < tileHeight; i++)
			{
                        	result = NCSReadViewLineBGRA(pNCSFileView, ptrdest);
				ptrdest += bmdata.Stride;
			}
                }
		finally 
		{
			bitmap.UnlockBits(bmdata);
		}
		bitmap.Save("op.jpg", System.Drawing.Imaging.ImageFormat.Jpeg);
 
            }
            finally
            {
                NCSShutdown();
            }
        }
    }

    internal enum NCSError
    {
    }
}

Its possibly something with the Image32 class?

 

Mark

Occasional Contributor
Posts: 8
Registered: ‎02-21-2019

Re: ECW SDK 5.4.0 Exception with macos mono, fine with windows

Maybe threading?

In our project, ecw is just one part of a complex image composition, which is done in different threads.
So all calls to NCSReadViewLineRGB / NCSSetFleView are from different threads then the one where NCSOpenFileView was called.

This works with Windows, but could this be an issue on mac?

Or do you recomment different aproaches with multi-threading?

Staff
Posts: 5
Registered: ‎02-28-2019

Re: ECW SDK 5.4.0 Exception with macos mono, fine with windows

The SDK is thread safe but not re-entrant. You cant use the same file view for decoding in different threads, you must have a unique one per thread. However, I dont think that opening the file, setting the view and decoding in a different thread would be an issue.

 

The Mac port is essentially the same as the Linux one, using POSIX APIs (pthreads, etc), and the Linux one is used in our APOLLO product which is highly threaded, so I can't imagine there would be an issue there. I'll ask the developers and see if anyone has any other ideas.

 

I guess you could try deferring the open/setview to the decoder thread and see if the issue persists?

 

Mark

Do you need immediate support?
If you encounter a critical issue and need immediate assistance please submit a Service Request through our Support Portal.