Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Begin["Pi`"];
- Pi`B = {};
- Pi`NotebookOpen[c_] := NotebookPut[Notebook[{Cell[c, "Input"]}]];
- Pi`GraphicsToRGBA[g_, size_: Automatic] := (
- Image[Image[Rasterize[
- Append[g, Method -> {"ShrinkWrap" -> True}]
- , "Image", Background -> None, ImageSize -> size], "Byte",
- ColorSpace -> "RGB", ImageSize -> size]
- ~ColorSeparate~"RGBA"~ColorCombine~"RGB"
- , "Byte", ColorSpace -> "RGB", ImageSize -> size]
- );
- Pi`ImageToRGBA[i_, size_: Automatic] := Image[
- Rasterize[
- Image[i, ImageSize -> size]
- , Background -> None, ImageSize -> size]~ColorSeparate~"RGBA"~
- ColorCombine~"RGB"
- , "Byte", ColorSpace -> "RGB"];
- Pi`ToRGBA[i_] :=
- If[Head@i === Graphics,
- Pi`GraphicsToRGBA[i]
- , If[Head@i === Image,
- Pi`ImageToRGBA[i],
- Throw["must be image or graphics"]
- ]
- ];
- FromSVG[xml_, id_] := ImportString[StringReplace[
- Replace[
- Cases[xml,
- XMLElement[
- "image", {___,
- "id" -> id, {"http://www.w3.org/1999/xlink", "href"} ->
- c_, ___}, {}]
- , \[Infinity]][[1, 2]]
- , {___, {"http://www.w3.org/1999/xlink", "href"} -> c_, ___} -> c
- ], "data:image/png;base64," -> ""], "Base64"];
- AddExp[image_, box_] := (
- Pi`AddText[image, ToString[box]]
- );
- GetExp[image_] := (
- ToExpression[Pi`GetText@image]
- );
- AddText[carrierImage_Image, mesg_String] :=
- Block[{carrierData, truncatedCarrier, pixelChannels, secretBits},
- carrierData = ImageData[carrierImage, "Byte"];
- (*Number of places to hide data*)
- pixelChannels = Apply[Times, Dimensions[carrierData]];
- (*Convert message to bit stream*)
- secretBits = PadRight[Flatten[IntegerDigits[
- ToCharacterCode[CreateStringHash[mesg] <> mesg]
- , 2, 8]], pixelChannels];
- (* Discard ONE least significant bit *)
- truncatedCarrier = BitAnd[carrierData, 2^^11111110];
- Image[truncatedCarrier +
- (*Reshape the secret bits data*)
- Fold[Partition, secretBits,
- Reverse@Rest[Dimensions[carrierData]]], "Byte",
- ColorSpace -> "RGB"
- ]];
- GetText[img_Image] := Block[{secretData},
- (*Get the least significant bit of every pixel channel*)
- secretData = BitAnd[ImageData[img, "Byte"], 1];
- (*Group into 8 bit words*)
- secretData = Partition[Flatten[secretData], 8];
- (*Convert each binary word to decimal, and then to characters*)
- secretData = FromCharacterCode[FromDigits[#, 2] & /@ secretData];
- (*Discard large blocks of trailing character zeros*)
- secretData = StringSplit[
- FromCharacterCode@Select[ToCharacterCode@secretData, (# != 0) &],
- ""];
- If[ CreateStringHash[StringJoin@secretData[[11 ;;]]] ==
- StringJoin@secretData[[1 ;; 10]],
- StringJoin@secretData[[11 ;;]], $Failed]
- ];
- CreateStringHash[s_String] := StringJoin@PadLeft[
- StringSplit[IntegerString[Hash[s, "CRC32"]], ""], 10, "0"];
- a = "";
- Print@{ Button["File",
- NotebookWrite[InputNotebook[], "\"" <>
- StringReplace[SystemDialogInput["FileSave"], "\\" -> "\\\\"]
- <> "\""];
- ], Button["BoxData", Module[{f, nb, i},
- Pi`B = Append[Pi`B, "temp"];
- i = Length[Pi`B];
- NotebookWrite[InputNotebook[], "Pi`B[[" <> ToString[i] <> "]]"];
- f = ToExpression[
- "Function[Pi`B[[" <> ToString[i] <>
- "]]=ToString@FullForm@Cases[NotebookGet[InputNotebook[]][[1]]
- , Cell[___, CellTags -> \"MyCell\", ___]
- , {1}][[1, 1]];
- NotebookClose[EvaluationNotebook[]];
- ]"];
- nb = Notebook[{Cell[
- BoxData[" "], "Input", Evaluatable -> True,
- CellEvaluationFunction -> f, CellTags -> "MyCell"]
- }];
- (* NotebookPut[nb]; *)
- NotebookFind[nb, " "];
- SetSelectedNotebook[nb];
- ]]};
- Print@Prepend[Map[
- Function[Button[#[[1]],
- CellPrint[Cell[BoxData@
- Replace[
- ToBoxes@#[[2]], TagBox[t___, HoldForm] -> t
- ]
- , "Input"]]]], {
- "Export[file]" -> HoldForm[
- Export[Placeholder[file],
- Pi`AddText[
- Pi`ToRGBA[Placeholder[grOrImage], Placeholder[size]],
- Placeholder[BoxData]]
- ]
- ]
- (* ,"Clipboard[..image,BoxData..]"-> HoldForm@
- Pi`Clipboard[Pi`AddExp[Placeholder[image],Placeholder[
- BoxData]]] *)
- }
- ], "Write To"];
- Print@Prepend[Map[
- Function[Button[#[[1]],
- CellPrint[Cell[BoxData@
- Replace[
- ToBoxes@#[[2]], TagBox[t___, HoldForm] -> t
- ]
- , "Input"]]]], {
- "Import[\[Placeholder]]" -> HoldForm@
- Pi`NotebookOpen[Pi`GetExp[Import[Placeholder[file]]]],
- "FromSVG[Import[\[Placeholder]]]" -> HoldForm@
- Pi`NotebookOpen[Pi`GetExp[
- Pi`FromSVG[Import[Placeholder[file], "XML"], Placeholder[id]]
- ]]
- }
- ], "Extract"];
- End[];
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement