0

I was trying to fade two images and the results were not the expected (did it earlier with C code). Here is the MMX code:

void fadeMMX(ImagenBMP *imagen1, ImagenBMP *imagen2, int f) {
double *puntero1;
double *puntero2;
int valor = (int)f*255;
for (int i=0; i <imagen1->tamanyo; i+=4) //De 4 en 4, ya que leemos 4 pixels cada vez
        {
            puntero1 = (double*) &imagen1->datos[i];
            puntero2 = (double*) &imagen2->datos[i];
            _asm{
                /*Guardar Imagenes*/
                /*Imagen A*/
                mov esi, puntero1[0]//imagen 1, leer
                mov edi, puntero1[0]//imagen 1, guardar
                movq mm1, [esi] //guardamos A (32 bits)
                punpcklbw mm2,mm1 //desempaquetamos A en mm2 
                /*Imagen B*/
                mov esi, puntero2[0]//imagen 2, leer
                movq mm3, [esi] //guardamos B (32 bits)
                punpcklbw mm4,mm3 //desempaquetamos B en mm4

                //Guardamos el valor
                movq mm0, valor     
                punpcklbw mm6,mm0 

                //Operaciones
                psubsw mm2,mm4  //restamos B a A y guardamos en A 
                pmulhw mm2, mm6 //multiplicamos A * F y guardamos en A, los bits mas significativos
                paddusw mm2, mm4 //sumamos lo que tenemos ahora en A mas lo que teniamos en B y guardamos en A

                /*Empaquetar*/
                packsswb mm5, mm2 //empaquetamos en mm5 - saturacion con signo
                movd [edi], mm5 //guardamos en memoria el nuevo valor
            }
} //end for
_asm
                {
                    emms //Finalizar utilización de registros MMX   
                }}; //end fadeMMX

Resulting image of fadeMMX with f = 100

Expected image

Paul R
  • 208,748
  • 37
  • 389
  • 560
danibeam
  • 29
  • 6
  • 1
    Any particular reason why you chose to use MMX and assembly for this ? An SSE implementation using intrinsics would have been much easier to code, and probably would give 2x faster run-time. – Paul R Nov 26 '15 at 16:53
  • It's a practice for a school project, the follow exercise is to do it with SSE, could you give me any advice for it? @PaulR – danibeam Nov 26 '15 at 16:56
  • 1
    For the SSE implementation I would definitely recommend using intrinsics rather than raw assembly. Other than that it will probably be fairly similar to your MMX approach, expect that you'll be processing twice as many pixels per loop iteration. Look at some of the questions and answers in the [tag:sse] tag here on StackOverflow for useful ideas. – Paul R Nov 26 '15 at 16:59

1 Answers1

1

I've just found the 'answer': I forgot to add this line

mov edi, puntero2[0]//imagen 2, guardar

To save the second image into the pointer.

danibeam
  • 29
  • 6