fragment shader skips part of coding - python-3.x

hi when i made my fragment shader coding i use:
---fragment
$HEADER$
void main(void)
{
float width = 0.0;
float offset = 0.1;
vec4 out_color = vec4(0, 0, 1, 1);
float coord = normalize(gl_FragCoord.x);
if (width == 0.0) {
vec4 out_color = vec4(0, 1, 1, 1);
}
else if (mod(((coord+offset) / width),2.0) < 0.3) {
vec4 out_color = vec4(0, 0, 0, 1);
}
gl_FragColor = out_color;
}
but when running the file with the vertex shader:
---vertex
$HEADER$
void main(void)
{
vec4 pos = vec4(vPosition.xy, 0.0, 1.0);
gl_Position = projection_mat * modelview_mat * pos;
}
and the python code:
from kivy.app import App
from kivy.base import EventLoop
from kivy.graphics import Mesh
from kivy.graphics.instructions import RenderContext
from kivy.uix.widget import Widged
w = 1000
h = 600
class GlslDemo(Widget):
def __init__(self, **kwargs):
Widget.__init__(self, **kwargs)
self.canvas = RenderContext(use_parent_projection=True)
self.canvas.shader.source = 'basic.glsl'
fmt = (
(b'vPosition', 2, 'float'),
)
vertices = (
0, 0,
w, 0,
w, h,
0, h,
)
indices = (0, 1, 2, 2, 3, 0)
with self.canvas:
Mesh(fmt=fmt, mode='triangles',
indices=indices, vertices=vertices)
class GlslApp(App):
def build(self):
EventLoop.ensure_window()
return GlslDemo()
if __name__ == '__main__':
GlslApp().run()
The problem when running this file is that it just outputs the vec4 out_color and skips the if and if else statements of the fragment shader code.

The variable out_color is declared 3 times in the fragment shader
1st in the scope of main
vec4 out_color = vec4(0, 0, 1, 1);
And another 2 times in the if statement:
if (width == 0.0) {
vec4 out_color = vec4(0, 1, 1, 1);
}
else if (mod(((coord+offset) / width),2.0) < 0.3) {
vec4 out_color = vec4(0, 0, 0, 1);
}
You have to assign to the variable which is declared in the scope of main, instead of declaring new variables in the if statement:
vec4 out_color = vec4(0, 0, 1, 1);
if (width == 0.0) {
out_color = vec4(0, 1, 1, 1);
}
else if (mod(((coord+offset) / width),2.0) < 0.3) {
out_color = vec4(0, 0, 0, 1);
}
Note, vec4 out_color; is a variable declaration, So vec4 out_color = vec4(0, 1, 1, 1); is an assignment to a new variabel, while out_color = vec4(0, 1, 1, 1); would assign to the existing variable out_color.

Related

Python OpenGL Texture wont properly load

This is my program:
import glfw
from OpenGL.GL import *
import OpenGL.GL.shaders
import numpy
from PIL import Image
def main():
# initialize glfw
if not glfw.init():
return
# creating the window
window = glfw.create_window(800, 600, "My OpenGL window", None, None)
if not window:
glfw.terminate()
return
glfw.make_context_current(window)
# positions colors texture coords
quad = [-0.5, -0.5, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0,
0.5, -0.5, 0.0, 0.0, 1.0, 0.0, 1.0, 0.0,
0.5, 0.5, 0.0, 0.0, 0.0, 1.0, 1.0, 1.0,
-0.5, 0.5, 0.0, 1.0, 1.0, 1.0, 0.0, 1.0]
quad = numpy.array(quad, dtype=numpy.float32)
indices = [0, 1, 2,
2, 3, 0]
indices = numpy.array(indices, dtype=numpy.uint32)
vertex_shader = """
#version 330
in layout(location = 0) vec3 position;
in layout(location = 1) vec3 color;
in layout(location = 2) vec2 inTexCoords;
out vec3 newColor;
out vec2 outTexCoords;
void main()
{
gl_Position = vec4(position, 1.0f);
newColor = color;
outTexCoords = inTexCoords;
}
"""
fragment_shader = """
#version 330
in vec3 newColor;
in vec2 outTexCoords;
out vec4 outColor;
uniform sampler2D samplerTex;
void main()
{
outColor = texture(samplerTex, outTexCoords);
}
"""
shader = OpenGL.GL.shaders.compileProgram(OpenGL.GL.shaders.compileShader(vertex_shader, GL_VERTEX_SHADER),
OpenGL.GL.shaders.compileShader(fragment_shader, GL_FRAGMENT_SHADER))
VBO = glGenBuffers(1)
glBindBuffer(GL_ARRAY_BUFFER, VBO)
glBufferData(GL_ARRAY_BUFFER, 128, quad, GL_STATIC_DRAW)
EBO = glGenBuffers(1)
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO)
glBufferData(GL_ELEMENT_ARRAY_BUFFER, 24, indices, GL_STATIC_DRAW)
# position = glGetAttribLocation(shader, "position")
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 32, ctypes.c_void_p(0))
glEnableVertexAttribArray(0)
# color = glGetAttribLocation(shader, "color")
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 32, ctypes.c_void_p(12))
glEnableVertexAttribArray(1)
# texCoords = glGetAttribLocation(shader, "inTexCoords")
glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, 32, ctypes.c_void_p(24))
glEnableVertexAttribArray(2)
texture = glGenTextures(1)
glBindTexture(GL_TEXTURE_2D, texture)
# texture wrapping params
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT)
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT)
# texture filtering params
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR)
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR)
image = Image.open("res/crate.jpg")
img_data = numpy.array(list(image.getdata()), numpy.uint8)
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 420, 420, 0, GL_RGB, GL_UNSIGNED_BYTE, img_data)
glUseProgram(shader)
glClearColor(0.2, 0.3, 0.2, 1.0)
while not glfw.window_should_close(window):
glfw.poll_events()
glClear(GL_COLOR_BUFFER_BIT)
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, None)
glfw.swap_buffers(window)
glfw.terminate()
if __name__ == "__main__":
main()
Expectation:
Result:
Help.
After loading the image, the format of the image is 'JPEG'.
You have to convert the image to the format RGB
image = image.convert('RGB')
and to flip the image top to bottom:
image = image.transpose(Image.FLIP_TOP_BOTTOM)
Before you load the image you have to set the the GL_UNPACK_ALIGNMENT to 1 by glPixelStorei, because the length of the lines of the image is not aligned to 4:
image = Image.open("res/crate.jpg")
image = image.convert('RGB')
image = image.transpose(Image.FLIP_TOP_BOTTOM)
img_data = numpy.array(list(image.getdata()), numpy.uint8)
glPixelStorei(GL_UNPACK_ALIGNMENT, 1)
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 420, 420, 0, GL_RGB, GL_UNSIGNED_BYTE, img_data)
Of course you can use the format RGBA too:
image = Image.open("res/crate.jpg")
image = image.convert('RGBA')
image = image.transpose(Image.FLIP_TOP_BOTTOM)
img_data = numpy.array(list(image.getdata()), numpy.uint8)
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 420, 420, 0, GL_RGBA, GL_UNSIGNED_BYTE, img_data)
image = Image.open("res/crate.jpg")
image = image.convert('RGB')
image = image.transpose(Image.FLIP_TOP_BOTTOM)
img_data = np.array(list(image.getdata()), np.uint8)
glPixelStorei(GL_UNPACK_ALIGNMENT, 1)
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 420, 420, 0, GL_RGB, GL_UNSIGNED_BYTE, img_data)
#image = Image.open("res/crate.jpg")
#image = image.convert('RGBA')
#image = image.transpose(Image.FLIP_TOP_BOTTOM)
#img_data = np.array(list(image.getdata()), np.uint8)
#glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 420, 420, 0, GL_RGBA, GL_UNSIGNED_BYTE, img_data)
both of those methods fix my problem so thanks for everyone who helped

OpenGL ES 3.0 GL_POINTS doesn't render anything

Below is a mininal reproducer:
GL_CHECK(glClearColor(0.4f, 0.4f, 0.4f, 1.0f));
GL_CHECK(glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT |
GL_STENCIL_BUFFER_BIT));
GL_CHECK(glUseProgram(this->pp_->shader_program));
GL_CHECK(glEnable(GL_TEXTURE_2D));
GL_CHECK(glActiveTexture(GL_TEXTURE0));
GL_CHECK(glBindTexture(GL_TEXTURE_2D, this->particle_->id));
GLfloat points[] = { 150.f, 150.f, 10.0f, 150.0f, 175.0f, 10.0f };
GLfloat colors[] = { 1.0f, 0.0f, 0.0f, 1.0f, 1.0f, 1.0f, 0.0f, 1.0f };
shader_pass_set_uniform(this->pp_, HASH("mvp"), glm::value_ptr(this->camera_));
GL_CHECK(glEnableVertexAttribArray(0));
GL_CHECK(glEnableVertexAttribArray(3));
GL_CHECK(glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, &points[0]));
GL_CHECK(glVertexAttribPointer(3, 4, GL_FLOAT, GL_FALSE, 0, &colors[0]));
GL_CHECK(glDrawArrays(GL_POINTS, 0, 2));
vertex shader:
#version 300 es
layout(location = 0) in highp vec3 vertex;
layout(location = 3) in highp vec4 color;
out lowp vec4 vcolor;
uniform mat4 mvp;
void main()
{
vcolor = color;
gl_Position = mvp * vec4(vertex.xy, 0.0, 1.0);
gl_PointSize = vertex.z;
}
fragment shader:
#version 300 es
uniform sampler2D stexture;
in lowp vec4 vcolor;
layout(location = 0) out lowp vec4 ocolor;
void main()
{
ocolor = texture(stexture, gl_PointCoord) * vcolor;
}
Nothing gets rendered on-screen, my glxinfo can be found in this pastebin. When I render the same texture onto a triangle it works.
Here is also the render loop as captured with apitrace:
250155 glClearColor(red = 0.5, green = 0.5, blue = 0.5, alpha = 1)
250157 glClear(mask = GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT | GL_COLOR_BUFFER_BIT)
250159 glUseProgram(program = 9)
250161 glEnable(cap = GL_TEXTURE_2D)
250163 glActiveTexture(texture = GL_TEXTURE0)
250165 glBindTexture(target = GL_TEXTURE_2D, texture = 2)
250167 glUniformMatrix4fv(location = 0, count = 1, transpose = GL_FALSE, value = {0.001953125, 0, 0, 0, 0, 0.002604167, 0, 0, 0, 0, -1, 0, -1, -1, -0, 1})
250168 glEnableVertexAttribArray(index = 0)
250170 glEnableVertexAttribArray(index = 3)
250174 glVertexAttribPointer(index = 0, size = 3, type = GL_FLOAT, normalized = GL_FALSE, stride = 0, pointer = blob(24))
250175 glVertexAttribPointer(index = 3, size = 4, type = GL_FLOAT, normalized = GL_FALSE, stride = 0, pointer = blob(32))
250176 glDrawArrays(mode = GL_POINTS, first = 0, count = 2)
250178 glXSwapBuffers(dpy = 0x1564010, drawable = 50331661)
I guess that could mean, that the range of point sizes, that your GLES implementation supports, may be not what you are expecting. You can try something like:
GLint range[2];
glGetIntegerv(GL_ALIASED_POINT_SIZE_RANGE, range); // for example, (1, 511) in my implementation
to check it out. Any implementation guarantees, that range[1] is at least 1.0. The value gl_PointSize is getting clamped to the range, so in the worst case you won't be able to set point with size, greater than 1. In this case gl_PointCoord seems to evaluate to (1, 0) (according to formula from specs) at the only fragment, that is going to be drawn for each point. After the texture's wrap mode come into play, (1, 0) may turn into (0, 0), for example if you are using GL_REPEAT as the texture's wrap mode. If you'd like to safely draw a point with side, greater than one, you should try using the ordinary way to draw squares (for example using four vertices and GL_TRIANGLE_FAN or GL_TRIANGLE_STRIP mode).

process gets killed by linux kernel?

I am trying to break the DES algorithms by finding the key of 56 bits using threads, it works fine for key sizes of 27 bit but when it goes higher the process gets killed by linux kernel. I dont understand why? I've used htop to see what is happening it looks like it is using a lot of memory and the swap memory for no reason . I've checked my code again but it seems fine i dont see any memory leaks. Please help me out.
# include <stdio.h>
# include <fstream>
# include <string.h>
# include <iostream>
# include <stdlib.h>
# include <math.h>
# include <pthread.h>
# include <fstream>
# include <streambuf>
using namespace std;
int flag=0;
int thread=0;
struct common_thread_param{
int bin[56];
char plain_text[1000];
char cipher_text[1000];
int thread;
} p1,p2,p3,p4; // for 4 threads
struct result{
int key_mod[64];
int thread;
} res; // for 4 threads
class Des
{
public:
int keyi[16][48],
total[64],
left[32],
right[32],
ck[28],
dk[28],
expansion[48],
z[48],
xor1[48],
sub[32],
p[32],
xor2[32],
temp[64],
pc1[56],
ip[64],
inv[8][8];
char final[1000];
void IP();
void PermChoice1(int[64]);
void PermChoice2();
void Expansion();
void inverse();
void xor_two();
void xor_oneE(int);
void xor_oneD(int);
void substitution();
void permutation();
void keygen(int[64]);
char * Encrypt(char *, int[64]);
char * Decrypt(char *, int[64]);
};
void Des::IP() //Initial Permutation
{
int k = 58, i;
for (i = 0; i<32; i++)
{
ip[i] = total[k-1];
if (k - 8>0) k = k - 8;
else k = k + 58;
}
k = 57;
for (i = 32; i<64; i++)
{
ip[i] = total[k-1];
if (k - 8>0) k = k - 8;
else k = k + 58;
}
}
void Des::PermChoice1(int key_mod[64]) //Permutation Choice-1
{
int k = 57, i;
for (i = 0; i<28; i++)
{
pc1[i] = key_mod[k - 1];
if (k - 8>0) k = k - 8;
else k = k + 57;
}
k = 63;
for (i = 28; i<52; i++)
{
pc1[i] = key_mod[k - 1];
if (k - 8>0) k = k - 8;
else k = k + 55;
}
k = 28;
for (i = 52; i<56; i++)
{
pc1[i] = key_mod[k - 1];
k = k - 8;
}
}
void Des::Expansion() //Expansion Function applied on `right' half
{
int exp[8][6], i, j, k;
for (i = 0; i<8; i++)
{
for (j = 0; j<6; j++)
{
if ((j != 0) || (j != 5))
{
k = 4 * i + j;
exp[i][j] = right[k - 1];
}
if (j == 0)
{
k = 4 * i;
exp[i][j] = right[k - 1];
}
if (j == 5)
{
k = 4 * i + j;
exp[i][j] = right[k - 1];
}
}
}
exp[0][0] = right[31];
exp[7][5] = right[0];
k = 0;
for (i = 0; i<8; i++)
for (j = 0; j<6; j++)
expansion[k++] = exp[i][j];
}
void Des::PermChoice2()
{
int per[56], i, k;
for (i = 0; i<28; i++) per[i] = ck[i];
for (k = 0, i = 28; i<56; i++) per[i] = dk[k++];
z[0] = per[13];
z[1] = per[16];
z[2] = per[10];
z[3] = per[23];
z[4] = per[0];
z[5] = per[4];
z[6] = per[2];
z[7] = per[27];
z[8] = per[14];
z[9] = per[5];
z[10] = per[20];
z[11] = per[9];
z[12] = per[22];
z[13] = per[18];
z[14] = per[11];
z[15] = per[3];
z[16] = per[25];
z[17] = per[7];
z[18] = per[15];
z[19] = per[6];
z[20] = per[26];
z[21] = per[19];
z[22] = per[12];
z[23] = per[1];
z[24] = per[40];
z[25] = per[51];
z[26] = per[30];
z[27] = per[36];
z[28] = per[46];
z[29] = per[54];
z[30] = per[29];
z[31] = per[39];
z[32] = per[50];
z[33] = per[46];
z[34] = per[32];
z[35] = per[47];
z[36] = per[43];
z[37] = per[48];
z[38] = per[38];
z[39] = per[55];
z[40] = per[33];
z[41] = per[52];
z[42] = per[45];
z[43] = per[41];
z[44] = per[49];
z[45] = per[35];
z[46] = per[28];
z[47] = per[31];
}
void Des::xor_oneE(int round) //for Encrypt
{
int i;
for (i = 0; i<48; i++)
xor1[i] = expansion[i] ^ keyi[round - 1][i];
}
void Des::xor_oneD(int round) //for Decrypt
{
int i;
for (i = 0; i<48; i++)
xor1[i] = expansion[i] ^ keyi[16 - round][i];
}
void Des::substitution()
{
int s1[4][16] =
{
14, 4, 13, 1, 2, 15, 11, 8, 3, 10, 6, 12, 5, 9, 0, 7,
0, 15, 7, 4, 14, 2, 13, 1, 10, 6, 12, 11, 9, 5, 3, 8,
4, 1, 14, 8, 13, 6, 2, 11, 15, 12, 9, 7, 3, 10, 5, 0,
15, 12, 8, 2, 4, 9, 1, 7, 5, 11, 3, 14, 10, 0, 6, 13
};
int s2[4][16] =
{
15, 1, 8, 14, 6, 11, 3, 4, 9, 7, 2, 13, 12, 0, 5, 10,
3, 13, 4, 7, 15, 2, 8, 14, 12, 0, 1, 10, 6, 9, 11, 5,
0, 14, 7, 11, 10, 4, 13, 1, 5, 8, 12, 6, 9, 3, 2, 15,
13, 8, 10, 1, 3, 15, 4, 2, 11, 6, 7, 12, 0, 5, 14, 9
};
int s3[4][16] =
{
10, 0, 9, 14, 6, 3, 15, 5, 1, 13, 12, 7, 11, 4, 2, 8,
13, 7, 0, 9, 3, 4, 6, 10, 2, 8, 5, 14, 12, 11, 15, 1,
13, 6, 4, 9, 8, 15, 3, 0, 11, 1, 2, 12, 5, 10, 14, 7,
1, 10, 13, 0, 6, 9, 8, 7, 4, 15, 14, 3, 11, 5, 2, 12
};
int s4[4][16] =
{
7, 13, 14, 3, 0, 6, 9, 10, 1, 2, 8, 5, 11, 12, 4, 15,
13, 8, 11, 5, 6, 15, 0, 3, 4, 7, 2, 12, 1, 10, 14, 9,
10, 6, 9, 0, 12, 11, 7, 13, 15, 1, 3, 14, 5, 2, 8, 4,
3, 15, 0, 6, 10, 1, 13, 8, 9, 4, 5, 11, 12, 7, 2, 14
};
int s5[4][16] =
{
2, 12, 4, 1, 7, 10, 11, 6, 8, 5, 3, 15, 13, 0, 14, 9,
14, 11, 2, 12, 4, 7, 13, 1, 5, 0, 15, 10, 3, 9, 8, 6,
4, 2, 1, 11, 10, 13, 7, 8, 15, 9, 12, 5, 6, 3, 0, 14,
11, 8, 12, 7, 1, 14, 2, 13, 6, 15, 0, 9, 10, 4, 5, 3
};
int s6[4][16] =
{
12, 1, 10, 15, 9, 2, 6, 8, 0, 13, 3, 4, 14, 7, 5, 11,
10, 15, 4, 2, 7, 12, 9, 5, 6, 1, 13, 14, 0, 11, 3, 8,
9, 14, 15, 5, 2, 8, 12, 3, 7, 0, 4, 10, 1, 13, 11, 6,
4, 3, 2, 12, 9, 5, 15, 10, 11, 14, 1, 7, 6, 0, 8, 13
};
int s7[4][16] =
{
4, 11, 2, 14, 15, 0, 8, 13, 3, 12, 9, 7, 5, 10, 6, 1,
13, 0, 11, 7, 4, 9, 1, 10, 14, 3, 5, 12, 2, 15, 8, 6,
1, 4, 11, 13, 12, 3, 7, 14, 10, 15, 6, 8, 0, 5, 9, 2,
6, 11, 13, 8, 1, 4, 10, 7, 9, 5, 0, 15, 14, 2, 3, 12
};
int s8[4][16] =
{
13, 2, 8, 4, 6, 15, 11, 1, 10, 9, 3, 14, 5, 0, 12, 7,
1, 15, 13, 8, 10, 3, 7, 4, 12, 5, 6, 11, 0, 14, 9, 2,
7, 11, 4, 1, 9, 12, 14, 2, 0, 6, 10, 13, 15, 3, 5, 8,
2, 1, 14, 7, 4, 10, 8, 13, 15, 12, 9, 0, 3, 5, 6, 11
};
int a[8][6], k = 0, i, j, p, q, count = 0, g = 0, v;
for (i = 0; i<8; i++)
{
for (j = 0; j<6; j++)
{
a[i][j] = xor1[k++];
}
}
for (i = 0; i<8; i++)
{
p = 1;
q = 0;
k = (a[i][0] * 2) + (a[i][5] * 1);
j = 4;
while (j>0)
{
q = q + (a[i][j] * p);
p = p * 2;
j--;
}
count = i + 1;
switch (count)
{
case 1:
v = s1[k][q];
break;
case 2:
v = s2[k][q];
break;
case 3:
v = s3[k][q];
break;
case 4:
v = s4[k][q];
break;
case 5:
v = s5[k][q];
break;
case 6:
v = s6[k][q];
break;
case 7:
v = s7[k][q];
break;
case 8:
v = s8[k][q];
break;
}
int d, i = 3, a[4];
while (v>0)
{
d = v % 2;
a[i--] = d;
v = v / 2;
}
while (i >= 0)
{
a[i--] = 0;
}
for (i = 0; i<4; i++)
sub[g++] = a[i];
}
}
void Des::permutation()
{
p[0] = sub[15];
p[1] = sub[6];
p[2] = sub[19];
p[3] = sub[20];
p[4] = sub[28];
p[5] = sub[11];
p[6] = sub[27];
p[7] = sub[16];
p[8] = sub[0];
p[9] = sub[14];
p[10] = sub[22];
p[11] = sub[25];
p[12] = sub[4];
p[13] = sub[17];
p[14] = sub[30];
p[15] = sub[9];
p[16] = sub[1];
p[17] = sub[7];
p[18] = sub[23];
p[19] = sub[13];
p[20] = sub[31];
p[21] = sub[26];
p[22] = sub[2];
p[23] = sub[8];
p[24] = sub[18];
p[25] = sub[12];
p[26] = sub[29];
p[27] = sub[5];
p[28] = sub[21];
p[29] = sub[10];
p[30] = sub[3];
p[31] = sub[24];
}
void Des::xor_two()
{
int i;
for (i = 0; i<32; i++)
{
xor2[i] = left[i] ^ p[i];
}
}
void Des::inverse()
{
int p = 40, q = 8, k1, k2, i, j;
for (i = 0; i<8; i++)
{
k1 = p;
k2 = q;
for (j = 0; j<8; j++)
{
if (j % 2 == 0)
{
inv[i][j] = temp[k1 - 1];
k1 = k1 + 8;
}
else if (j % 2 != 0)
{
inv[i][j] = temp[k2 - 1];
k2 = k2 + 8;
}
}
p = p - 1;
q = q - 1;
}
}
char * Des::Encrypt(char *Text1, int key_mod[64])
{
int i, a1, j, nB, m, iB, k, K, B[8], n, t, d, round;
char *Text = new char[1000];
strcpy(Text, Text1);
i = strlen(Text);
int mc = 0;
a1 = i % 8;
if (a1 != 0) for (j = 0; j<8 - a1; j++, i++) Text[i] = ' ';
Text[i] = '\0';
keygen(key_mod);
for (iB = 0, nB = 0, m = 0; m<(strlen(Text) / 8); m++) //Repeat for TextLenth/8 times.
{
for (iB = 0, i = 0; i<8; i++, nB++)
{
n = (int)Text[nB];
for (K = 7; n >= 1; K--)
{
B[K] = n % 2; //Converting 8-Bytes to 64-bit Binary Format
n /= 2;
}
for (; K >= 0; K--) B[K] = 0;
for (K = 0; K<8; K++, iB++) total[iB] = B[K]; //Now `total' contains the 64-Bit binary format of 8-Bytes
}
IP(); //Performing initial permutation on `total[64]'
for (i = 0; i<64; i++) total[i] = ip[i]; //Store values of ip[64] into total[64]
for (i = 0; i<32; i++) left[i] = total[i]; // +--> left[32]
// total[64]--|
for (; i<64; i++) right[i - 32] = total[i]; // +--> right[32]
for (round = 1; round <= 16; round++)
{
Expansion(); //Performing expansion on `right[32]' to get `expansion[48]'
xor_oneE(round); //Performing XOR operation on expansion[48],z[48] to get xor1[48]
substitution();//Perform substitution on xor1[48] to get sub[32]
permutation(); //Performing Permutation on sub[32] to get p[32]
xor_two(); //Performing XOR operation on left[32],p[32] to get xor2[32]
for (i = 0; i<32; i++) left[i] = right[i]; //Dumping right[32] into left[32]
for (i = 0; i<32; i++) right[i] = xor2[i]; //Dumping xor2[32] into right[32]
}
for (i = 0; i<32; i++) temp[i] = right[i]; // Dumping -->[ swap32bit ]
for (; i<64; i++) temp[i] = left[i - 32]; // left[32],right[32] into temp[64]
inverse(); //Inversing the bits of temp[64] to get inv[8][8]
/* Obtaining the Cypher-Text into final[1000]*/
k = 128;
d = 0;
for (i = 0; i<8; i++)
{
for (j = 0; j<8; j++)
{
d = d + inv[i][j] * k;
k = k / 2;
}
final[mc++] = (char)d;
k = 128;
d = 0;
}
} //for loop ends here
final[mc] = '\0';
return(final);
}
int common_thread_function(struct common_thread_param * data, Des d){
long i = 0;
int key_mod[64] =
{
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0
};
long quotient = 0, temp = 0;
for (unsigned long long int y = 0; y < pow(2,25); y++){
if(flag ==1)
break;
i = 55;
quotient = temp;
if(flag!=0)
break;
//quotient = 16385;
while (quotient != 0){
data->bin[i--] = quotient % 2;
quotient = quotient / 2;
}
temp++;
int sum = 0, f = 0;
for (int j = 0; j < 56; j++){
sum = sum + data->bin[j];
key_mod[f] = data->bin[j];
f++;
if (f % 8 == 7){
if (sum % 2 == 0){
key_mod[f] = 1;
}
else{
key_mod[f] = 0;
}
f++;
sum = 0;
}
}
//cout << key_mod;
if (strcmp(d.Encrypt(data->plain_text, key_mod), data->cipher_text) == 0){
flag=1;
memcpy(res.key_mod,key_mod,64*sizeof(int));
res.thread = data->thread;
thread = res.thread;
break;
}
}
return flag;
}
void *thread1(void *s)
{
Des d;
if(common_thread_function((struct common_thread_param *)s,d)==1 && thread == 1){
cout << "Result Found\n";
for (int k = 0; k < 64; k++)
cout << res.key_mod[k];
cout << "\n in Thread-" << res.thread << "\n" ;
}
pthread_exit(NULL);
}
void *thread2(void *s)
{
Des d;
if(common_thread_function((struct common_thread_param *)s,d)==1 && thread == 2){
cout << "Result Found\n";
for (int k = 0; k < 64; k++)
cout << res.key_mod[k];
cout << "\n in Thread-" << res.thread << "\n" ;
}
pthread_exit(NULL);
}
void *thread3(void *s)
{
Des d;
if(common_thread_function((struct common_thread_param *)s,d)==1 && thread == 3){
cout << "Result Found\n";
for (int k = 0; k < 64; k++)
cout << res.key_mod[k];
cout << "\n in Thread-" << res.thread << "\n" ;
}
pthread_exit(NULL);
}
void *thread4(void *s)
{
Des d;
if(common_thread_function((struct common_thread_param *)s,d)==1 && thread == 4){
cout << "Result Found\n";
for (int k = 0; k < 64; k++)
cout << res.key_mod[k];
cout << "\n in Thread-" << res.thread << "\n" ;
}
pthread_exit(NULL);
}
int main()
{
Des d2;
int bin_t1[56] = {
0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0
};
int bin_t2[56] = {
0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0,
0, 1, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0
};
int bin_t3[56] = {
0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0,
1, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0
};
int bin_t4[56] = {
0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0,
1, 1, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0
};
memcpy(p1.bin,bin_t1,56*sizeof(int));
memcpy(p2.bin,bin_t2,56*sizeof(int));
memcpy(p3.bin,bin_t3,56*sizeof(int));
memcpy(p4.bin,bin_t4,56*sizeof(int));
p1.thread =1;
p2.thread =2;
p3.thread =3;
p4.thread =4;
char pt[1000];
cin >> pt;
strcpy(p1.plain_text, pt);
strcpy(p2.plain_text, pt);
strcpy(p3.plain_text, pt);
strcpy(p4.plain_text, pt);
strcpy(p1.cipher_text, ct);
strcpy(p2.cipher_text, ct);
strcpy(p3.cipher_text, ct);
strcpy(p4.cipher_text, ct);
pthread_t t1,t2,t3,t4;
pthread_create(&t1,NULL,thread1,&p1);
pthread_create(&t2,NULL,thread2,&p2);
pthread_create(&t3,NULL,thread3,&p3);
pthread_create(&t4,NULL,thread4,&p4);
pthread_exit(NULL);
}
void Des::keygen(int key_mod[64])
{
PermChoice1(key_mod);
int i, j, k = 0;
for (i = 0; i<28; i++)
{
ck[i] = pc1[i];
}
for (i = 28; i<56; i++)
{
dk[k] = pc1[i];
k++;
}
int noshift = 0, round;
for (round = 1; round <= 16; round++)
{
if (round == 1 || round == 2 || round == 9 || round == 16)
noshift = 1;
else
noshift = 2;
while (noshift>0)
{
int t;
t = ck[0];
for (i = 0; i<28; i++)
ck[i] = ck[i + 1];
ck[27] = t;
t = dk[0];
for (i = 0; i<28; i++)
dk[i] = dk[i + 1];
dk[27] = t;
noshift--;
}
PermChoice2();
for (i = 0; i<48; i++)
keyi[round - 1][i] = z[i];
}
}
It sounds like your process is being terminated by the Linux OOM-killer, is that what you are saying? If so, then you will have to dig around to find out why using the standard tools, or else show us your code so we can see what might be happening.
One could speculate that in going from n bits to 2n bits (e.g. 27 to 54 bits or higher) you are not merely doubling the keyspace but exponentially increasing it, since n+1 bit key lengths imply a search space an order of magnitude larger than n bit ones. I could quite easily see how that might make your memory demands higher.

