;============================================================ ; iron_image_net.hsp — 画像処理 (.NET版) ; ; System.Drawing を使用。PNG/JPG/BMP/GIF の読み書き、 ; リサイズ、クロップ、回転、グレースケール変換に対応。 ; hsp3net 専用。 ; ; API: ; imgn_load "path" 画像読込 (stat=ハンドル) ; imgn_save handle, "path" 保存 (拡張子でフォーマット判別) ; imgn_resize handle, w, h リサイズ (Bicubic) ; imgn_crop handle, x, y, w, h クロップ ; imgn_rotate handle, degrees 回転 ; imgn_grayscale handle グレースケール変換 ; imgn_get_size handle, var_w, var_h サイズ取得 ; imgn_screenshot var_handle スクリーンショット ; imgn_close handle 解放 ;============================================================ #ifndef __iron_image_net_hsp__ #define __iron_image_net_hsp__ #module iron_image_net dim _imgn_cs_loaded, 1 #deffunc _imgn_load_cs if _imgn_cs_loaded : return sdim _cs, 16384 _cs = {" using System; using System.IO; using System.Drawing; using System.Drawing.Drawing2D; using System.Drawing.Imaging; using System.Collections.Generic; public class HspImageNet { static Dictionary images = new Dictionary(); static int nextId = 1; public static string Load(string path) { try { var bmp = new Bitmap(path); int id = nextId++; images[id] = bmp; return id.ToString(); } catch (Exception e) { return "-1\t" + e.Message; } } public static string Save(int handle, string path) { try { if (!images.ContainsKey(handle)) return "ERROR:invalid handle"; var bmp = images[handle]; string ext = Path.GetExtension(path).ToLower(); ImageFormat fmt = ImageFormat.Png; if (ext == ".jpg" || ext == ".jpeg") fmt = ImageFormat.Jpeg; else if (ext == ".bmp") fmt = ImageFormat.Bmp; else if (ext == ".gif") fmt = ImageFormat.Gif; bmp.Save(path, fmt); return "ok"; } catch (Exception e) { return "ERROR:" + e.Message; } } public static string Resize(int handle, int w, int h) { try { if (!images.ContainsKey(handle)) return "ERROR:invalid handle"; var src = images[handle]; var dst = new Bitmap(w, h); using (var g = Graphics.FromImage(dst)) { g.InterpolationMode = InterpolationMode.HighQualityBicubic; g.SmoothingMode = SmoothingMode.HighQuality; g.DrawImage(src, 0, 0, w, h); } src.Dispose(); images[handle] = dst; return "ok"; } catch (Exception e) { return "ERROR:" + e.Message; } } public static string Crop(int handle, int x, int y, int w, int h) { try { if (!images.ContainsKey(handle)) return "ERROR:invalid handle"; var src = images[handle]; var rect = new Rectangle(x, y, w, h); var dst = src.Clone(rect, src.PixelFormat); src.Dispose(); images[handle] = dst; return "ok"; } catch (Exception e) { return "ERROR:" + e.Message; } } public static string Rotate(int handle, int degrees) { try { if (!images.ContainsKey(handle)) return "ERROR:invalid handle"; var bmp = images[handle]; switch (degrees % 360) { case 90: case -270: bmp.RotateFlip(RotateFlipType.Rotate90FlipNone); break; case 180: case -180: bmp.RotateFlip(RotateFlipType.Rotate180FlipNone); break; case 270: case -90: bmp.RotateFlip(RotateFlipType.Rotate270FlipNone); break; } return "ok"; } catch (Exception e) { return "ERROR:" + e.Message; } } public static string Grayscale(int handle) { try { if (!images.ContainsKey(handle)) return "ERROR:invalid handle"; var src = images[handle]; var dst = new Bitmap(src.Width, src.Height); using (var g = Graphics.FromImage(dst)) { var cm = new ColorMatrix(new float[][] { new float[] {0.299f, 0.299f, 0.299f, 0, 0}, new float[] {0.587f, 0.587f, 0.587f, 0, 0}, new float[] {0.114f, 0.114f, 0.114f, 0, 0}, new float[] {0, 0, 0, 1, 0}, new float[] {0, 0, 0, 0, 1} }); var attr = new ImageAttributes(); attr.SetColorMatrix(cm); g.DrawImage(src, new Rectangle(0, 0, src.Width, src.Height), 0, 0, src.Width, src.Height, GraphicsUnit.Pixel, attr); } src.Dispose(); images[handle] = dst; return "ok"; } catch (Exception e) { return "ERROR:" + e.Message; } } public static string GetSize(int handle) { try { if (!images.ContainsKey(handle)) return "ERROR:invalid handle"; var bmp = images[handle]; return bmp.Width + "\t" + bmp.Height; } catch (Exception e) { return "ERROR:" + e.Message; } } public static string Screenshot() { try { var bounds = System.Windows.Forms.Screen.PrimaryScreen.Bounds; var bmp = new Bitmap(bounds.Width, bounds.Height); using (var g = Graphics.FromImage(bmp)) { g.CopyFromScreen(bounds.Location, Point.Empty, bounds.Size); } int id = nextId++; images[id] = bmp; return id.ToString(); } catch (Exception e) { return "-1\t" + e.Message; } } public static string Close(int handle) { try { if (images.ContainsKey(handle)) { images[handle].Dispose(); images.Remove(handle); } return "ok"; } catch (Exception e) { return "ERROR:" + e.Message; } } } "} loadnet _cs, 3, "System.Drawing.dll\nSystem.Windows.Forms.dll" _imgn_cs_loaded = 1 return ;------------------------------------------------------------ ; imgn_load "path" — 画像読込 (stat=ハンドル) ;------------------------------------------------------------ #deffunc imgn_load str path, \ local _h, local _r, local _s _imgn_load_cs newnet _h, "HspImageNet" mcall _h, "Load", _r, path _s = "" + _r if instr(_s, 0, "\t") >= 0 : return -1 return int(_s) ;------------------------------------------------------------ ; imgn_save handle, "path" — 保存 ;------------------------------------------------------------ #deffunc imgn_save int handle, str path, \ local _h, local _r _imgn_load_cs newnet _h, "HspImageNet" mcall _h, "Save", _r, handle, path if instr("" + _r, 0, "ERROR") >= 0 : return 1 return 0 ;------------------------------------------------------------ ; imgn_resize handle, w, h — リサイズ ;------------------------------------------------------------ #deffunc imgn_resize int handle, int w, int h, \ local _h, local _r _imgn_load_cs newnet _h, "HspImageNet" mcall _h, "Resize", _r, handle, w, h if instr("" + _r, 0, "ERROR") >= 0 : return 1 return 0 ;------------------------------------------------------------ ; imgn_crop handle, x, y, w, h — クロップ ;------------------------------------------------------------ #deffunc imgn_crop int handle, int x, int y, int w, int h, \ local _h, local _r _imgn_load_cs newnet _h, "HspImageNet" mcall _h, "Crop", _r, handle, x, y, w, h if instr("" + _r, 0, "ERROR") >= 0 : return 1 return 0 ;------------------------------------------------------------ ; imgn_rotate handle, degrees — 回転 ;------------------------------------------------------------ #deffunc imgn_rotate int handle, int degrees, \ local _h, local _r _imgn_load_cs newnet _h, "HspImageNet" mcall _h, "Rotate", _r, handle, degrees if instr("" + _r, 0, "ERROR") >= 0 : return 1 return 0 ;------------------------------------------------------------ ; imgn_grayscale handle — グレースケール変換 ;------------------------------------------------------------ #deffunc imgn_grayscale int handle, \ local _h, local _r _imgn_load_cs newnet _h, "HspImageNet" mcall _h, "Grayscale", _r, handle if instr("" + _r, 0, "ERROR") >= 0 : return 1 return 0 ;------------------------------------------------------------ ; imgn_get_size handle, var_w, var_h — サイズ取得 ;------------------------------------------------------------ #deffunc imgn_get_size int handle, var v_w, var v_h, \ local _h, local _r, local _s, local _tab _imgn_load_cs newnet _h, "HspImageNet" mcall _h, "GetSize", _r, handle _s = "" + _r if instr(_s, 0, "ERROR") >= 0 : return 1 _tab = instr(_s, 0, "\t") v_w = int(strmid(_s, 0, _tab)) v_h = int(strmid(_s, _tab + 1, strlen(_s) - _tab - 1)) return 0 ;------------------------------------------------------------ ; imgn_screenshot var_handle — スクリーンショット ;------------------------------------------------------------ #deffunc imgn_screenshot var v_handle, \ local _h, local _r, local _s _imgn_load_cs newnet _h, "HspImageNet" mcall _h, "Screenshot", _r _s = "" + _r if instr(_s, 0, "\t") >= 0 { v_handle = -1 return 1 } v_handle = int(_s) return 0 ;------------------------------------------------------------ ; imgn_close handle — 解放 ;------------------------------------------------------------ #deffunc imgn_close int handle, \ local _h, local _r _imgn_load_cs newnet _h, "HspImageNet" mcall _h, "Close", _r, handle return 0 #global #endif