forked from swharden/Spectrogram
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathImage.cs
More file actions
67 lines (55 loc) · 2.3 KB
/
Image.cs
File metadata and controls
67 lines (55 loc) · 2.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.Runtime.InteropServices;
using System.Text;
namespace Spectrogram
{
public static class Image
{
public static Bitmap Create(byte[,] pixelValues, int wrapIndex = 0)
{
int height = pixelValues.GetLength(1);
int width = pixelValues.GetLength(0);
Bitmap bmp = new Bitmap(width, height, PixelFormat.Format8bppIndexed);
var lockRect = new Rectangle(0, 0, width, height);
BitmapData bitmapData = bmp.LockBits(lockRect, ImageLockMode.ReadOnly, bmp.PixelFormat);
int stride = bitmapData.Stride;
byte[] bytes = new byte[bitmapData.Stride * height];
for (int col = 0; col < width; col++)
{
for (int row = 0; row < height; row++)
{
int bytePosition = (height - 1 - row) * stride + col;
bytes[bytePosition] = pixelValues[col, row];
}
}
Marshal.Copy(bytes, 0, bitmapData.Scan0, bytes.Length);
bmp.UnlockBits(bitmapData);
return bmp;
}
public static Bitmap CreateMax(byte[,] pixelValues, int wrapIndex = 0, int pxPerPx = 1)
{
int height = pixelValues.GetLength(1) / pxPerPx;
int width = pixelValues.GetLength(0);
Bitmap bmp = new Bitmap(width, height, PixelFormat.Format8bppIndexed);
var lockRect = new Rectangle(0, 0, width, height);
BitmapData bitmapData = bmp.LockBits(lockRect, ImageLockMode.ReadOnly, bmp.PixelFormat);
int stride = bitmapData.Stride;
byte[] bytes = new byte[bitmapData.Stride * height];
for (int col = 0; col < width; col++)
{
for (int row = 0; row < height; row++)
{
int bytePosition = (height - 1 - row) * stride + col;
for (int i = 0; i < pxPerPx; i++)
bytes[bytePosition] = Math.Max(bytes[bytePosition], pixelValues[col, row * pxPerPx + i]);
}
}
Marshal.Copy(bytes, 0, bitmapData.Scan0, bytes.Length);
bmp.UnlockBits(bitmapData);
return bmp;
}
}
}