uint32_t ADMImage::lumaDiff(ADMImage *src1,ADMImage *src2,uint32_t noise)
{

#ifdef ADM_CPU_X86
uint32_t r1,r2;
        if(CpuCaps::hasMMX())
        {
                return computeDiffMMX(YPLANE(src1),YPLANE(src2),noise,src1->_width*src1->_height);                
        }
#endif
        return computeDiff(YPLANE(src1),YPLANE(src2),noise,src1->_width*src1->_height);
}
uint32_t ADMImage::lumaDiff(ADMImage *src1,ADMImage *src2,uint32_t noise)
{

#if 1 && defined( ARCH_X86 ) || defined (ARCH_X86_64)
uint32_t r1,r2;
        if(CpuCaps::hasMMX())
        {
                return computeDiffMMX(YPLANE(src1),YPLANE(src2),noise,src1->_width*src1->_height);                
        }
#endif
        return computeDiff(YPLANE(src1),YPLANE(src2),noise,src1->_width*src1->_height);
}
static uint32_t computeDiffMMX(uint8_t  *s1,uint8_t *s2,uint32_t noise,uint32_t l)
{
uint32_t df=0;
uint32_t delta;
uint32_t ll,rr;
uint64_t noise2=(uint64_t )noise;

uint32_t result=0;
        noise64=noise2+(noise2<<16)+(noise2<<32)+(noise2<<48);
        ll=l>>2;
        rr=l&3;

#ifdef GCC_2_95_X
         __asm__(
                         "pxor %mm7,%mm7\n"
                         "pxor %mm3,%mm3\n"
                         "movq "Mangle(noise64)", %mm6\n"
                :: 
                 );
#else
         __asm__(
                         "pxor %%mm7,%%mm7\n"
                         "pxor %%mm3,%%mm3\n"
                         "movq "Mangle(noise64)", %%mm6\n"
                :: 
                 );
#endif

          for(int y=0;y<ll;y++)
          {
                __asm__(
                        "movd           (%0),  %%mm0 \n"
                        "movd           (%1),  %%mm1 \n"
                        "punpcklbw      %%mm7, %%mm0 \n"
                        "punpcklbw      %%mm7, %%mm1 \n"
                        
                        "movq           %%mm0, %%mm2 \n"
                        "psubusw        %%mm1, %%mm2 \n"
                        "psubusw        %%mm0, %%mm1 \n"
                        "por            %%mm1, %%mm2 \n" // SAD  
                                           
                        "movq           %%mm2, %%mm0 \n"
                        "pcmpgtw        %%mm6, %%mm2 \n" // Threshold against noise
                        "pand           %%mm2, %%mm0 \n" // MM0 is the 4 diff, time to pack

                        "movq           %%mm0, %%mm1 \n" // MM0 is a b c d and we want
                        "psrlq          $16,  %%mm1 \n"  // mm3+=a+b+c+d

                        "movq           %%mm0, %%mm2 \n"
                        "psrlq          $32,  %%mm2 \n"

                        "movq           %%mm0, %%mm4 \n"
                        "psrlq          $48,  %%mm4 \n"

                        "paddw          %%mm1, %%mm0 \n"
                        "paddw          %%mm2, %%mm4 \n"
                        "paddw          %%mm4, %%mm0 \n"

                        "psllq          $48,  %%mm0 \n"
                        "psrlq          $48,  %%mm0 \n"

                        "paddw          %%mm0, %%mm3 \n" /* PADDQ is SSE2 */

                : : "r" (s1),"r" (s2)
                );
                        s1+=4;
                        s2+=4;
                        
         }
        // Pack result
#if 1        
                __asm__(
                       
                        "movd           %%mm3,(%0)\n"
                        "emms\n"
                :: "r"(&result)
                );
#endif                
        if(rr) result+=computeDiff(s1, s2, noise,rr);
        return result;
}
Example #4
0
int main(int argc, char **argv)
{
    FILE *fp;
    int width, height, alpha;
    unsigned char *image, *fb;
    SDL_Surface *screen;
    struct triangles *triangles, *best, *absbest;
    long long diff;
    float percdiff, bestdiff;

    /* Initialization */
    srand(time(NULL));
    state.max_shapes = 64;
    state.max_shapes_incremental = 1;
    state.temperature = 0.10;
    state.generation = 0;
    state.absbestdiff = 100; /* 100% is worst diff possible. */

    /* Check arity and parse additional args if any. */
    if (argc < 4) {
        showHelp(argv[0]);
        exit(1);
    }

    if (argc > 4) {
        int j;
        for (j = 4; j < argc; j++) {
            int moreargs = j+1 < argc;

            if (!strcmp(argv[j],"--use-triangles") && moreargs) {
                opt_use_triangles = atoi(argv[++j]);
            } else if (!strcmp(argv[j],"--use-circles") && moreargs) {
                opt_use_circles = atoi(argv[++j]);
            } else if (!strcmp(argv[j],"--max-shapes") && moreargs) {
                state.max_shapes = atoi(argv[++j]);
            } else if (!strcmp(argv[j],"--initial-shapes") && moreargs) {
                state.max_shapes_incremental = atoi(argv[++j]);
            } else if (!strcmp(argv[j],"--mutation-rate") && moreargs) {
                opt_mutation_rate = atoi(argv[++j]);
            } else if (!strcmp(argv[j],"--restart")) {
                opt_restart = 1;
            } else if (!strcmp(argv[j],"--help")) {
                showHelp(argv[0]);
            } else {
                fprintf(stderr,"Invalid options.");
                showHelp(argv[0]);
                exit(1);
            }
        }
    }

    /* Sanity check. */
    if (state.max_shapes_incremental > state.max_shapes)
        state.max_shapes = state.max_shapes_incremental;
    if (opt_mutation_rate > 1000)
        opt_mutation_rate = 1000;

    /* Load the PNG in memory. */
    fp = fopen(argv[1],"rb");
    if (!fp) {
        perror("Opening PNG file");
        exit(1);
    }
    if ((image = PngLoad(fp,&width,&height,&alpha)) == NULL) {
        printf("Can't load the specified image.");
        exit(1);
    }

    printf("Image %d %d, alpha:%d at %p\n", width, height, alpha, image);
    fclose(fp);

    /* Initialize SDL and allocate our arrays of triangles. */
    screen = sdlInit(width,height,0);
    fb = malloc(width*height*3);
    triangles = mkRandomtriangles(state.max_shapes,width,height);
    best = mkRandomtriangles(state.max_shapes,width,height);
    absbest = mkRandomtriangles(state.max_shapes,width,height);
    state.absbestdiff = bestdiff = 100;

    /* Load the binary file if any. */
    if (!opt_restart) {
        loadBinary(argv[2],best);
    } else {
        best->inuse = state.max_shapes_incremental;
    }
    absbest->inuse = best->inuse;
    memcpy(absbest->triangles,best->triangles,
        sizeof(struct triangle)*best->count);

    /* Show the current evolved image and the real image for one scond each. */
    memset(fb,0,width*height*3);
    drawtriangles(fb,width,height,best);
    sdlShowRgb(screen,fb,width,height);
    sleep(1);
    sdlShowRgb(screen,image,width,height);
    sleep(1);

    /* Evolve the current solution using simulated annealing. */
    while(1) {
        state.generation++;
        if (state.temperature > 0 && !(state.generation % 10)) {
            state.temperature -= 0.00001;
            if (state.temperature < 0) state.temperature = 0;
        }

        /* From time to time allow the current solution to use one more
         * triangle, up to the configured max number. */
        if ((state.generation % 1000) == 0) {
            if (state.max_shapes_incremental < triangles->count &&
                triangles->inuse > state.max_shapes_incremental-1)
            {
                state.max_shapes_incremental++;
            }
        }

        /* Copy what is currenly the best solution, and mutate it. */
        memcpy(triangles->triangles,best->triangles,
            sizeof(struct triangle)*best->count);
        triangles->inuse = best->inuse;
        mutatetriangles(triangles,10,width,height);

        /* Draw the mutated solution, and check what is its fitness.
         * In our case the fitness is the difference bewteen the target
         * image and our image. */
        memset(fb,0,width*height*3);
        drawtriangles(fb,width,height,triangles);
        diff = computeDiff(image,fb,width,height);

        /* The percentage of difference is calculate taking the ratio between
         * the maximum difference and the current difference.
         * The magic constant 422 is actually the max difference between
         * two pixels as r,g,b coordinates in the space, so sqrt(255^2*3). */
        percdiff = (float)diff/(width*height*442)*100;
        if (percdiff < bestdiff ||
            (state.temperature > 0 &&
             ((float)rand()/RAND_MAX) < state.temperature &&
             (percdiff-state.absbestdiff) < 2*state.temperature))
        {
            /* Save what is currently our "best" solution, even if actually
             * this may be a jump backward depending on the temperature.
             * It will be used as a base of the next iteration. */
            best->inuse = triangles->inuse;
            memcpy(best->triangles,triangles->triangles,
                sizeof(struct triangle)*best->count);

            if (percdiff < bestdiff) {
                /* We always save a copy of the absolute best solution we found
                 * so far, after some generation without finding anything better
                 * we may jump back to that solution.
                 *
                 * We also use the absolute best solution to save the program
                 * state in the binary file, and as SVG output. */
                absbest->inuse = best->inuse;
                memcpy(absbest->triangles,best->triangles,
                    sizeof(struct triangle)*best->count);
                state.absbestdiff = percdiff;
            }

            printf("Diff is %f%% (inuse:%d, max:%d, gen:%lld, temp:%f)\n",
                percdiff,
                triangles->inuse,
                state.max_shapes_incremental,
                state.generation,
                state.temperature);

            bestdiff = percdiff;
            sdlShowRgb(screen,fb,width,height);
        }
        processSdlEvents();

        /* From time to time save the current state into a binary save
         * and produce an SVG of the current solution. */
        if ((state.generation % 100) == 0) {
            saveSvg(argv[3],absbest,width,height);
            saveBinary(argv[2],absbest);
        }
    }
    return 0;
}
Example #5
0
int main(int argc, char **argv)
{
    struct sockaddr_in host;
    struct hostent     h_ent, *p_h_ent;

    char               host_name[80];
    char               *c;

    int                s;
    int                ret;
    int                mess_len;
    char               mess_buf[MAX_MESS_LEN];
    char               *neto_mess_ptr = &mess_buf[sizeof (mess_len)];
    FILE                *fr; /* Pointer to source file, which we read */
    int                 nread;


    int totalSent=0;
    int totalSuccessfullySent=0;
    struct timeval beginTime, endTime, progBeginTime;

    if(argc != 3) {
	printf("Usage: t_ncp <source_file_path> <servername>\n");
	exit(1);
    }    

     /* Open the source file for reading */
    if ((fr = fopen(argv[1], "r")) == NULL) {
        perror("fopen");
        exit(0);
    }
    printf("Opened for reading...\n");

    s = socket(AF_INET, SOCK_STREAM, 0); /* Create a socket (TCP) */
    if (s<0) {
        perror("Net_client: socket error");
        exit(1);
    }

    host.sin_family = AF_INET;
    host.sin_port   = htons(PORT);

    c = strchr(argv[2],'\n'); /* remove new line */
    if ( c ) *c = '\0';
    c = strchr(argv[2],'\r'); /* remove carriage return */
    if ( c ) *c = '\0';

    p_h_ent = gethostbyname(argv[2]);
    if ( p_h_ent == NULL ) {
        printf("net_client: gethostbyname error.\n");
        exit(1);
    }

    memcpy( &h_ent, p_h_ent, sizeof(h_ent) );
    memcpy( &host.sin_addr, h_ent.h_addr_list[0],  sizeof(host.sin_addr) );

    ret = connect(s, (struct sockaddr *)&host, sizeof(host) ); /* Connect! */
    if( ret < 0)
    {
        perror( "Net_client: could not connect to server"); 
        exit(1);
    }
    gettimeofday(&(beginTime), NULL);
    gettimeofday(&progBeginTime, NULL);

    for(;;)
    {
         /* Read in a chunk of the file */
        
        nread = fread(neto_mess_ptr, 1, BUF_SIZE, fr);
        mess_len = nread + sizeof(mess_len);
        memcpy( mess_buf, &mess_len, sizeof(mess_len));
        ret = send( s, mess_buf, mess_len, 0);
        totalSent += mess_len;
        totalSuccessfullySent+=mess_len;
        if (totalSent >(20 * 1024 * 1024)) {
            printf("Total Amount of Data Successfully Sent = %d kBytes. \n", totalSuccessfullySent / 1024);
            gettimeofday(&endTime, NULL);
            printf("Transfer rate for last 20MBytes = %.3f Mbits/sec. \n", ((double) (20 * 8 * 1000)) / (computeDiff(endTime, beginTime)));
            totalSent=0;
            gettimeofday(&beginTime, NULL);
                      
        }

        if (nread < BUF_SIZE) {
            gettimeofday(&(endTime), NULL);
            printf("Time taken :%ld ms\n", computeDiff(endTime, progBeginTime));
            /* Did we reach the EOF? */
            if (feof(fr)) {
                printf("Finished writing.\n");
                break;
            } else {
                printf("An error occurred...\n");
                exit(0);
            }
        }
    }
    return 0;
}