Draw Raphael Donut Chart

http://www.mediafire.com/view/z8ad4pedqr7twbl/donut.png
I want to draw this donut like that image. I have use raphaeljs for draw it. But I can't find the solution to make these borders with red and blue. Can someone help me? Is it possible or not?
My code below:
Raphael.fn.donutChart = function (cx, cy, r, rin, values, labels, stroke) {
var paper = this,
rad = Math.PI / 180,
chart = this.set();
function sector(cx, cy, r, startAngle, endAngle, params) {
//console.log(params.fill);
var x1 = cx + r * Math.cos(-startAngle * rad),
x2 = cx + r * Math.cos(-endAngle * rad),
y1 = cy + r * Math.sin(-startAngle * rad),
y2 = cy + r * Math.sin(-endAngle * rad),
xx1 = cx + rin * Math.cos(-startAngle * rad),
xx2 = cx + rin * Math.cos(-endAngle * rad),
yy1 = cy + rin * Math.sin(-startAngle * rad),
yy2 = cy + rin * Math.sin(-endAngle * rad);
return paper.path(["M", xx1, yy1,
"L", x1, y1,
"A", r, r, 0, +(endAngle - startAngle > 180), 0, x2, y2,
"L", xx2, yy2,
"A", rin, rin, 0, +(endAngle - startAngle > 180), 1, xx1, yy1, "z"]
).attr(params);
}
var angle = 0,
total = 0,
start = 0,
process = function (j) {
var value = values[j],
angleplus = 360 * value / total,
popangle = angle + (angleplus / 2),
color = Raphael.hsb(start, .75, 1),
ms = 500,
delta = 30,
bcolor = "#ccc",
p = sector(cx, cy, r, angle, angle + angleplus, {fill:"#dfdfdf", "stroke-width":3, stroke:"red"}),
txt = paper.text(cx + (r + delta + 155) * Math.cos(-popangle * rad), cy + (r + delta + 150) * Math.sin(-popangle * rad), labels[j]).attr({fill:"#000", stroke: "none", opacity: 0, "font-size": 20});
p.mouseover(function () {
p.stop().animate({transform: "s1.25 1.25 " + cx + " " + cy}, ms, "elastic");
txt.stop().animate({opacity: 1}, ms, "elastic");
}).mouseout(function () {
p.stop().animate({transform: ""}, ms, "elastic");
txt.stop().animate({opacity: 0}, ms);
});
angle += angleplus;
chart.push(p);
chart.push(txt);
start += .1;
};
for (var i = 0, ii = values.length; i < ii; i++) {
total += values[i];
}
for (i = 0; i < ii; i++) {
process(i);
}
return chart;
};
var values = [],
labels = [];
$("tr").each(function () {
values.push(parseInt($("td", this).text(), 10));
labels.push($("th", this).text());
});
$("table").hide();
Raphael("holder", 700, 700).donutChart(350, 350, 80, 150, values, labels, "#fff");
You have to create separate paths if you want to stroke them differently:
var p = paper.path(["M", xx1, yy1,
"L", x1, y1,
"A", r, r, 0, +(endAngle - startAngle > 180), 0, x2, y2,
"L", xx2, yy2,
"A", rin, rin, 0, +(endAngle - startAngle > 180), 1, xx1, yy1, "z"]
).attr(params);
paper.path(["M", x1, y1, "A", r, r, 0, +(endAngle - startAngle > 180), 0, x2, y2]).attr({stroke: 'blue', 'stroke-width': 3});
paper.path(["M", xx2, yy2, "A", rin, rin, 0, +(endAngle - startAngle > 180), 1, xx1, yy1]).attr({stroke: 'red', 'stroke-width': 3});
return p;

the probIem of "cvEigenVV"

When I compile the program,VC2005 always tell:
error C2198: 'cvEigenVV' : too few arguments for call"
Part of the code below:
void draw_oxfd_feature( IplImage* img, struct feature* feat, CvScalar color )
{
double m[4] = { feat->a, feat->b, feat->b, feat->c };
double v[4] = { 0 };
double e[2] = { 0 };
CvMat M;
CvMat V;
CvMat E;
double alpha, l1, l2;
/* compute axes and orientation of ellipse surrounding affine region */
cvInitMatHeader( &M, 2, 2, CV_64FC1, m, CV_AUTOSTEP );
cvInitMatHeader( &V, 2, 2, CV_64FC1, v, CV_AUTOSTEP );
cvInitMatHeader( &E, 2, 1, CV_64FC1, e, CV_AUTOSTEP );
cvEigenVV( &M, &V, &E, DBL_EPSILON );
l1 = 1 / sqrt( e[1] );
l2 = 1 / sqrt( e[0] );
alpha = -atan2( v[1], v[0] );
alpha *= 180 / CV_PI;
cvEllipse( img, cvPoint( feat->x, feat->y ), cvSize( l2, l1 ), alpha,
0, 360, CV_RGB(0,0,0), 3, 8, 0 );
cvEllipse( img, cvPoint( feat->x, feat->y ), cvSize( l2, l1 ), alpha,
0, 360, color, 1, 8, 0 );
cvLine( img, cvPoint( feat->x+2, feat->y ), cvPoint( feat->x-2, feat->y ),
color, 1, 8, 0 );
cvLine( img, cvPoint( feat->x, feat->y+2 ), cvPoint( feat->x, feat->y-2 ),
color, 1, 8, 0 );
// cvCircle(img,cvPoint(cvRound( feat->x ),cvRound( feat->y )),2, color, CV_FILLED, 8, 0);
cvCircle(img,cvPoint( cvRound( feat->x ), cvRound( feat->y )), 2,CV_RGB(0,255,0), CV_FILLED, 8, 0 );
}
How to solve the problem?Thank you!
Take a look at the declaration of the cvEigenVV() function call, wherever that's located. It looks like you are either missing a parameter, or have too many....
Is it possible that there are overloaded versions, and that due to type conversion problems, the wrong one is being called when the overload is being resolved?

Resources