private GCHandle _ConvertTextureToGrayscaleBytes(AugmentedImageSrc imageSrc) { byte[] grayscaleBytes = null; if (imageSrc.Format == TextureFormat.RGB24 || imageSrc.Format == TextureFormat.RGBA32) { Color[] pixels = imageSrc.Pixels; grayscaleBytes = new byte[pixels.Length]; for (int i = 0; i < imageSrc.Height; i++) { int widthDelta = i * imageSrc.Width; for (int j = 0; j < imageSrc.Width; j++) { int pixelIndex = ((imageSrc.Height - 1 - i) * imageSrc.Width) + j; grayscaleBytes[widthDelta + j] = (byte)(( (0.213 * pixels[pixelIndex].r) + (0.715 * pixels[pixelIndex].g) + (0.072 * pixels[pixelIndex].b)) * 255); } } } else { Debug.LogError("Unsupported texture format " + imageSrc.Format); } return(GCHandle.Alloc(grayscaleBytes, GCHandleType.Pinned)); }
public int AddAugmentedImageAtRuntime(IntPtr augmentedImageDatabaseHandle, string name, AugmentedImageSrc imageSrc, float width) { int outIndex = -1; if (InstantPreviewManager.IsProvidingPlatform) { InstantPreviewManager.LogLimitedSupportMessage( "add images to Augmented Image database"); return(outIndex); } GCHandle grayscaleBytesHandle = _ConvertTextureToGrayscaleBytes(imageSrc); if (grayscaleBytesHandle.AddrOfPinnedObject() == IntPtr.Zero) { return(-1); } ApiArStatus status; if (width > 0) { status = ExternApi.ArAugmentedImageDatabase_addImageWithPhysicalSize( m_NativeSession.SessionHandle, augmentedImageDatabaseHandle, name, grayscaleBytesHandle.AddrOfPinnedObject(), imageSrc.Width, imageSrc.Height, imageSrc.Width, width, ref outIndex); } else { status = ExternApi.ArAugmentedImageDatabase_addImage( m_NativeSession.SessionHandle, augmentedImageDatabaseHandle, name, grayscaleBytesHandle.AddrOfPinnedObject(), imageSrc.Width, imageSrc.Height, imageSrc.Width, ref outIndex); } if (grayscaleBytesHandle.IsAllocated) { grayscaleBytesHandle.Free(); } if (status != ApiArStatus.Success) { Debug.LogWarningFormat( "Failed to add aumented image at runtime with status {0}", status); return(-1); } return(outIndex); }