Advertisement
DestBro

Img2hex.nc

Dec 17th, 2016
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.55 KB | None | 0 0
  1. import base.win;
  2. import base.gui;
  3. import std.file;
  4. import "./FlashEx_img.nc";
  5. Dialog ref hDlg;
  6. EditBox hEditIn;
  7. EditBox hEditOut;
  8.  
  9. //Скрипт переводит картинку в текст для вставки на форму
  10. //фильт файлов настроен на jpg но можно png, icon (с альфа каналом)
  11.  
  12.  
  13. char[] ToHex(int n)
  14. {
  15. char[8] r;
  16. for(int i = 0; i < 8; i++)
  17. {
  18. int c = n&0xF;
  19. n >>= 4;
  20. if(c < 10)
  21. r[7-i] = c+'0';
  22. else
  23. r[7-i] = c + 'A'-10;
  24. }
  25. return r;
  26. }
  27.  
  28. void btnConvert(Dialog ref dlg, int param)
  29. {
  30. File fileIn, fileOut;
  31. fileIn.Open(hEditIn.GetText(), "rb");
  32. if(!fileIn.Opened())
  33. {
  34. dlg.Msg("Open image file", "Error");
  35. return;
  36. }
  37. fileOut.Open(hEditOut.GetText(), "w+");
  38. if(!fileOut.Opened())
  39. {
  40. fileIn.Close();
  41. dlg.GetHWND().MessageBox("create out file", "Error", 0x10/*MB_ICONERROR*/);
  42. return;
  43. }
  44. long size = fileIn.Size();
  45. fileOut.Print("int[] imgData = {\n 0x");
  46. int d4, endFor = size>>2;
  47. for(int i = 0; i < endFor; i++)
  48. {
  49. if(i)
  50. {
  51. if(i&7)
  52. fileOut.Print(", 0x");
  53. else
  54. fileOut.Print(",\n 0x");
  55. }
  56. fileIn.Read(d4);
  57. fileOut.Print(ToHex(d4));
  58. }
  59. d4 = 0;
  60. if(size%4)
  61. {
  62. fileIn.Read(d4);
  63. fileOut.Print(", 0x");
  64. fileOut.Print(ToHex(d4));
  65. }
  66. fileOut.Print("\n};\n");
  67. fileOut.Close();
  68. fileIn.Close();
  69. }
  70.  
  71.  
  72.  
  73. void btnSelectImage(Dialog ref dlg, int param)
  74. {
  75. char[] nameFile;
  76. HWND wnd = dlg.GetHWND();
  77. if(wnd.OpenFile(nameFile, "*.jpg"))
  78. {
  79. hEditIn.SetText(nameFile);
  80. }
  81. }
  82.  
  83. void btnSaveImage(Dialog ref dlg, int param)
  84. {
  85. char[] nameFile;
  86. if(dlg.GetHWND().SaveFile(nameFile, "*.nc"))
  87. hEditOut.SetText(nameFile);
  88. }
  89.  
  90. void closeDlg(Dialog ref Dialog, int param){}
  91. hDlg = CreateDialog("Img2hex.nc", 200, 200, 450, 300, closeDlg);
  92. hDlg.AddStatic("image file:", 5,8, 55, 20);
  93. hDlg.AddStatic("out file:", 15,35, 55, 20);
  94. hEditIn = hDlg.AddEdit("", 60,5, 300, 20);
  95. hEditOut = hDlg.AddEdit("", 60,30, 300, 20);
  96. hDlg.AddButton("...", 365, 5, 70, 20, btnSelectImage, 0);
  97. hDlg.AddButton("...", 365, 30, 70, 20, btnSaveImage, 0);
  98. hDlg.AddButton("Converting", 10, 55, 425, 25, btnConvert, 0);
  99. hDlg.AddImage(imgData, 200, 150);
  100. hEditIn.ReadOnly(true);
  101. hEditOut.ReadOnly(true);
  102. hDlg.Show(true);
  103. hDlg.WaitClose();
  104. return 0;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement