PageRenderTime 39ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

/TugasAkhir1/Scramble.cs

https://gitlab.com/MFathirIrhas/TugasAkhir1
C# | 450 lines | 336 code | 58 blank | 56 comment | 44 complexity | 48cda6838582c9d622c654a38780e3ec MD5 | raw file
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.IO;
  7. using System.Drawing;
  8. using System.Drawing.Imaging;
  9. namespace TugasAkhir1
  10. {
  11. /**
  12. * This class was used to do some matrix manipulation
  13. * 1. ConvertToVectorMatrix Class
  14. * 2. ConvertToBinaryVectorMatrix Class
  15. * 3. 1/3 Convolution Code
  16. * 4. Direct Sequence Spread Spectrum
  17. * 5. Interleaving Sequence
  18. * 6. Segment
  19. * */
  20. public class Scramble
  21. {
  22. //Global Variables
  23. static int[] pnseed = new int[5];
  24. #region 1. ConvertToVectorMatrix
  25. //Convert to 1 Dimensional Matrix fill with black and white value
  26. public static List<int> ConvertToVectorMatrix(Bitmap bmp)
  27. {
  28. List<int> m = new List<int>();
  29. int width = bmp.Width;
  30. int height = bmp.Height;
  31. for (int y = 0; y < height; y++)
  32. {
  33. for (int x = 0; x < width; x++)
  34. {
  35. Color c = bmp.GetPixel(x, y);
  36. int p = (c.R+c.G+c.B)/3;
  37. m.Add(p);
  38. }
  39. }
  40. return m;
  41. }
  42. #endregion
  43. #region 2. ConvertToBinaryVectorMatrix
  44. //Convert to 0 or 1 bit sequence , 1 denote to white space and 0 denote to black space
  45. public static List<int> ConvertToBinaryVectorMatrix(List<int> vm)
  46. {
  47. List<int> bvm = new List<int>();
  48. foreach (int i in vm)
  49. {
  50. if (i >= 250)
  51. {
  52. int b = 1;
  53. bvm.Add(b);
  54. }
  55. else if (i <= 0)
  56. {
  57. int b = 0;
  58. bvm.Add(b);
  59. }
  60. }
  61. return bvm;
  62. }
  63. #endregion
  64. #region 3. 1/3 Convolution Code
  65. /**
  66. * 1/3 Convolutional Code
  67. * k = 1 , Number of input each time process
  68. * n = 3 , number of output each time after process
  69. * m = input data
  70. * Generator Polynomial use:
  71. * g1 = { 1, 1, 1, 1, 0, 1, 1, 1 }
  72. * g2 = { 1, 1, 0, 1, 1, 0, 0, 1 }
  73. * g3 = { 1, 0, 0, 1, 0, 1, 0, 1 }
  74. **/
  75. public static List<int> ConvolutionEncoding(List<int> m)
  76. {
  77. List<int> mc = new List<int>(); //Output list
  78. int elm = 0;
  79. int v = 3; //1/3 rate with 3 output at a time.
  80. //memory register
  81. int[] reg = new int[m.Count];
  82. //Generator Polynomial
  83. int[,] g = new int[,] { { 1, 1, 1, 1, 0, 1, 1, 1 }, //g0
  84. { 1, 1, 0, 1, 1, 0, 0, 1 }, //g1
  85. { 1, 0, 0, 1, 0, 1, 0, 1 } }; //g2
  86. for (int n = 0; n < m.Count; n++)
  87. {
  88. for (int i = 0; i < v/*3*/; i++)
  89. {
  90. for(int j = 0; j < 8; j++)
  91. {
  92. int l = n - j;
  93. if (l < 0)
  94. {
  95. elm += g[i,j] * 0; //x[n] = 0 , if n = negative
  96. }
  97. else
  98. {
  99. elm += g[i, j] * m[l];
  100. }
  101. }
  102. int item = elm % 2;
  103. mc.Add(item);
  104. }
  105. }
  106. return mc;
  107. }
  108. #endregion
  109. #region 4. Direct Sequence Spread Spectrum
  110. /* *
  111. * Direct Sequence Spread Spectrum
  112. * convert the input binary sequence into sequence with values 1 or 0
  113. * 1. Input bit is a binary bit from 1/3 Convolutional Code
  114. * 2. The input bit is generated using a PN Sequence
  115. * 3. PN Sequence is generated using a key
  116. * 4. The result will be a sequence contain values 1 and -1 with length = input bit length * PN Sequence length
  117. * */
  118. public static List<int> DSSS(List<int> mc, List<int> PNSeq)
  119. {
  120. List<int> dsss = new List<int>();
  121. //int pnlength = mc.Count * 4;
  122. ////List<int> PNSeq = PNSeqGenerate(pnlength); //Pseudonoise sequence generated randomly using pseudorandom sequence
  123. //string pn_seed = "1000"; //Secret Key K
  124. //string pn_mask = "1010";
  125. //int pn_length = pnlength;
  126. //List<int> PNSeq = PNSeqLFSR(pn_seed, pn_mask, pn_length);
  127. int k = 0;
  128. for (int i = 0; i < mc.Count; i++) //Looping for input data
  129. {
  130. while(k < (i+1)*5) // Looping for PN sequence, each bit in input data is attach to 5 PN bit sequence
  131. {
  132. if (mc[i] == 0)
  133. {
  134. int xor = mc[i] ^ PNSeq[k];
  135. dsss.Add(xor);
  136. }
  137. else
  138. {
  139. int xor = mc[i] ^ PNSeq[k];
  140. dsss.Add(xor);
  141. }
  142. k = k + 1;
  143. }
  144. }
  145. return dsss;
  146. }
  147. //Generate PNSequence using LFSR(Linear Feedback Shift Register)
  148. public static List<int> PNSeqLFSR(string seed, string mask, int length)
  149. {
  150. List<int> pnseq = new List<int>();
  151. //Initialize shift register with the pn_seed
  152. for (int i = 0; i < seed.Length; i++)
  153. {
  154. pnseed[i] = (int)Char.GetNumericValue(seed[i]);
  155. }
  156. int[] key = pnseed; //key = sr
  157. for (int i = 0; i < length; i++)
  158. {
  159. int new_bit = 0;
  160. for (int j = 0; j < 5; j++)
  161. {
  162. if ((int)Char.GetNumericValue(mask[j]) == 1)
  163. new_bit = new_bit ^ key[j];
  164. }
  165. pnseq.Add(key[5 - 1]);
  166. key = Roll(key);
  167. key[0] = new_bit;
  168. }
  169. return pnseq;
  170. }
  171. //Shift pnseed to the right
  172. public static int[] Roll(int[] key)
  173. {
  174. int[] ShiftedKey = new int[key.Length];
  175. for (int i = 0; i < key.Length; i++)
  176. {
  177. ShiftedKey[(i + 1) % ShiftedKey.Length] = key[i];
  178. }
  179. return ShiftedKey;
  180. }
  181. public List<int> PNSeqGenerate(int l)
  182. {
  183. List<int> pn = new List<int>();
  184. Random rnd = new Random();
  185. for (int i = 0; i < l; i++)
  186. {
  187. pn.Add(rnd.Next(2));
  188. }
  189. return pn;
  190. }
  191. #endregion
  192. #region 5. Interleaving Sequence
  193. public static List<int> Interleaving(List<int> dsss)
  194. {
  195. List<int> il = new List<int>();
  196. var ds3 = dsss;
  197. List<int> ySeq = GenerateRandomBinarySeq(ds3.Count);
  198. //dsss.AddRange(ySeq);
  199. //var InterLength = dsss.Count * 2;
  200. //int k1 = 0;
  201. //int k2 = InterLength / 2;
  202. while (ds3.Count > 0 && ySeq.Count > 0)
  203. {
  204. if (ds3.Count > 0)
  205. {
  206. il.Add(ds3[0]);
  207. ds3.RemoveAt(0);
  208. }
  209. if (ySeq.Count > 0)
  210. {
  211. il.Add(ySeq[0]);
  212. ySeq.RemoveAt(0);
  213. }
  214. }
  215. return il;
  216. }
  217. public static List<int> GenerateRandomBinarySeq(int length) //generate random binary sequence for Interleaving sequence
  218. {
  219. var randBin = new List<int>();
  220. Random rand = new Random();
  221. for (int i = 0; i < length; i++)
  222. {
  223. if (rand.Next() % 2 == 0)
  224. {
  225. randBin.Add(0);
  226. }
  227. else
  228. {
  229. randBin.Add(1);
  230. }
  231. }
  232. randBin.Sort();
  233. return randBin;
  234. }
  235. #endregion
  236. #region 6. Segmentation
  237. //Group the Interleaved sequence into M-bit segments. M = 5.
  238. public static List<List<int>> Segment(List<int> Interleaved)
  239. {
  240. List<List<int>> Tree = new List<List<int>>();
  241. List<int> tree_th = new List<int>();
  242. //Get total number of trees
  243. double t = Interleaved.Count / 5; //5 didapat dari jumlah node dalam 1 pohon HMM, 3 parents dan 12 anak-nya untuk setiap scale.
  244. int nSize =(int)Math.Floor(t);
  245. double s = Interleaved.Count / nSize;
  246. int segSize = (int)s;
  247. for (int i = 0; i < Interleaved.Count; i += segSize)
  248. {
  249. Tree.Add(Interleaved.GetRange(i, Math.Min(segSize, Interleaved.Count - i)));
  250. }
  251. return Tree;
  252. }
  253. #endregion
  254. #region 7. Mapping the Scrambled Watermark
  255. public static double[,] Mapping(List<List<int>> SegmentedWatermark)
  256. {
  257. double[,] MappedWatermark = new double[SegmentedWatermark.Count,15];
  258. for (int i = 0; i < SegmentedWatermark.Count; i++)
  259. {
  260. int[] repeated_version = SegmentedWatermark[i].ToArray();
  261. int[] inversed_version = new int[SegmentedWatermark[i].Count];
  262. for (int j = 0; j < SegmentedWatermark[i].Count; j++)
  263. {
  264. if (SegmentedWatermark[i][j] == 0)
  265. {
  266. inversed_version[j] = 1;
  267. }
  268. else
  269. {
  270. inversed_version[j] = 0;
  271. }
  272. }
  273. //Watermark Mapping
  274. //Horizontal -> LH : Level 1 and 2
  275. MappedWatermark[i,0] = repeated_version[0]; //Triangle #0
  276. MappedWatermark[i,1] = repeated_version[3]; //Triangle #3
  277. MappedWatermark[i,2] = inversed_version[1]; //Square #1
  278. MappedWatermark[i,3] = inversed_version[4]; //Square #4
  279. MappedWatermark[i,4] = repeated_version[2]; //Circle #2
  280. //Diagonal -> HH : Level 1 and 2
  281. MappedWatermark[i,5] = repeated_version[1]; //Triangle #1
  282. MappedWatermark[i,6] = repeated_version[4]; //Triangle #4
  283. MappedWatermark[i,7] = inversed_version[2]; //Square #2
  284. MappedWatermark[i,8] = repeated_version[0]; //Circle #0
  285. MappedWatermark[i,9] = repeated_version[3]; //Circle #3
  286. //Vertical -> HL : Level 1 and 2
  287. MappedWatermark[i,10] = repeated_version[2]; //Triangle #2
  288. MappedWatermark[i,11] = inversed_version[0]; //Square #0
  289. MappedWatermark[i,12] = inversed_version[3]; //Square #3
  290. MappedWatermark[i,13] = repeated_version[1]; //Circle #1
  291. MappedWatermark[i,14] = repeated_version[4]; //Circle #4
  292. }
  293. return MappedWatermark;
  294. }
  295. #endregion
  296. #region Mapping2 the scrambled watermark
  297. public static double[,] Mapping2(List<List<int>> SegmentedWatermark)
  298. {
  299. double[,] MappedWatermark = new double[SegmentedWatermark.Count, 5];
  300. for(int i = 0; i < MappedWatermark.GetLength(0); i++)
  301. {
  302. for(int j = 0; j < MappedWatermark.GetLength(1); j++)
  303. {
  304. MappedWatermark[i, j] = SegmentedWatermark[i][j];
  305. }
  306. }
  307. return MappedWatermark;
  308. }
  309. #endregion
  310. #region 8. Inverse
  311. /// <summary>
  312. /// Merge the 5-segment watermark.
  313. /// </summary>
  314. /// <param name="inversedMapping"></param>
  315. /// <returns></returns>
  316. public static List<double> MergeSegmentedWatermark(double[,] inversedMapping)
  317. {
  318. List<double> ScrambledWatermark = new List<double>();
  319. for(int i = 0; i < inversedMapping.GetLength(0); i++)
  320. {
  321. for(int j = 0; j < inversedMapping.GetLength(1); j++)
  322. {
  323. ScrambledWatermark.Add(inversedMapping[i, j]);
  324. }
  325. }
  326. return ScrambledWatermark;
  327. }
  328. public static List<double> MergeSegmentedWatermark2(double[][] inversedMapping)
  329. {
  330. List<double> ScrambledWatermark = new List<double>();
  331. for (int i = 0; i < inversedMapping.GetLength(0); i++)
  332. {
  333. for (int j = 0; j < inversedMapping[i].Length; j++)
  334. {
  335. ScrambledWatermark.Add(inversedMapping[i][j]);
  336. }
  337. }
  338. return ScrambledWatermark;
  339. }
  340. public static List<int> InverseDSSS(List<double> ScrambledWatermark, List<int> PNSeq)
  341. {
  342. List<int> InversedDSSS = new List<int>();
  343. List<int> sWatermark = ScrambledWatermark.ConvertAll(Convert.ToInt32); //Convert list of double into list of integers
  344. List<int> xorList = new List<int>();
  345. List<int> RealWatermark = new List<int>();
  346. for(int i = 0; i < sWatermark.Count; i++)
  347. {
  348. xorList.Add(sWatermark[i] ^ PNSeq[i]);
  349. }
  350. List<List<int>> segmentedXOR = SplitList(xorList, 5);
  351. for(int i = 0; i < segmentedXOR.Count; i++)
  352. {
  353. RealWatermark.Add(ReturnMaxItem(segmentedXOR[i]));
  354. }
  355. return RealWatermark;
  356. }
  357. public static List<List<int>> SplitList(List<int> inputlist, int nSize)
  358. {
  359. var list = new List<List<int>>();
  360. for (int i = 0; i < inputlist.Count; i += nSize)
  361. {
  362. list.Add(inputlist.GetRange(i, Math.Min(nSize, inputlist.Count - i)));
  363. }
  364. return list;
  365. }
  366. public static int ReturnMaxItem(List<int> list)
  367. {
  368. int maxRepeated = list.GroupBy(s => s)
  369. .OrderByDescending(s => s.Count())
  370. .First().Key;
  371. return maxRepeated;
  372. }
  373. #endregion
  374. #region Convert to 1 and -1 watermark value
  375. public static List<int> ConvertTo1minus1(List<int> BinaryVectorImage)
  376. {
  377. List<int> oneminusone = new List<int>();
  378. for(int i = 0; i < BinaryVectorImage.Count; i++)
  379. {
  380. if (BinaryVectorImage[i] == 1)
  381. {
  382. oneminusone.Add(1);
  383. }else
  384. {
  385. oneminusone.Add(-1);
  386. }
  387. }
  388. return oneminusone;
  389. }
  390. #endregion
  391. }
  392. }