Created
October 13, 2016 12:19
-
-
Save yousry/2aa849e26791d99e8641800751840ceb to your computer and use it in GitHub Desktop.
Draft Probabilistic Steganography
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#define ToRadian(x) ((x) * M_PI / 180.0f) | |
#define ToDegree(x) ((x) * 180.0f / M_PI) | |
#define ToGrad(x) ((x) * M_PI / 360.0f) | |
static const NSString* TAG = @"main"; | |
static NSMutableArray* visited = [NSMutableArray new]; | |
static YACanvas *cSource = nil; | |
char bitsToC(vector<bool> v, int pos) { | |
char r = 0; | |
for(int i = 0; i <= 7; ++i) { | |
if(v[i + pos] == true) { | |
int s = 1 << (7 - i); | |
r += s; | |
} | |
} | |
return r; | |
} | |
vector<bool> cTobits(char c) { | |
vector<bool> r; | |
for(int i = 7; i >= 0; --i) { | |
if(c & (1 << i)) { | |
r.push_back(true); | |
} else { | |
r.push_back(false); | |
} | |
} | |
return r; | |
} | |
int main (int argc, const char * argv[]) | |
{ | |
@autoreleasepool { | |
NSLog(@"Application Startup"); | |
#define variance 101 | |
YAVector4f *whiteBack = [[YAVector4f alloc] initVal: 1]; | |
YAVector2i *grid = [[YAVector2i alloc] initVals: 16 : 12 ]; // 192 | |
float probZero = 0.25; | |
float probOne = 0.75; | |
char cryptText[] = "This is the text message."; | |
int cryptTextN = 24; | |
char pass[] = "Password"; | |
int passN = 8; | |
vector<bool> textBits; | |
for (int i = 0; i < cryptTextN; ++i) { | |
char c = cryptText[i] ^ pass[i % passN]; | |
// char c = cryptText[i]; | |
vector<bool> charBits = cTobits(c); | |
textBits.insert(textBits.end(), charBits.begin(), charBits.end()); | |
} | |
cout << "R: " << textBits.size() << endl; | |
YACanvas *cv = [[YACanvas alloc] initFile: @"original.png"]; | |
YACanvas *cvR = [[YACanvas alloc] initFile: result.png"]; | |
YAVector2i* res = cv.resolution.copy; | |
YACanvas *co = [[YACanvas alloc] initResolution: res Color: whiteBack]; | |
int hist[variance] = {0}; | |
int *h = hist; | |
YAVector3f *newCol = [[YAVector3f alloc] init]; | |
vector<bool> *tb = &textBits; | |
[res sweep: ^(YAVector2i* pos){ | |
YAVector4f *pixel = [cv getPixel: pos]; | |
Color col; | |
col.setRGB(pixel.x, pixel.y, pixel.z); | |
float value = col.getValue(); | |
int valRange = int(value * float(variance)); | |
h[int(value * float(variance))] += 1; | |
if(valRange % 2 != 0) | |
valRange -= 1; | |
assert(valRange >= 0); | |
assert(valRange % 2 == 0); | |
// calculate id | |
int x = (float(pos.x) / float(res.x)) * grid.x; | |
int y = (float(pos.y) / float(res.y)) * grid.y; | |
int idx = y * grid.x + x; | |
bool theBit = (*tb)[idx]; | |
int incer = 0; | |
if(theBit) { | |
if([YAProbability dice: probOne]) | |
incer = 1; | |
} else { | |
if([YAProbability dice: probZero]) | |
incer = 1; | |
} | |
valRange += incer; | |
float newValue = float(valRange) / float(variance); | |
col.setValue(newValue); | |
[newCol setValues: col.getR() : col.getG() : col.getB()]; | |
[co setPixel: pos Color: newCol]; | |
}]; | |
vector<int> rawRes(grid.x * grid.y, 0); | |
vector<int> *rR = &rawRes; | |
[res sweep: ^(YAVector2i* pos){ | |
// YAVector4f *pixel = [co getPixel: pos]; | |
YAVector4f *pixel = [cvR getPixel: pos]; | |
int x = (float(pos.x) / float(res.x)) * grid.x; | |
int y = (float(pos.y) / float(res.y)) * grid.y; | |
int idx = y * grid.x + x; | |
Color col; | |
col.setRGB(pixel.x, pixel.y, pixel.z); | |
float value = col.getValue(); | |
int valRange = int(value * float(variance)); | |
if(valRange % 2 != 0) { | |
(*rR)[idx] += 1; | |
} | |
}]; | |
vector<bool> textBitsOut; | |
int celSize = (res.y / grid.y) * (res.x / grid.x); | |
for(auto r : rawRes) { | |
float rp = float(r) / float(celSize); | |
if( fabs(probZero - rp) < fabs(probOne - rp)) | |
textBitsOut.push_back(true); | |
else | |
textBitsOut.push_back(false); | |
} | |
for(int i = 0; i < textBits.size(); ++i) { | |
cout << i << " : " << textBits[i] << " / " << textBitsOut[i] << endl; | |
} | |
for(int i = 0; i < 24; ++i) { | |
char c = bitsToC(textBitsOut, i * 8); | |
c = c ^ pass[i % passN]; | |
char cc = cryptText[i]; | |
printf("%c, %c\n", c ,cc); | |
} | |
cout << endl; | |
[co writePNGTo: @"result.png"]; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment