Skip to content

Instantly share code, notes, and snippets.

@senior-sigan
Last active December 15, 2015 15:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save senior-sigan/5285689 to your computer and use it in GitHub Desktop.
Save senior-sigan/5285689 to your computer and use it in GitHub Desktop.
Игры с фотками как с матрицами
//Copyright by Siganiv Ilya. LOL THIS CODE JUST FOR LULZ!!!!!!!!!!
#include <stdio.h>
#include <stdlib.h>
#include <wand/MagickWand.h>
#include <sys/types.h>
//gcc `MagickWand-config --cflags --cppflags` -o main main.c `MagickWand-config --ldflags --libs`
void transpose(Quantum** a,Quantum** res,unsigned int columns, unsigned int rows){
int i,j;
for (i=0;i<rows;++i){
for(j=0;j<columns;++j){
res[i][j] = a[j][i];
}
}
}
Quantum production(Quantum* a,Quantum* b, unsigned int size){
unsigned int i=0,res=0;
for (i=0;i<size;++i){
res += a[i]*b[i];
}
return res;
}
void matrix_production( Quantum** a, Quantum** b, Quantum** res, unsigned int a_rows, unsigned int a_columns,unsigned int b_rows,unsigned int b_columns){
unsigned int i,j,k;
if (a_columns != b_columns){
perror("FAIL! MATRIX IS NOT VALID!!\n");
return;
}
for(i=0;i<a_rows;++i){
for(j=0;j<b_rows;++j){
k = production(a[i],b[j],a_columns);
res[i][j] = k;
}
}
}
int main(void)
{
MagickWand *mw,*mw_xor,*mw2;
PixelIterator *imw,*imw_xor,*imw2;
PixelWand **pmw,**pmw2,**pmw_xor;
Quantum qr,qg,qb;
Quantum qr_xor,qg_xor,qb_xor;
char* fname1="test1.jpg";
char* fname2="test2.jpg";
char* res="res.jpg";
unsigned long y;
register long x;
unsigned int width,height;
MagickWandGenesis();
mw = NewMagickWand();
mw_xor = NewMagickWand();
MagickReadImage(mw, fname1);
MagickReadImage(mw_xor, fname2);
width = MagickGetImageWidth(mw);
height = MagickGetImageHeight(mw);
if (MagickGetImageWidth(mw_xor) != width && MagickGetImageHeight(mw_xor) != height){
perror("Width and height doesn't match\n");
exit(-1);
}
printf("width %d\nheight %d\n",width,height);
Quantum** pic_red;
Quantum** pic_blue;
Quantum** pic_green;
Quantum** pic2_red;
Quantum** pic2_blue;
Quantum** pic2_green;
Quantum** res_red;
Quantum** res_blue;
Quantum** res_green;
pic_red = (Quantum**)malloc(sizeof(Quantum*)*height);
pic_blue = (Quantum**)malloc(sizeof(Quantum*)*height);
pic_green = (Quantum**)malloc(sizeof(Quantum*)*height);
pic2_red = (Quantum**)malloc(sizeof(Quantum*)*height);
pic2_blue = (Quantum**)malloc(sizeof(Quantum*)*height);
pic2_green = (Quantum**)malloc(sizeof(Quantum*)*height);
res_red = (Quantum**)malloc(sizeof(Quantum*)*height);
res_blue = (Quantum**)malloc(sizeof(Quantum*)*height);
res_green = (Quantum**)malloc(sizeof(Quantum*)*height);
for (y = 0; y< height; ++y){
pic_red[y] = (Quantum*)malloc(sizeof(Quantum)*width);
pic_blue[y] = (Quantum*)malloc(sizeof(Quantum)*width);
pic_green[y] = (Quantum*)malloc(sizeof(Quantum)*width);
pic2_red[y] = (Quantum*)malloc(sizeof(Quantum)*width);
pic2_blue[y] = (Quantum*)malloc(sizeof(Quantum)*width);
pic2_green[y] = (Quantum*)malloc(sizeof(Quantum)*width);
res_red[y] = (Quantum*)malloc(sizeof(Quantum)*width);
res_blue[y] = (Quantum*)malloc(sizeof(Quantum)*width);
res_green[y] = (Quantum*)malloc(sizeof(Quantum)*width);
}
mw2 = NewMagickWand();
MagickSetSize(mw2,width,height);
MagickReadImage(mw2,"xc:none");
imw = NewPixelIterator(mw);
imw2 = NewPixelIterator(mw2);
imw_xor = NewPixelIterator(mw_xor);
for(y = 0;y < height; ++y){
pmw = PixelGetNextIteratorRow(imw, &width);
// pmw2 = PixelGetNextIteratorRow(imw2, &width);
pmw_xor = PixelGetNextIteratorRow(imw_xor, &width);
for (x = 0;x < (long)width; ++x){
pic_red[y][x] = qr = PixelGetRedQuantum(pmw[x]);
pic_green[y][x] = qg = PixelGetGreenQuantum(pmw[x]);
pic_blue[y][x] = qb = PixelGetBlueQuantum(pmw[x]);
pic2_red[y][x] = PixelGetRedQuantum(pmw[x]);
pic2_green[y][x] = PixelGetGreenQuantum(pmw[x]);
pic2_blue[y][x] = PixelGetBlueQuantum(pmw[x]);
// qr_xor = PixelGetRedQuantum(pmw_xor[x]);
// qg_xor = PixelGetGreenQuantum(pmw_xor[x]);
// qb_xor = PixelGetBlueQuantum(pmw_xor[x]);
// PixelSetRedQuantum(pmw2[x],qr + qb_xor);
// PixelSetGreenQuantum(pmw2[x],qg + qr_xor);
// PixelSetBlueQuantum(pmw2[x],qb + qg_xor);
// PixelSetRedQuantum(pmw2[x],qg); меняем цвета местами очень красиво выходит
// PixelSetGreenQuantum(pmw2[x],qb);
// PixelSetBlueQuantum(pmw2[x],qr);
}
// PixelSyncIterator(imw2);
// PixelSyncIterator(imw_xor);
}
transpose(pic_red,res_red,width,height);
transpose(pic_green,res_green,width,height);
transpose(pic_blue,res_blue,width,height);
matrix_production(res_red,pic2_red,pic_red,height,width,height,width);
matrix_production(res_blue,pic2_blue,pic_blue,height,width,height,width);
matrix_production(res_green,pic2_green,pic_green,height,width,height,width);
for(y=0;y<height;++y){
pmw2 = PixelGetNextIteratorRow(imw2, &width);
for (x = 0;x< width; ++x){
PixelSetRedQuantum(pmw2[x],pic_red[y][x]);
PixelSetGreenQuantum(pmw2[x],pic_green[y][x]);
PixelSetBlueQuantum(pmw2[x],pic_blue[y][x]);
}
PixelSyncIterator(imw2);
}
MagickWriteImage(mw2,res);
imw = DestroyPixelIterator(imw);
imw2 = DestroyPixelIterator(imw2);
mw = DestroyMagickWand(mw);
mw2 = DestroyMagickWand(mw2);
MagickWandTerminus();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment