Skip to content

Commit

Permalink
Merge branch 'master' of https://github.com/emgucv/emgutf
Browse files Browse the repository at this point in the history
  • Loading branch information
emgucv committed Jun 7, 2024
2 parents aaefcdb + 77297cb commit 4ec7675
Show file tree
Hide file tree
Showing 254 changed files with 1,704 additions and 82,116 deletions.
287 changes: 145 additions & 142 deletions CMakeLists.txt

Large diffs are not rendered by default.

7 changes: 7 additions & 0 deletions Emgu.Models/NativeImageIO.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,13 @@ namespace Emgu.Models
/// </summary>
public partial class NativeImageIO
{
/// <summary>
/// Scales the location of a rectangle in an image.
/// </summary>
/// <param name="location">An array of four float values representing the top left (x0, y0) and bottom right (x1, y1) corners of a rectangle. The values should be in the range of [0, 1].</param>
/// <param name="imageWidth">The width of the image.</param>
/// <param name="imageHeight">The height of the image.</param>
/// <returns>An array of four float values representing the scaled top left (x0, y0) and bottom right (x1, y1) corners of the rectangle in the image.</returns>
public static float[] ScaleLocation(float[] location, int imageWidth, int imageHeight)
{
float left = location[0] * imageWidth;
Expand Down
126 changes: 66 additions & 60 deletions Emgu.Models/NativeImageIO.iOS.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,66 +41,72 @@ public partial class NativeImageIO
bool swapBR = false)
where T : struct
{

if (inputHeight <= 0)
inputHeight = (int) image.Size.Height;
inputHeight = (int)image.Size.Height;

if (inputWidth <= 0)
inputWidth = (int)image.Size.Width;


int[] intValues = new int[inputWidth * inputHeight];
System.Runtime.InteropServices.GCHandle handle = System.Runtime.InteropServices.GCHandle.Alloc(intValues, System.Runtime.InteropServices.GCHandleType.Pinned);
using (CGImage cgimage = image.CGImage)
using (CGColorSpace cspace = CGColorSpace.CreateDeviceRGB())
using (CGBitmapContext context = new CGBitmapContext(

int[] intValues = new int[inputWidth * inputHeight];
System.Runtime.InteropServices.GCHandle handle = System.Runtime.InteropServices.GCHandle.Alloc(intValues, System.Runtime.InteropServices.GCHandleType.Pinned);
using (CGImage cgimage = image.CGImage)
using (CGColorSpace cspace = CGColorSpace.CreateDeviceRGB())
using (CGBitmapContext context = new CGBitmapContext(
handle.AddrOfPinnedObject(),
inputWidth,
inputHeight,
8,
inputWidth * 4,
cspace,
CGImageAlphaInfo.PremultipliedLast
))
{
context.DrawImage(new CGRect(new CGPoint(), new CGSize(inputWidth, inputHeight)), cgimage);

}

if (typeof(T) == typeof(float))
{
Emgu.TF.Util.Toolbox.Pixel32ToPixelFloat(
handle.AddrOfPinnedObject(),
inputWidth,
inputHeight,
8,
inputWidth * 4,
cspace,
CGImageAlphaInfo.PremultipliedLast
))
{
context.DrawImage(new CGRect(new CGPoint(), new CGSize(inputWidth, inputHeight)), cgimage);

}

if (typeof(T) == typeof(float))
{
Emgu.TF.Util.Toolbox.Pixel32ToPixelFloat(
handle.AddrOfPinnedObject(),
inputWidth,
inputHeight,
inputMean,
scale,
flipUpSideDown,
swapBR,
dest
);
}
else if (typeof(T) == typeof(byte))
{
Emgu.TF.Util.Toolbox.Pixel32ToPixelByte(
handle.AddrOfPinnedObject(),
inputWidth,
inputHeight,
inputMean,
scale,
flipUpSideDown,
swapBR,
dest
);
}
else
{
throw new NotImplementedException(String.Format("Destination data type {0} is not supported.", typeof(T).ToString()));
}
handle.Free();

inputMean,
scale,
flipUpSideDown,
swapBR,
dest
);
}
else if (typeof(T) == typeof(byte))
{
Emgu.TF.Util.Toolbox.Pixel32ToPixelByte(
handle.AddrOfPinnedObject(),
inputWidth,
inputHeight,
inputMean,
scale,
flipUpSideDown,
swapBR,
dest
);
}
else
{
throw new NotImplementedException(String.Format("Destination data type {0} is not supported.", typeof(T).ToString()));
}
handle.Free();

}

/// <summary>
/// Draws annotations on a given UIImage.
/// </summary>
/// <param name="uiimage">The UIImage to draw annotations on.</param>
/// <param name="annotations">An array of Annotation objects representing the annotations to be drawn.</param>
/// <returns>A new UIImage with the annotations drawn on it.</returns>
public static UIImage DrawAnnotations(UIImage uiimage, Annotation[] annotations)
{
UIGraphics.BeginImageContextWithOptions(uiimage.Size, false, 0);
Expand All @@ -117,10 +123,10 @@ public static UIImage DrawAnnotations(UIImage uiimage, Annotation[] annotations)
(int)uiimage.Size.Width,
(int)uiimage.Size.Height);
CGRect cgRect = new CGRect(
(nfloat)rects[0],
(nfloat)rects[1],
(nfloat)(rects[2] - rects[0]),
(nfloat)(rects[3] - rects[1]));
(NFloat)rects[0],
(NFloat)rects[1],
(NFloat)(rects[2] - rects[0]),
(NFloat)(rects[3] - rects[1]));
context.AddRect(cgRect);
context.DrawPath(CGPathDrawingMode.Stroke);
}
Expand All @@ -133,7 +139,7 @@ public static UIImage DrawAnnotations(UIImage uiimage, Annotation[] annotations)
(int)uiimage.Size.Width,
(int)uiimage.Size.Height);
context.SelectFont("Helvetica", 18, CGTextEncoding.MacRoman);
context.SetFillColor((nfloat)1.0, (nfloat)0.0, (nfloat)0.0, (nfloat)1.0);
context.SetFillColor((NFloat)1.0, (NFloat)0.0, (NFloat)0.0, (NFloat)1.0);
context.SetTextDrawingMode(CGTextDrawingMode.Fill);
context.ShowTextAtPoint(rects[0], uiimage.Size.Height - rects[1], annotations[i].Label);
}
Expand All @@ -146,16 +152,16 @@ public static UIImage DrawAnnotations(UIImage uiimage, Annotation[] annotations)
/// Read the file and draw rectangles on it.
/// </summary>
/// <param name="fileName">The name of the file.</param>
/// <param name="annotations">Annotations to be add to the image. Can consist of rectangles and labels</param>
/// <param name="annotations">Annotations to be added to the image. Can consist of rectangles and labels</param>
/// <returns>The image in Jpeg stream format</returns>
public static JpegData ImageFileToJpeg(String fileName, Annotation[] annotations = null)
{
UIImage uiimage = new UIImage(fileName);

UIImage imgWithRect = DrawAnnotations(uiimage, annotations);
var jpegData = imgWithRect.AsJPEG();
byte[] jpeg = new byte[jpegData.Length];
System.Runtime.InteropServices.Marshal.Copy(jpegData.Bytes, jpeg, 0, (int)jpegData.Length);
byte[] jpeg = new byte[jpegData.Length];
System.Runtime.InteropServices.Marshal.Copy(jpegData.Bytes, jpeg, 0, (int)jpegData.Length);
JpegData result = new JpegData();
result.Raw = jpeg;
result.Width = (int)uiimage.Size.Width;
Expand Down Expand Up @@ -215,10 +221,10 @@ public static byte[] PixelToJpeg(byte[] rawPixel, int width, int height, int cha
int inputHeight = -1,
int inputWidth = -1,
float inputMean = 0.0f,
float scale = 1.0f,
float scale = 1.0f,
bool flipUpSideDown = false,
bool swapBR = false)
where T: struct
where T : struct
{
if (!File.Exists(fileName))
throw new FileNotFoundException(String.Format("File {0} do not exist.", fileName));
Expand Down
Loading

0 comments on commit 4ec7675

Please sign in to comment.