@@ -104,35 +104,31 @@ public class ImageUtil
104104 /// <param name="height">height</param>
105105 /// <param name="dstWidth">dst width</param>
106106 /// <param name="dstHeight">dst height</param>
107+ /// <param name="quality">jpeg quality</param>
107108 /// <returns></returns>
108- public static byte [ ] Raw2Jpeg ( byte [ ] raw , int bytesPerPixel , int bytesPerLine , int width , int height , int dstWidth = 0 , int dstHeight = 0 )
109+ public static unsafe byte [ ] Raw2Jpeg ( byte [ ] raw , int bytesPerPixel , int bytesPerLine , int width , int height , int dstWidth = 0 , int dstHeight = 0 , int ? quality = null )
109110 {
110111 if ( raw == null ) return null ;
111112 if ( bytesPerPixel != 3 && bytesPerPixel != 4 ) throw new ArgumentException ( nameof ( bytesPerPixel ) ) ;
112113 var pixel = bytesPerPixel == 3 ? PixelFormat . Format24bppRgb : PixelFormat . Format32bppRgb ;
113- using ( var bmp = new Bitmap ( width , height ) )
114+ fixed ( byte * scan0 = raw )
114115 {
115- var data = bmp . LockBits ( new Rectangle ( 0 , 0 , width , height ) , ImageLockMode . WriteOnly , pixel ) ;
116- var lineSize = Math . Min ( bytesPerLine , data . Stride ) ;
117- for ( int i = 0 ; i < height ; i ++ )
116+ using ( var bmp = new Bitmap ( width , height , bytesPerLine , pixel , ( IntPtr ) scan0 ) )
118117 {
119- Marshal . Copy ( raw , bytesPerLine * i , data . Scan0 + data . Stride * i , lineSize ) ;
120- }
121- bmp . UnlockBits ( data ) ;
122118
123- // fill
124- if ( ( dstWidth <= 0 && dstHeight <= 0 ) || ( dstWidth == width && dstHeight == height ) )
125- {
126- return bmp . ToArray ( ImageFormat . Jpeg ) ;
127- }
128- else
129- {
130- using ( var dstImage = new Bitmap ( dstWidth , dstHeight ) )
131- using ( var g = Graphics . FromImage ( dstImage ) )
119+ if ( ( dstWidth <= 0 && dstHeight <= 0 ) || ( dstWidth == width && dstHeight == height ) )
132120 {
133- g . Clear ( Color . White ) ;
134- g . DrawImage ( bmp , new Point ( 0 , 0 ) ) ;
135- return dstImage . ToArray ( ImageFormat . Jpeg ) ;
121+ return bmp . ToArray ( ImageFormat . Jpeg , quality ) ;
122+ }
123+ else // fill
124+ {
125+ using ( var dstImage = new Bitmap ( dstWidth , dstHeight ) )
126+ using ( var g = Graphics . FromImage ( dstImage ) )
127+ {
128+ g . Clear ( Color . White ) ;
129+ g . DrawImage ( bmp , new Point ( 0 , 0 ) ) ;
130+ return dstImage . ToArray ( ImageFormat . Jpeg , quality ) ;
131+ }
136132 }
137133 }
138134 }
@@ -142,17 +138,7 @@ public static BgraData Jpeg2Bgra(byte[] jpeg)
142138 {
143139 return new BgraData ( jpeg , false ) ;
144140 }
145-
146- public static BgraData Jpeg2Bgra2 ( byte [ ] jpeg )
147- {
148- using ( var mat = Mat . ImDecode ( jpeg , ImreadModes . Color ) )
149- {
150- byte [ ] bgra = new byte [ mat . DataEnd . ToInt64 ( ) - mat . DataStart . ToInt64 ( ) ] ;
151- Marshal . Copy ( mat . Data , bgra , 0 , bgra . Length ) ;
152- return new BgraData ( mat . Width , mat . Height , ( int ) mat . Step ( ) , bgra , mat . Channels ( ) ) ;
153- }
154- }
155-
141+
156142 /// <summary>
157143 /// Join by <paramref name="srcPixelTiles"/> and cut by <paramref name="srcPixelExtent"/> then scale to <paramref name="dstPixelExtent"/>(only height an width is useful).
158144 /// </summary>
0 commit comments