Additional Bitmap Methods

Frost is giving me fits. It works perfectly in terms of scattering the pixels, but it simultaneously wraps pixels that get pushed from the top or left edges of the bitmap to the bottom and right sides. It also starts to lag with larger images (even at one pass) which makes me think the code is a bit chunky. Heres a good example of what I mean:
Content Hidden Spoiler
Image
frosted to 24

I'll post the code to frost if you want to take a look at it. In the meantime, I'm going to take another whack at pixelate.
  1. extern "C" _declspec (dllexport) BOOL Frost(long object, int passes){
  2.     RGSSBMINFO *bitmap = ((RGSSBITMAP*) (object<<1)) -> bm -> bminfo;
  3.     DWORD rowsize;
  4.     DWORD width, height;
  5.     LPBYTE row;
  6.     long x, y, z;
  7.     int randomx, randomy;
  8.     if(!bitmap) return false;
  9.     width = bitmap -> infoheader -> biWidth;
  10.     height = bitmap -> infoheader -> biHeight;
  11.     rowsize = width * 4;
  12.     row = (LPBYTE) (bitmap -> firstRow);
  13.     //srand((unsigned)time(0));
  14.     for ( y = 0; y < (int) height; y++) {
  15.         LPBYTE thisrow = row;
  16.         for ( x = 0; x < (int) width; x++) {
  17.             randomx = 0;
  18.             randomy = 0;
  19.             //Summate the random, so as to avoid repeating the whole process for each pass.
  20.             //LPBYTE newrow = thisrow;
  21.             for ( z = 0; z <= passes - 2; z++) {
  22.                 randomx += ((rand() % - 5) + 2);
  23.             }
  24.             for ( z = 0; z <= passes - 2; z++) {
  25.                 randomy += ((rand() % - 5) + 2);
  26.             }
  27.             if((y > 0) && ((y + randomy) < (int) height) && ((x + randomx) < (int) width)) {
  28.                 LPBYTE newrow = thisrow;
  29.                 newrow -= (randomy * (width * 4));
  30.                 newrow += (randomx * 4 );
  31.                 thisrow[2] = newrow[2];
  32.                 thisrow[1] = newrow[1];
  33.                 thisrow[0] = newrow[0];
  34.                 thisrow[3] = newrow[3];
  35.             }
  36.             else {
  37.                 thisrow[2] = 0;
  38.                 thisrow[1] = 0;
  39.                 thisrow[0] = 0;
  40.                 thisrow[3] = 0;
  41.             }
  42.             thisrow += 4;
  43.         }
  44.         row -= rowsize;
  45.     }
  46.     return true;
  47. }
Expand to see the code.


As for the emboss, I thought an embossed image had to be devoid of colors. The emboss method leaves some colors behind. It should be a easy fix though; just loop through the pixels a second time and put them into grayscale or monochrome.
two attempts at pixelate, both of them failures Spoiler
  1. //Pixelate #2
  2. extern "C" _declspec (dllexport) BOOL Pixelate(long object, int size)
  3. {
  4.     RGSSBMINFO *bitmap = ((RGSSBITMAP*) (object<<1)) -> bm -> bminfo;
  5.     DWORD rowsize;
  6.     DWORD width, height;
  7.     LPBYTE row, scana, scanb, scanc, reset;
  8.     long x, y, w, h, z, wx, wy;
  9.     int red, green, blue, alpha;
  10.     if(!bitmap) return false;
  11.     if(size <= 1) return false;
  12.     red = 0;
  13.     green = 0;
  14.     blue = 0;
  15.     alpha = 0;
  16.     width = bitmap -> infoheader -> biWidth;
  17.     height = bitmap -> infoheader -> biHeight;
  18.     rowsize = (width * 4);
  19.     row = (LPBYTE) (bitmap -> firstRow);
  20.     reset = row;
  21.     scana = row;
  22.     scanb = row;
  23.     scanc = row;
  24.     double block = size;
  25.     for ( y = 0; y < (int) ((height - 1) / size); y++)
  26.     {
  27.         LPBYTE thisrow = row;
  28.         h = y;
  29.         h *= size;
  30.         for ( x = 0; x < (int) ((width - 1) / size); x++)
  31.         {
  32.             w = x;
  33.             w *= size;
  34.             red, green, blue, alpha = 0;
  35.             for ( z = 0; z == size - 1; z++)
  36.             {
  37.                 scana = reset;
  38.                 scana += (4 * w);
  39.                 scana -= (rowsize * h);
  40.                 scanb = reset;
  41.                 scanb += (4 * (w + z));
  42.                 scanb -= (rowsize * h);
  43.                 scanc = reset;
  44.                 scanc += (4 * w);
  45.                 scanc -= (rowsize * (h + z));
  46.                 red += ( scana[2] + scanb[2] + scanc[2] );
  47.                 green += ( scana[1] + scanb[1] + scanc[1] );
  48.                 blue += ( scana[0] + scanb[0] + scanc[0] );
  49.                 alpha += ( scana[3] + scanb[3] + scanc[3] );
  50.             }
  51.             red /= pow(block,2);
  52.             green /= pow(block,2);
  53.             blue /= pow(block,2);
  54.             alpha /= pow(block,2);
  55.             red *= sqrt(block);
  56.             green *= sqrt(block);
  57.             blue *= sqrt(block);
  58.             alpha *= sqrt(block);
  59.             for (wy = 0; wy == (size - 1); wy++);
  60.             {   
  61.                 for (wx = 0; wx == (size - 1); wx++);
  62.                 {
  63.                     thisrow[2] = (BYTE) red;
  64.                     thisrow[1] = (BYTE) green;
  65.                     thisrow[0] = (BYTE) blue;
  66.                     thisrow[3] = (BYTE) alpha;
  67.                     thisrow += 4;
  68.                 }
  69.                 thisrow -= (width * 4);
  70.             }
  71.             thisrow += (4);
  72.         }
  73.         thisrow -= (rowsize);
  74.     }
  75.     return true;
  76. }
Expand to see the code.

  1.  
  2. extern "C" _declspec (dllexport) BOOL Pixelate(long object, int size)
  3. {
  4.     RGSSBMINFO *bitmap = ((RGSSBITMAP*) (object<<1)) -> bm -> bminfo;
  5.     DWORD rowsize;
  6.     DWORD width, height;
  7.     LPBYTE row;
  8.     long x, y, zx, zy, wx, wy;
  9.     int red, green, blue, alpha;
  10.     if(!bitmap) return false;
  11.     if(size <= 1) return false;
  12.     red = 0;
  13.     green = 0;
  14.     blue = 0;
  15.     alpha = 0;
  16.     width = bitmap -> infoheader -> biWidth;
  17.     height = bitmap -> infoheader -> biHeight;
  18.     rowsize = (width * 4 * size);
  19.     row = (LPBYTE) (bitmap -> firstRow);
  20.     for ( y = 0; y < (int) ((height - 1) / size); y++)
  21.     {
  22.         LPBYTE thisrow = row;
  23.         for ( x = 0; x < (int) ((width - 1) / size); x++)
  24.         {
  25.             red, green, blue, alpha = 0;
  26.             for ( zy = 0; zy == size - 1; zy++)
  27.             {
  28.                 LPBYTE scanrow = thisrow;
  29.                 for ( zx = 0; zx == size - 1; zx++)
  30.                 {
  31.                     red += (scanrow[2]);
  32.                     green += (scanrow[1]);
  33.                     blue += (scanrow[0]);
  34.                     alpha += (scanrow[3]);
  35.                     scanrow += 4;
  36.                 }
  37.                 scanrow += (4 * width);
  38.             }
  39.             double block = size;
  40.             double r = red / pow(block,2);
  41.             r *=  sqrt(block);
  42.             double g = green / pow(block,2);
  43.             g *=  sqrt(block);
  44.             double b = blue / pow(block,2);
  45.             b *=  sqrt(block);
  46.             double a = alpha / pow(block,2);
  47.             a *=  sqrt(block);
  48.             LPBYTE saverow = thisrow;
  49.             for (wy = 0; wy == (size - 1); wy++);
  50.             {   
  51.                 for (wx = 0; wx == (size - 1); wx++);
  52.                 {
  53.                     thisrow[2] = (BYTE) r;
  54.                     thisrow[1] = (BYTE) g;
  55.                     thisrow[0] = (BYTE) b;
  56.                     thisrow[3] = (BYTE) a;
  57.                     thisrow += 4;
  58.                 }
  59.                 thisrow -= (width * 4);
  60.             }
  61.             thisrow = saverow;
  62.             thisrow += (4 * size) ;
  63.         }
  64.         thisrow -= (rowsize);
  65.     }
  66.     return true;
  67. }
  68.  
Expand to see the code.


I'm just about ready to punch a kitten.

I did fix Sepia though. The result looks a lot better.
new:
Image
old:
Image
code:
  1. extern "C" _declspec (dllexport) BOOL Sepia(long object){
  2.     RGSSBMINFO *bitmap = ((RGSSBITMAP*) (object<<1)) -> bm -> bminfo;
  3.     DWORD rowsize;
  4.     DWORD width, height;
  5.     LPBYTE row;
  6.     long x, y;
  7.     double red, green, blue, avg;
  8.     if(!bitmap) return false;
  9.     width = bitmap -> infoheader -> biWidth;
  10.     height = bitmap -> infoheader -> biHeight;
  11.     rowsize = width * 4;
  12.     row = (LPBYTE)(bitmap -> firstRow);
  13.     for ( y = 0; y < (int) height; y++) {
  14.         LPBYTE thisrow = row;
  15.         for ( x = 0; x < (int) width; x++) {
  16.             avg = (thisrow[2] + thisrow[1] + thisrow[0]) / 3;
  17.             red = (((thisrow[2] * 0.740) + (thisrow[1] * 0.475) + (thisrow[0] * 0.225)) + avg) / 2;
  18.             green = (((thisrow[2] * 0.565) + (thisrow[1] * 0.335) + (thisrow[0] * 0.180)) + avg) / 2;
  19.             blue = (((thisrow[2] * 0.325) + (thisrow[1] * 0.278) + (thisrow[0] * 0.110)) + avg) / 2;
  20.             if(red > 255) red = 255;
  21.             if(green > 255) green = 255;
  22.             if(blue > 255) blue = 255;
  23.             thisrow[2] = (BYTE) red;
  24.             thisrow[1] = (BYTE) green;
  25.             thisrow[0] = (BYTE) blue;
  26.             thisrow += 4;
  27.         }
  28.         row -= rowsize;
  29.     }
  30.     return true;
  31. }
Expand to see the code.


Edit: The full source right now:
  1. #include <stdafx.h>
  2. #include <cstdlib>
  3. #include <ctime>
  4. #include <ctype.h>
  5. #include <math.h>
  6. #include <stddef.h>
  7. #include <stdio.h>
  8. #include <stdlib.h>
  9. #include <string.h>
  10. #include <windows.h>
  11.  
  12.  
  13.  
  14. typedef struct{
  15.     DWORD flags;
  16.     DWORD klass;
  17.     void (*dmark)(void*);
  18.     void (*dfree)(void*);
  19.     double *data;
  20. } RGSSCOLOR;
  21.  
  22. typedef struct{
  23.     DWORD unk1;
  24.     DWORD unk2;
  25.     BITMAPINFOHEADER *infoheader;
  26.     RGSSCOLOR *firstRow; //RGBQUAD *firstRow;
  27.     RGBQUAD *lastRow;
  28. } RGSSBMINFO;
  29.  
  30. typedef struct{
  31.     DWORD unk1;
  32.     DWORD unk2;
  33.     RGSSBMINFO *bminfo;
  34. } BITMAPSTRUCT;
  35.  
  36. typedef struct{
  37.     DWORD flags;
  38.     DWORD klass;
  39.     void (*dmark)(void*);
  40.     void (*dfree)(void*);
  41.     BITMAPSTRUCT *bm;
  42. } RGSSBITMAP;
  43.  
  44. #define ASSERT(x) if(!x){DebugOut("Failed: %s: %d", #x, __LINE__);}
  45.  
  46. //Brighten
  47. extern "C" _declspec (dllexport) BOOL Brighten(long object, int amount)
  48. {
  49.     RGSSBMINFO *bitmap = ((RGSSBITMAP*) (object<<1)) -> bm -> bminfo;
  50.     DWORD rowsize;
  51.     DWORD width, height;
  52.     LPBYTE row;
  53.     long x, y;
  54.     int red, green, blue;
  55.     if(!bitmap) return false;
  56.     width = bitmap -> infoheader -> biWidth;
  57.     height = bitmap -> infoheader -> biHeight;
  58.     rowsize = width * 4;
  59.     row = (LPBYTE) (bitmap -> firstRow);
  60.     for ( y = 0; y < (int) height; y++) {
  61.         LPBYTE thisrow = row;
  62.         for ( x = 0; x < (int) width; x++) {
  63.             red = (thisrow[2] + amount);
  64.             green = (thisrow[1] + amount);
  65.             blue = (thisrow[0] + amount);
  66.             if(red > 255) red = 255;
  67.             if(green > 255) green = 255;
  68.             if(blue > 255) blue = 255;
  69.             thisrow[2] = (BYTE) red;
  70.             thisrow[1] = (BYTE) green;
  71.             thisrow[0] = (BYTE) blue;
  72.             thisrow += 4;
  73.         }
  74.         row -= rowsize;
  75.     }
  76.     return true;
  77. }
  78.  
  79. //Darken
  80. extern "C" _declspec (dllexport) BOOL Darken(long object, int amount)
  81. {
  82.     RGSSBMINFO *bitmap = ((RGSSBITMAP*) (object<<1)) -> bm -> bminfo;
  83.     DWORD rowsize;
  84.     DWORD width, height;
  85.     LPBYTE row;
  86.     long x, y;
  87.     int red, green, blue;
  88.     if(!bitmap) return false;
  89.     width = bitmap -> infoheader -> biWidth;
  90.     height = bitmap -> infoheader -> biHeight;
  91.     rowsize = width * 4;
  92.     row = (LPBYTE) (bitmap -> firstRow);
  93.     for ( y = 0; y < (int) height; y++) {
  94.         LPBYTE thisrow = row;
  95.         for ( x = 0; x < (int) width; x++) {
  96.             red = (thisrow[2] - amount);
  97.             green = (thisrow[1] - amount);
  98.             blue = (thisrow[0] - amount);
  99.             if(red < 0) red = 0;
  100.             if(green < 0) green = 0;
  101.             if(blue < 0) blue = 0;
  102.             thisrow[2] = (BYTE) red;
  103.             thisrow[1] = (BYTE) green;
  104.             thisrow[0] = (BYTE) blue;
  105.             thisrow += 4;
  106.         }
  107.         row -= rowsize;
  108.     }
  109.     return true;
  110. }
  111.  
  112. //Invert
  113. extern "C" _declspec (dllexport) BOOL Invert(long object)
  114. {
  115.     RGSSBMINFO *bitmap = ((RGSSBITMAP*) (object<<1)) -> bm -> bminfo;
  116.     DWORD rowsize;
  117.     DWORD width, height;
  118.     LPBYTE row;
  119.     long x, y;
  120.     int red, green, blue;
  121.     if(!bitmap) return false;
  122.     width = bitmap -> infoheader -> biWidth;
  123.     height = bitmap -> infoheader -> biHeight;
  124.     rowsize = width * 4;
  125.     row = (LPBYTE) (bitmap -> firstRow);
  126.     for ( y = 0; y < (int) height; y++) {
  127.         LPBYTE thisrow = row;
  128.         for ( x = 0; x < (int) width; x++) {
  129.             red = (255 - thisrow[2]);
  130.             green = (255 - thisrow[1]);
  131.             blue = (255 - thisrow[0]);
  132.             thisrow[2] = (BYTE) red;
  133.             thisrow[1] = (BYTE) green;
  134.             thisrow[0] = (BYTE) blue;
  135.             thisrow += 4;
  136.         }
  137.         row -= rowsize;
  138.     }
  139.     return true;
  140. }
  141.  
  142. //Grayscale
  143. extern "C" _declspec (dllexport) BOOL Grayscale(long object)
  144. {
  145.     RGSSBMINFO *bitmap = ((RGSSBITMAP*) (object<<1)) -> bm -> bminfo;
  146.     DWORD rowsize;
  147.     DWORD width, height;
  148.     LPBYTE row;
  149.     long x, y;
  150.     int red, green, blue, color;
  151.     if(!bitmap) return false;
  152.     width = bitmap -> infoheader -> biWidth;
  153.     height = bitmap -> infoheader -> biHeight;
  154.     rowsize = width * 4;
  155.     row = (LPBYTE) (bitmap -> firstRow);
  156.     for ( y = 0; y < (int) height; y++) {
  157.         LPBYTE thisrow = row;
  158.         for ( x = 0; x < (int) width; x++) {
  159.             color = (thisrow[2] + thisrow[1] + thisrow[0]) / 3;
  160.             red = color;
  161.             green = color;
  162.             blue = color;
  163.             if(red < 0) red = 0;
  164.             if(green < 0) green = 0;
  165.             if(blue < 0) blue = 0;
  166.             thisrow[2] = (BYTE) red;
  167.             thisrow[1] = (BYTE) green;
  168.             thisrow[0] = (BYTE) blue;
  169.             thisrow += 4;
  170.         }
  171.         row -= rowsize;
  172.     }
  173.     return true;
  174. }
  175.  
  176. //Hyper Contrast
  177. extern "C" _declspec (dllexport) BOOL Hcontrast(long object){
  178.     RGSSBMINFO *bitmap = ((RGSSBITMAP*) (object<<1)) -> bm -> bminfo;
  179.     DWORD rowsize;
  180.     DWORD width, height;
  181.     LPBYTE row, pixel1, pixel2, pixel3, pixel4;
  182.     long x, y;
  183.     double red, green, blue;
  184.     if(!bitmap) return false;
  185.     width = bitmap -> infoheader -> biWidth;
  186.     height = bitmap -> infoheader -> biHeight;
  187.     rowsize = width * 4;
  188.     row = (LPBYTE) (bitmap -> firstRow);
  189.     for ( y = 0; y < (int) height; y++) {
  190.         pixel1 = row;
  191.         pixel2 = row;
  192.         pixel3 = row;
  193.         pixel4 = row;
  194.         if((y >= 0) && ((y + 1) < (int) height)) {
  195.             pixel2 -= rowsize;
  196.             pixel3 -= rowsize;
  197.         }
  198.         for ( x = 0; x < (int) width; x++) {
  199.             if(((y + 1) < (int) height) && ((x + 1) < (int) width)) {
  200.                 pixel2 += 4;
  201.                 pixel4 += 4;
  202.                 red = sqrt(pow(((double) pixel1[2] - pixel2[2]), 2) + pow(((double) pixel3[2] - pixel4[2]), 2));
  203.                 green = sqrt(pow(((double) pixel1[1] - pixel2[1]), 2) + pow(((double) pixel3[1] - pixel4[1]), 2));
  204.                 blue = sqrt(pow(((double) pixel1[0] - pixel2[0]), 2) + pow(((double) pixel3[0] - pixel4[0]), 2));
  205.             }
  206.             else if(((y + 1) >= (int) height) && ((x + 1) < (int) width)) {
  207.                 pixel2 += 4;
  208.                 pixel4 += 4;
  209.                 red = sqrt(pow((double) pixel1[2], 2) + pow((double) pixel4[2], 2));
  210.                 green = sqrt(pow((double) pixel1[1], 2) + pow((double) pixel4[1], 2));
  211.                 blue = sqrt(pow((double) pixel1[0], 2) + pow((double) pixel4[0], 2));
  212.             }
  213.             else if(((y + 1) < (int) height) && ((x + 1) >= (int) width)) {
  214.                 red = sqrt(pow((double) pixel1[2], 2) + pow((double) pixel3[2], 2));
  215.                 green = sqrt(pow((double) pixel1[1], 2) + pow((double) pixel3[1], 2));
  216.                 blue = sqrt(pow((double) pixel1[0], 2) + pow((double) pixel3[0], 2));
  217.             }
  218.             else {
  219.                 red = pixel1[2];
  220.                 green = pixel1[1];
  221.                 blue = pixel1[0];
  222.             }
  223.             if((red + green + blue) > (double) 60) {
  224.                 if((red >= green) && (red >= blue)) {
  225.                     red += 20;
  226.                 }
  227.                 else if((green >= red) && (green >= blue)) {
  228.                     green += 20;
  229.                 }
  230.                 else {
  231.                     blue += 20;
  232.                 }
  233.                 if(red > 255) red = 255;
  234.                 if(green > 255) green = 255;
  235.                 if(blue > 255) blue = 255;
  236.                 pixel1[2] = (BYTE) red;
  237.                 pixel1[1] = (BYTE) green;
  238.                 pixel1[0] = (BYTE) blue;
  239.             }
  240.             else {
  241.                 pixel1[2] = 0;
  242.                 pixel1[1] = 0;
  243.                 pixel1[0] = 0;
  244.             }
  245.             pixel1 += 4;
  246.             pixel3 += 4;
  247.         }
  248.         row -= rowsize;
  249.     }
  250.     return true;
  251. }
  252. //Monochrome
  253. extern "C" _declspec (dllexport) BOOL Monochrome(long object){
  254.     RGSSBMINFO *bitmap = ((RGSSBITMAP*) (object<<1)) -> bm -> bminfo;
  255.     DWORD rowsize;
  256.     DWORD width, height;
  257.     LPBYTE row;
  258.     long x, y;
  259.     int red, green, blue, shade;
  260.     if(!bitmap) return false;
  261.     width = bitmap -> infoheader -> biWidth;
  262.     height = bitmap -> infoheader -> biHeight;
  263.     rowsize = width * 4;
  264.     row = (LPBYTE)(bitmap -> firstRow);
  265.     for ( y = 0; y < (int) height; y++) {
  266.         LPBYTE thisrow = row;
  267.         for ( x = 0; x < (int) width; x++) {
  268.             red = (thisrow[1] - thisrow[0] + thisrow[1] + thisrow[2]);
  269.             red = (red * thisrow[2] / 256);
  270.             if(red > 255) red = 255;
  271.             green = (thisrow[0] - thisrow[1] + thisrow[0] + thisrow[2]);
  272.             green = (green * thisrow[1] / 256);
  273.             if(green > 255) green = 255;
  274.             blue = (thisrow[0] - thisrow[1] + thisrow[0] + thisrow[2]);
  275.             blue = (blue * thisrow[0] / 256);
  276.             if(blue > 255) blue = 255;
  277.             shade = ((red + green + blue) / 3);
  278.             thisrow[2] = shade;
  279.             thisrow[1] = shade;
  280.             thisrow[0] = shade;
  281.             thisrow += 4;
  282.         }
  283.         row -= rowsize;
  284.     }
  285.     return true;
  286. }
  287. //Emboss
  288. extern "C" _declspec (dllexport) BOOL Emboss(long object){
  289.     RGSSBMINFO *bitmap = ((RGSSBITMAP*) (object<<1)) -> bm -> bminfo;
  290.     DWORD rowsize;
  291.     DWORD width, height;
  292.     LPBYTE row;
  293.     long x, y;
  294.     int red, green, blue;
  295.     if(!bitmap) return false;
  296.     width = bitmap -> infoheader -> biWidth;
  297.     height = bitmap -> infoheader -> biHeight;
  298.     rowsize = width * 4;
  299.     row = (LPBYTE)(bitmap -> firstRow);
  300.     for ( y = 0; y < (int) height; y++) {
  301.         LPBYTE thisrow = row;
  302.         LPBYTE nextrow = row;
  303.         if((y > 0) && ((y + 1) < (int) height)) {
  304.             nextrow -= (width * 4);
  305.         }
  306.         for ( x = 0; x < (int) width; x++) {
  307.             if(((y + 1) < (int) height) && ((x + 1) < (int) width)) {
  308.                 nextrow += 4;
  309.                 red = (thisrow[2] - nextrow[2] + 128);
  310.                 green = (thisrow[1] - nextrow[1] + 128);
  311.                 blue = (thisrow[0] - nextrow[0] + 128);
  312.             }
  313.             else {
  314.                 red = (thisrow[2] + 128);
  315.                 green = (thisrow[1] + 128);
  316.                 blue = (thisrow[0] + 128);
  317.             }
  318.             red = abs(red);
  319.             green = abs(green);
  320.             blue = abs(blue);
  321.             if(red > 255) red = 255;
  322.             if(green > 255) green = 255;
  323.             if(blue > 255) blue = 255;
  324.             thisrow[2] = red;
  325.             thisrow[1] = green;
  326.             thisrow[0] = blue;
  327.             thisrow += 4;
  328.         }
  329.         row -= rowsize;
  330.     }
  331.     return true;
  332. }
  333. //contrast
  334. /* //This will be replaced by a contrast method
  335. extern "C" _declspec (dllexport) BOOL MinusTwo(long object){
  336.     RGSSBMINFO *bitmap = ((RGSSBITMAP*) (object<<1)) -> bm -> bminfo;
  337.     DWORD rowsize;
  338.     DWORD width, height;
  339.     LPBYTE row;
  340.     long x, y;
  341.     double color1, color2;
  342.     if(!bitmap) return false;
  343.     width = bitmap -> infoheader -> biWidth;
  344.     height = bitmap -> infoheader -> biHeight;
  345.     rowsize = width * 4;
  346.     row = (LPBYTE)(bitmap -> firstRow);
  347.     for ( y = 0; y < (int) height; y++) {
  348.         LPBYTE thisrow = row;
  349.         LPBYTE nextrow = row;
  350.         if((y > 0) && ((y + 1) < (int) height)) {
  351.             nextrow -= (width * 4);
  352.         }
  353.         for ( x = 0; x < (int) width; x++) {
  354.             if(((y + 1) < (int) height) && ((x + 1) < (int) width)) {
  355.                 nextrow += 4;
  356.                 color2 = (((nextrow[2] * 0.3) + (nextrow[1] * 0.6) + (nextrow[0] * 0.1)) / 10);
  357.             }
  358.             else {
  359.                 color2 = 0;
  360.             }
  361.             color1 = (((thisrow[2] * 0.3) + (thisrow[1] * 0.6) + (thisrow[0] * 0.1)) / 10);
  362.             if((color1 - color2) > 0.2) {
  363.                 thisrow[2] = 0;
  364.                 thisrow[1] = 0;
  365.                 thisrow[0] = 0;
  366.             }
  367.             else {
  368.                 thisrow[2] = 255;
  369.                 thisrow[1] = 255;
  370.                 thisrow[0] = 255;
  371.             }
  372.             thisrow += 4;
  373.         }
  374.         row -= rowsize;
  375.     }
  376.     return true;
  377. }
  378. */
  379.  
  380. //Sepia
  381. extern "C" _declspec (dllexport) BOOL Sepia(long object){
  382.     RGSSBMINFO *bitmap = ((RGSSBITMAP*) (object<<1)) -> bm -> bminfo;
  383.     DWORD rowsize;
  384.     DWORD width, height;
  385.     LPBYTE row;
  386.     long x, y;
  387.     double red, green, blue, avg;
  388.     if(!bitmap) return false;
  389.     width = bitmap -> infoheader -> biWidth;
  390.     height = bitmap -> infoheader -> biHeight;
  391.     rowsize = width * 4;
  392.     row = (LPBYTE)(bitmap -> firstRow);
  393.     for ( y = 0; y < (int) height; y++) {
  394.         LPBYTE thisrow = row;
  395.         for ( x = 0; x < (int) width; x++) {
  396.             avg = (thisrow[2] + thisrow[1] + thisrow[0]) / 3;
  397.             red = (((thisrow[2] * 0.740) + (thisrow[1] * 0.475) + (thisrow[0] * 0.225)) + avg) / 2;
  398.             green = (((thisrow[2] * 0.565) + (thisrow[1] * 0.335) + (thisrow[0] * 0.180)) + avg) / 2;
  399.             blue = (((thisrow[2] * 0.325) + (thisrow[1] * 0.278) + (thisrow[0] * 0.110)) + avg) / 2;
  400.             if(red > 255) red = 255;
  401.             if(green > 255) green = 255;
  402.             if(blue > 255) blue = 255;
  403.             thisrow[2] = (BYTE) red;
  404.             thisrow[1] = (BYTE) green;
  405.             thisrow[0] = (BYTE) blue;
  406.             thisrow += 4;
  407.         }
  408.         row -= rowsize;
  409.     }
  410.     return true;
  411. }
  412. //Sharpen
  413. extern "C" _declspec (dllexport) BOOL Sharpen(long object){
  414.     RGSSBMINFO *bitmap = ((RGSSBITMAP*) (object<<1)) -> bm -> bminfo;
  415.     DWORD rowsize;
  416.     DWORD width, height;
  417.     LPBYTE row;
  418.     long x, y;
  419.     double red, green, blue, ap;
  420.     if(!bitmap) return false;
  421.     width = bitmap -> infoheader -> biWidth;
  422.     height = bitmap -> infoheader -> biHeight;
  423.     rowsize = width * 4;
  424.     row = (LPBYTE)(bitmap -> firstRow);
  425.     red = 0;
  426.     green = 0;
  427.     blue = 0;
  428.     ap = 0.8;
  429.     for ( y = 0; y < (int) height; y++) {
  430.         LPBYTE thisrow = row;
  431.         LPBYTE nextrow = row;
  432.         if((y > 0) && ((y + 1) < (int) height)) {
  433.             nextrow -= (width * 4);
  434.         }
  435.         for ( x = 0; x < (int) width; x++) {
  436.             if(((y + 1) < (int) height) && ((x + 1) < (int) width)) {
  437.                 nextrow += 4;
  438.                 red += nextrow[2];
  439.                 green += nextrow[1];
  440.                 blue += nextrow[0];
  441.             }
  442.             red = (((thisrow[2] - (red / 3)) * ap) + thisrow[2]);
  443.             green = (((thisrow[1] - (green / 3)) * ap) + thisrow[1]);
  444.             blue = (((thisrow[0] - (blue / 3)) * ap) + thisrow[0]);
  445.             if(red > 255) red = 255;
  446.             if(green > 255) green = 255;
  447.             if(blue > 255) blue = 255;
  448.             if(red < 0) red = 0;
  449.             if(green < 0) green = 0;
  450.             if(blue < 0) blue = 0;
  451.             thisrow[2] = (BYTE) red;
  452.             thisrow[1] = (BYTE) green;
  453.             thisrow[0] = (BYTE) blue;
  454.             thisrow += 4;
  455.         }
  456.         row -= rowsize;
  457.     }
  458.     return true;
  459. }
  460. //Soften
  461. extern "C" _declspec (dllexport) BOOL Soften(long object){
  462.     RGSSBMINFO *bitmap = ((RGSSBITMAP*) (object<<1)) -> bm -> bminfo;
  463.     DWORD rowsize;
  464.     DWORD width, height;
  465.     LPBYTE row;
  466.     long x, y, xoffset, yoffset;
  467.     int red, green, blue;
  468.     if(!bitmap) return false;
  469.     width = bitmap -> infoheader -> biWidth;
  470.     height = bitmap -> infoheader -> biHeight;
  471.     rowsize = width * 4;
  472.     row = (LPBYTE) (bitmap -> firstRow);
  473.     for ( y = 0; y < (int) height; y++) {
  474.         LPBYTE thisrow = row;
  475.         for ( x = 0; x < (int) width; x++) {
  476.             red = 0;
  477.             green = 0;
  478.             blue = 0;
  479.             for ( xoffset = -1; xoffset <= 1; xoffset++) {
  480.                 for ( yoffset = -1; yoffset <= 1; yoffset++) {
  481.                     if ((y > 0) && ((y + yoffset) < (int) height) && ((y + yoffset) > 0) && ((x + xoffset) > 0) && ((x + xoffset) < (int) width)) {
  482.                         LPBYTE newrow = thisrow;
  483.                         newrow -= (yoffset * (width * 4));
  484.                         newrow += (xoffset * 4);
  485.                         red += newrow[2];
  486.                         green += newrow[1];
  487.                         blue += newrow[0];
  488.                     }
  489.                 }
  490.             }
  491.             red /= 9;
  492.             green /= 9;
  493.             blue /= 9;
  494.             thisrow[2] = red;
  495.             thisrow[1] = green;
  496.             thisrow[0] = blue;
  497.             thisrow += 4;
  498.         }
  499.         row -= rowsize;
  500.     }
  501.     return true;
  502. }
  503. //Frost
  504. extern "C" _declspec (dllexport) BOOL Frost(long object, int passes){
  505.     RGSSBMINFO *bitmap = ((RGSSBITMAP*) (object<<1)) -> bm -> bminfo;
  506.     DWORD rowsize;
  507.     DWORD width, height;
  508.     LPBYTE row;
  509.     long x, y, z;
  510.     int randomx, randomy;
  511.     if(!bitmap) return false;
  512.     width = bitmap -> infoheader -> biWidth;
  513.     height = bitmap -> infoheader -> biHeight;
  514.     rowsize = width * 4;
  515.     row = (LPBYTE) (bitmap -> firstRow);
  516.     for ( y = 0; y < (int) height; y++) {
  517.         LPBYTE thisrow = row;
  518.         for ( x = 0; x < (int) width; x++) {
  519.             randomx = 0;
  520.             randomy = 0;
  521.             //Summate the random, so as to avoid repeating the whole process for each pass.
  522.             //LPBYTE newrow = thisrow;
  523.             for ( z = 0; z <= passes - 2; z++) {
  524.                 randomx += ((rand() % - 5) + 2);
  525.             }
  526.             for ( z = 0; z <= passes - 2; z++) {
  527.                 randomy += ((rand() % - 5) + 2);
  528.             }
  529.             if(((y + randomy) < (int) height) && ((x + randomx) < (int) width) && ((y + randomy) > 0 && ((x + randomx) > 0))) {
  530.                 LPBYTE newrow = thisrow;
  531.                 newrow -= (randomy * (width * 4));
  532.                 newrow += (passes * (width * 4));
  533.                 newrow += (randomx * 4 );
  534.                 newrow -= (passes * 4);
  535.                 thisrow[2] = newrow[2];
  536.                 thisrow[1] = newrow[1];
  537.                 thisrow[0] = newrow[0];
  538.                 thisrow[3] = newrow[3];
  539.             }
  540.             else {
  541.                 thisrow[2] = 0;
  542.                 thisrow[1] = 0;
  543.                 thisrow[0] = 0;
  544.                 thisrow[3] = 0;
  545.             }
  546.             thisrow += 4;
  547.         }
  548.         row -= rowsize;
  549.     }
  550.     return true;
  551. }
  552.  
  553.  
  554. //Pixelate
  555. extern "C" _declspec (dllexport) BOOL Pixelate(long object, int size)
  556. {
  557.     RGSSBMINFO *bitmap = ((RGSSBITMAP*) (object<<1)) -> bm -> bminfo;
  558.     DWORD rowsize;
  559.     DWORD width, height;
  560.     LPBYTE row;
  561.     long x, y, zx, zy, wx, wy;
  562.     int red, green, blue, alpha;
  563.     if(!bitmap) return false;
  564.     if(size <= 1) return false;
  565.     red = 0;
  566.     green = 0;
  567.     blue = 0;
  568.     alpha = 0;
  569.     width = bitmap -> infoheader -> biWidth;
  570.     height = bitmap -> infoheader -> biHeight;
  571.     rowsize = (width * 4 * size);
  572.     row = (LPBYTE) (bitmap -> firstRow);
  573.     for ( y = 0; y < (int) ((height - 1) / size); y++)
  574.     {
  575.         LPBYTE thisrow = row;
  576.         for ( x = 0; x < (int) ((width - 1) / size); x++)
  577.         {
  578.             red, green, blue, alpha = 0;
  579.             for ( zy = 0; zy == size - 1; zy++)
  580.             {
  581.                 LPBYTE scanrow = thisrow;
  582.                 for ( zx = 0; zx == size - 1; zx++)
  583.                 {
  584.                     red += (scanrow[2]);
  585.                     green += (scanrow[1]);
  586.                     blue += (scanrow[0]);
  587.                     alpha += (scanrow[3]);
  588.                     scanrow += 4;
  589.                 }
  590.                 scanrow -= (4 * width);
  591.             }
  592.            
  593.             double block = static_cast<double>(size);
  594.             double r = red / pow(block,2);
  595.             r *=  sqrt(block);
  596.             double g = green / pow(block,2);
  597.             g *=  sqrt(block);
  598.             double b = blue / pow(block,2);
  599.             b *=  sqrt(block);
  600.             double a = alpha / pow(block,2);
  601.             a *=  sqrt(block);
  602.             LPBYTE saverow = thisrow;
  603.             for (wy = 0; wy == (size - 1); wy++);
  604.             {   
  605.                 for (wx = 0; wx == (size - 1); wx++);
  606.                 {
  607.                     thisrow[2] = (BYTE) r;
  608.                     thisrow[1] = (BYTE) g;
  609.                     thisrow[0] = (BYTE) b;
  610.                     thisrow[3] = (BYTE) a;
  611.                     thisrow += 4;
  612.                 }
  613.                 thisrow -= (width * 4);
  614.             }
  615.             thisrow = saverow;
  616.             thisrow += (4 * size) ;
  617.         }
  618.         thisrow -= (rowsize);
  619.     }
  620.     return true;
  621. }
  622.  
  623.  
  624.  /*
  625. extern "C" _declspec (dllexport) BOOL Swap(long object1, long object2, int index){
  626.     RGSSBMINFO *bitmap = ((RGSSBITMAP*) (object1<<1)) -> bm -> bminfo;
  627.     RGSSBMINFO *palette = ((RGSSBITMAP*) (object2<<1)) -> bm -> bminfo;
  628.     DWORD rowsize, pRowSize;
  629.     DWORD width, height, pWidth;
  630.     LPBYTE row, originalColor, newColor;
  631.     long x, y;
  632.     int red, green, blue, oldRed, oldGreen, oldBlue;
  633.     if(!bitmap) return false;
  634.     width = bitmap -> infoheader -> biWidth;
  635.     height = bitmap -> infoheader -> biHeight;
  636.     pWidth = palette -> infoheader -> biWidth;
  637.     rowsize = width * 4;
  638.     pRowSize = pWidth * 4;
  639.     row = (LPBYTE) (bitmap -> firstRow);
  640.     for ( y = 0; y < (int) height; y++) {
  641.         LPBYTE thisrow = row;
  642.         for ( x = 0; x < (int) width; x++) {
  643.             red = thisrow[2];
  644.             green = thisrow[1];
  645.             blue = thisrow[0];
  646.             originalColor = (LPBYTE) (palette -> firstRow);
  647.             for ( int i = 0; i < (int) pWidth; i++) {
  648.                 oldRed = originalColor[2];
  649.                 oldGreen = originalColor[1];
  650.                 oldBlue = originalColor[0];
  651.                 if ( (red == oldRed) && (green == oldGreen) && (blue == oldBlue)) {
  652.                     newColor = originalColor - (pRowSize * (index + 1));
  653.                     thisrow[2] = newColor[2];
  654.                     thisrow[1] = newColor[1];
  655.                     thisrow[0] = newColor[0];
  656.                 }
  657.                 originalColor += 4;
  658.                 newColor += 4;
  659.             }
  660.             thisrow += 4;
  661.         }
  662.         row -= rowsize;
  663.     }
  664.     return true;
  665. }
  666.  
  667.  
  668. extern "C" _declspec (dllexport) BOOL Blank(long object){
  669.     RGSSBMINFO *bitmap = ((RGSSBITMAP*) (object<<1)) -> bm -> bminfo;
  670.     DWORD rowsize;
  671.     DWORD width, height;
  672.     LPBYTE row;
  673.     long x, y;
  674.     int red, green, blue;
  675.     if(!bitmap) return false;
  676.     width = bitmap -> infoheader -> biWidth;
  677.     height = bitmap -> infoheader -> biHeight;
  678.     rowsize = width * 4;
  679.     row = (LPBYTE) (bitmap -> firstRow);
  680.     for ( y = 0; y < (int) height; y++) {
  681.         LPBYTE thisrow = row;
  682.         for ( x = 0; x < (int) width; x++) {
  683.             red = thisrow[2];
  684.             green = thisrow[1];
  685.             blue = thisrow[0];
  686.             thisrow += 4;
  687.         }
  688.         row -= rowsize;
  689.     }
  690.     return true;
  691. }
  692.  
  693. extern "C" _declspec (dllexport) BOOL Pixelate(long object, int size)
  694. {
  695.     RGSSBMINFO *bitmap = ((RGSSBITMAP*) (object<<1)) -> bm -> bminfo;
  696.     DWORD rowsize;
  697.     DWORD width, height;
  698.     LPBYTE row;
  699.     long x, y, zx, zy, wx, wy;
  700.     int red, green, blue, alpha;
  701.     if(!bitmap) return false;
  702.     if(size <= 1) return false;
  703.     red = 0;
  704.     green = 0;
  705.     blue = 0;
  706.     alpha = 0;
  707.     width = bitmap -> infoheader -> biWidth;
  708.     height = bitmap -> infoheader -> biHeight;
  709.     rowsize = (width * 4 * size);
  710.     row = (LPBYTE) (bitmap -> firstRow);
  711.     for ( y = 0; y < (int) ((height - 1) / size); y++)
  712.     {
  713.         LPBYTE thisrow = row;
  714.         for ( x = 0; x < (int) ((width - 1) / size); x++)
  715.         {
  716.             red, green, blue, alpha = 0;
  717.             for ( zy = 0; zy == size - 1; zy++)
  718.             {
  719.                 LPBYTE scanrow = thisrow;
  720.                 for ( zx = 0; zx == size - 1; zx++)
  721.                 {
  722.                     red += (scanrow[2]);
  723.                     green += (scanrow[1]);
  724.                     blue += (scanrow[0]);
  725.                     alpha += (scanrow[3]);
  726.                     scanrow += 4;
  727.                 }
  728.                 scanrow += (4 * width);
  729.             }
  730.             double block = size;
  731.             double r = red / pow(block,2);
  732.             r *=  sqrt(block);
  733.             double g = green / pow(block,2);
  734.             g *=  sqrt(block);
  735.             double b = blue / pow(block,2);
  736.             b *=  sqrt(block);
  737.             double a = alpha / pow(block,2);
  738.             a *=  sqrt(block);
  739.             LPBYTE saverow = thisrow;
  740.             for (wy = 0; wy == (size - 1); wy++);
  741.             {   
  742.                 for (wx = 0; wx == (size - 1); wx++);
  743.                 {
  744.                     thisrow[2] = (BYTE) r;
  745.                     thisrow[1] = (BYTE) g;
  746.                     thisrow[0] = (BYTE) b;
  747.                     thisrow[3] = (BYTE) a;
  748.                     thisrow += 4;
  749.                 }
  750.                 thisrow -= (width * 4);
  751.             }
  752.             thisrow = saverow;
  753.             thisrow += (4 * size) ;
  754.         }
  755.         thisrow -= (rowsize);
  756.     }
  757.     return true;
  758. }
  759. */
  760.  
  761. //Pixelate #2
  762. /*
  763. extern "C" _declspec (dllexport) BOOL Pixelate(long object, int size)
  764. {
  765.     RGSSBMINFO *bitmap = ((RGSSBITMAP*) (object<<1)) -> bm -> bminfo;
  766.     DWORD rowsize;
  767.     DWORD width, height;
  768.     LPBYTE row, scana, scanb, scanc, reset;
  769.     long x, y, w, h, z, wx, wy;
  770.     int red, green, blue, alpha;
  771.     if(!bitmap) return false;
  772.     if(size <= 1) return false;
  773.     red = 0;
  774.     green = 0;
  775.     blue = 0;
  776.     alpha = 0;
  777.     width = bitmap -> infoheader -> biWidth;
  778.     height = bitmap -> infoheader -> biHeight;
  779.     rowsize = (width * 4);
  780.     row = (LPBYTE) (bitmap -> firstRow);
  781.     reset = row;
  782.     scana = row;
  783.     scanb = row;
  784.     scanc = row;
  785.     double block = size;
  786.     for ( y = 0; y < (int) ((height - 1) / size); y++)
  787.     {
  788.         LPBYTE thisrow = row;
  789.         h = y;
  790.         h *= size;
  791.         for ( x = 0; x < (int) ((width - 1) / size); x++)
  792.         {
  793.             w = x;
  794.             w *= size;
  795.             red, green, blue, alpha = 0;
  796.             for ( z = 0; z == size - 1; z++)
  797.             {
  798.                 scana = reset;
  799.                 scana += (4 * w);
  800.                 scana -= (rowsize * h);
  801.                 scanb = reset;
  802.                 scanb += (4 * (w + z));
  803.                 scanb -= (rowsize * h);
  804.                 scanc = reset;
  805.                 scanc += (4 * w);
  806.                 scanc -= (rowsize * (h + z));
  807.                 red += ( scana[2] + scanb[2] + scanc[2] );
  808.                 green += ( scana[1] + scanb[1] + scanc[1] );
  809.                 blue += ( scana[0] + scanb[0] + scanc[0] );
  810.                 alpha += ( scana[3] + scanb[3] + scanc[3] );
  811.             }
  812.             red /= pow(block,2);
  813.             green /= pow(block,2);
  814.             blue /= pow(block,2);
  815.             alpha /= pow(block,2);
  816.             red *= sqrt(block);
  817.             green *= sqrt(block);
  818.             blue *= sqrt(block);
  819.             alpha *= sqrt(block);
  820.             for (wy = 0; wy == (size - 1); wy++);
  821.             {   
  822.                 for (wx = 0; wx == (size - 1); wx++);
  823.                 {
  824.                     thisrow[2] = (BYTE) red;
  825.                     thisrow[1] = (BYTE) green;
  826.                     thisrow[0] = (BYTE) blue;
  827.                     thisrow[3] = (BYTE) alpha;
  828.                     thisrow += 4;
  829.                 }
  830.                 thisrow -= (width * 4);
  831.             }
  832.             thisrow += (4);
  833.         }
  834.         thisrow -= (rowsize);
  835.     }
  836.     return true;
  837. }
  838. */
  839.  
  840. /*
  841. extern "C" _declspec (dllexport) void getpixel(int x, int y)
  842. {
  843.     RGSSBMINFO *bitmap = ((RGSSBITMAP*) (object<<1)) -> bm -> bminfo;
  844.     DWORD rowsize;
  845.     DWORD width, height;
  846.     LPBYTE row;
  847.     long x, y;
  848.     int red, green, blue;
  849.     if(!bitmap) return false;
  850.     width = bitmap -> infoheader -> biWidth;
  851.     height = bitmap -> infoheader -> biHeight;
  852.     if(x>=width) return false;
  853.     if(y>=height) return false;
  854. */
Expand to see the code.

Original thread: https://www.hbgames.org/forums/viewtopic.php?t=70805