Advertisement
Guest User

Untitled

a guest
Aug 13th, 2013
554
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.75 KB | None | 0 0
  1. Begin["Pi`"];
  2.  
  3. Pi`B = {};
  4. Pi`NotebookOpen[c_] := NotebookPut[Notebook[{Cell[c, "Input"]}]];
  5.  
  6. Pi`GraphicsToRGBA[g_, size_: Automatic] := (
  7. Image[Image[Rasterize[
  8. Append[g, Method -> {"ShrinkWrap" -> True}]
  9. , "Image", Background -> None, ImageSize -> size], "Byte",
  10. ColorSpace -> "RGB", ImageSize -> size]
  11. ~ColorSeparate~"RGBA"~ColorCombine~"RGB"
  12. , "Byte", ColorSpace -> "RGB", ImageSize -> size]
  13. );
  14. Pi`ImageToRGBA[i_, size_: Automatic] := Image[
  15. Rasterize[
  16. Image[i, ImageSize -> size]
  17. , Background -> None, ImageSize -> size]~ColorSeparate~"RGBA"~
  18. ColorCombine~"RGB"
  19. , "Byte", ColorSpace -> "RGB"];
  20.  
  21. Pi`ToRGBA[i_] :=
  22. If[Head@i === Graphics,
  23. Pi`GraphicsToRGBA[i]
  24. , If[Head@i === Image,
  25. Pi`ImageToRGBA[i],
  26. Throw["must be image or graphics"]
  27. ]
  28. ];
  29.  
  30. FromSVG[xml_, id_] := ImportString[StringReplace[
  31. Replace[
  32. Cases[xml,
  33. XMLElement[
  34. "image", {___,
  35. "id" -> id, {"http://www.w3.org/1999/xlink", "href"} ->
  36. c_, ___}, {}]
  37. , \[Infinity]][[1, 2]]
  38. , {___, {"http://www.w3.org/1999/xlink", "href"} -> c_, ___} -> c
  39. ], "data:image/png;base64," -> ""], "Base64"];
  40.  
  41. AddExp[image_, box_] := (
  42. Pi`AddText[image, ToString[box]]
  43. );
  44. GetExp[image_] := (
  45. ToExpression[Pi`GetText@image]
  46. );
  47. AddText[carrierImage_Image, mesg_String] :=
  48. Block[{carrierData, truncatedCarrier, pixelChannels, secretBits},
  49. carrierData = ImageData[carrierImage, "Byte"];
  50. (*Number of places to hide data*)
  51. pixelChannels = Apply[Times, Dimensions[carrierData]];
  52. (*Convert message to bit stream*)
  53. secretBits = PadRight[Flatten[IntegerDigits[
  54. ToCharacterCode[CreateStringHash[mesg] <> mesg]
  55. , 2, 8]], pixelChannels];
  56. (* Discard ONE least significant bit *)
  57. truncatedCarrier = BitAnd[carrierData, 2^^11111110];
  58. Image[truncatedCarrier +
  59. (*Reshape the secret bits data*)
  60. Fold[Partition, secretBits,
  61. Reverse@Rest[Dimensions[carrierData]]], "Byte",
  62. ColorSpace -> "RGB"
  63. ]];
  64. GetText[img_Image] := Block[{secretData},
  65. (*Get the least significant bit of every pixel channel*)
  66. secretData = BitAnd[ImageData[img, "Byte"], 1];
  67. (*Group into 8 bit words*)
  68. secretData = Partition[Flatten[secretData], 8];
  69. (*Convert each binary word to decimal, and then to characters*)
  70. secretData = FromCharacterCode[FromDigits[#, 2] & /@ secretData];
  71. (*Discard large blocks of trailing character zeros*)
  72. secretData = StringSplit[
  73. FromCharacterCode@Select[ToCharacterCode@secretData, (# != 0) &],
  74. ""];
  75. If[ CreateStringHash[StringJoin@secretData[[11 ;;]]] ==
  76. StringJoin@secretData[[1 ;; 10]],
  77. StringJoin@secretData[[11 ;;]], $Failed]
  78. ];
  79. CreateStringHash[s_String] := StringJoin@PadLeft[
  80. StringSplit[IntegerString[Hash[s, "CRC32"]], ""], 10, "0"];
  81. a = "";
  82.  
  83. Print@{ Button["File",
  84. NotebookWrite[InputNotebook[], "\"" <>
  85. StringReplace[SystemDialogInput["FileSave"], "\\" -> "\\\\"]
  86. <> "\""];
  87. ], Button["BoxData", Module[{f, nb, i},
  88. Pi`B = Append[Pi`B, "temp"];
  89. i = Length[Pi`B];
  90. NotebookWrite[InputNotebook[], "Pi`B[[" <> ToString[i] <> "]]"];
  91. f = ToExpression[
  92. "Function[Pi`B[[" <> ToString[i] <>
  93. "]]=ToString@FullForm@Cases[NotebookGet[InputNotebook[]][[1]]
  94. , Cell[___, CellTags -> \"MyCell\", ___]
  95. , {1}][[1, 1]];
  96. NotebookClose[EvaluationNotebook[]];
  97. ]"];
  98. nb = Notebook[{Cell[
  99. BoxData[" "], "Input", Evaluatable -> True,
  100. CellEvaluationFunction -> f, CellTags -> "MyCell"]
  101. }];
  102. (* NotebookPut[nb]; *)
  103. NotebookFind[nb, " "];
  104. SetSelectedNotebook[nb];
  105. ]]};
  106. Print@Prepend[Map[
  107. Function[Button[#[[1]],
  108. CellPrint[Cell[BoxData@
  109. Replace[
  110. ToBoxes@#[[2]], TagBox[t___, HoldForm] -> t
  111. ]
  112. , "Input"]]]], {
  113. "Export[file]" -> HoldForm[
  114. Export[Placeholder[file],
  115. Pi`AddText[
  116. Pi`ToRGBA[Placeholder[grOrImage], Placeholder[size]],
  117. Placeholder[BoxData]]
  118. ]
  119. ]
  120. (* ,"Clipboard[..image,BoxData..]"-> HoldForm@
  121. Pi`Clipboard[Pi`AddExp[Placeholder[image],Placeholder[
  122. BoxData]]] *)
  123. }
  124. ], "Write To"];
  125. Print@Prepend[Map[
  126. Function[Button[#[[1]],
  127. CellPrint[Cell[BoxData@
  128. Replace[
  129. ToBoxes@#[[2]], TagBox[t___, HoldForm] -> t
  130. ]
  131. , "Input"]]]], {
  132. "Import[\[Placeholder]]" -> HoldForm@
  133. Pi`NotebookOpen[Pi`GetExp[Import[Placeholder[file]]]],
  134. "FromSVG[Import[\[Placeholder]]]" -> HoldForm@
  135. Pi`NotebookOpen[Pi`GetExp[
  136. Pi`FromSVG[Import[Placeholder[file], "XML"], Placeholder[id]]
  137. ]]
  138. }
  139. ], "Extract"];
  140.  
  141. End[];
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement