This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <FL/Fl.H> | |
#include <FL/Fl_Box.H> | |
#include <FL/Fl_Double_Window.H> | |
#include <FL/Fl_GIF_Image.H> | |
#include <FL/Fl_JPEG_Image.H> | |
#include <FL/Fl_PNG_Image.H> | |
#include <FL/Fl_XBM_Image.H> | |
#include <string.h> | |
enum { | |
ROTATE_CLOCKWISE, | |
ROTATE_COUNTER_CLOCKWISE, | |
ROTATE_180, | |
FLIP_VERTICAL, | |
FLIP_HORIZONTAL | |
}; | |
// w=10,h=8,d=1,ld=15 | |
static const uchar rgb_array[] = { | |
0x00,0xff,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, | |
0x01,0x02,0x03,0x04,0x05, // line data | |
0xff,0xff,0x00,0xff,0xff,0xff,0xff,0xff,0xff,0xff, | |
0x06,0x07,0x08,0x09,0x0a, // line data | |
0x00,0x00,0x00,0xff,0x00,0x00,0x00,0x00,0x00,0x00, | |
0x01,0x02,0x03,0x04,0x05, // line data | |
0xff,0xff,0xff,0xff,0x00,0xff,0xff,0xff,0xff,0xff, | |
0x06,0x07,0x08,0x09,0x0a, // line data | |
0x00,0x00,0x00,0x00,0x00,0xff,0x00,0x00,0x00,0x00, | |
0x01,0x02,0x03,0x04,0x05, // line data | |
0xff,0xff,0xff,0xff,0xff,0x00,0xff,0xff,0xff,0xff, | |
0x06,0x07,0x08,0x09,0x0a, // line data | |
0x00,0x00,0x00,0x00,0xff,0x00,0x00,0x00,0x00,0x00, | |
0x01,0x02,0x03,0x04,0x05, // line data | |
0xff,0xff,0xff,0x00,0xff,0xff,0xff,0xff,0xff,0xff, | |
0x06,0x07,0x08,0x09,0x0a // line data | |
}; | |
static Fl_RGB_Image *modify_rgb_image(int method, Fl_RGB_Image *in) | |
{ | |
int i, j, x, y, w, h, d, ld, l; | |
if (!in) { | |
return NULL; | |
} | |
w = in->w(); | |
h = in->h(); | |
d = in->d(); | |
ld = in->ld(); | |
if (d < 1 || d > 4) { | |
return NULL; | |
} | |
if ((ld > 0 && ld < w*d) || ld < 0) { | |
return NULL; | |
} | |
uchar *out = new uchar[w*h*d]; | |
if (ld != 0) { | |
ld -= w*d; | |
} | |
if (d == 1) { | |
if (method == ROTATE_CLOCKWISE) { | |
for (x=0, i=0; x < w; ++x) { | |
for (y=h-1; y >= 0; --y, ++i) { | |
out[i] = in->array[y*w + y*ld + x]; | |
} | |
} | |
return new Fl_RGB_Image(out, h, w, 1); | |
} else if (method == ROTATE_COUNTER_CLOCKWISE) { | |
for (x=w-1, i=0; x >= 0; --x) { | |
for (y=0; y < h; ++y, ++i) { | |
out[i] = in->array[y*w + y*ld + x]; | |
} | |
} | |
return new Fl_RGB_Image(out, h, w, 1); | |
} | |
else if (method == ROTATE_180) { | |
if (ld == 0) { | |
for (i=0, l=w*h; i < l; ++i) { | |
out[i] = in->array[l - i]; | |
} | |
} else { | |
for (y=h-1, i=0; y >= 0; --y, ++i) { | |
for (x=w-1, j=0; x >= 0; --x, ++j) { | |
out[i*w + j] = in->array[y*w + y*ld + x]; | |
} | |
} | |
} | |
} | |
else if (method == FLIP_VERTICAL) { | |
for (y=h-1, i=0; y >= 0; --y, ++i) { | |
out[i*w] = in->array[y*ld + y*w]; | |
} | |
} | |
else if (method == FLIP_HORIZONTAL) { | |
for (y=0; y < h; ++y) { | |
for (x=w-1, i=0; x >= 0; --x, ++i) { | |
out[i + y*w] = in->array[y*w + y*ld + x]; | |
} | |
} | |
} else { | |
delete out; | |
return NULL; | |
} | |
} else { | |
if (method == ROTATE_CLOCKWISE) { | |
for (x=0, i=0; x < w; ++x) { | |
for (y=h-1; y >= 0; --y, i+=d) { | |
memcpy(out + i, in->array + y*w*d + y*ld + x*d, d); | |
} | |
} | |
return new Fl_RGB_Image(out, h, w, d); | |
} else if (method == ROTATE_COUNTER_CLOCKWISE) { | |
for (x=w-1, i=0; x >= 0; --x) { | |
for (y=0; y < h; ++y, i+=d) { | |
memcpy(out + i, in->array + y*w*d + y*ld + x*d, d); | |
} | |
} | |
return new Fl_RGB_Image(out, h, w, d); | |
} | |
else if (method == ROTATE_180) { | |
if (ld == 0) { | |
for (i=0, l=w*h*d; i < l; i+=d) { | |
memcpy(out + i, in->array + l - i - d, d); | |
} | |
} else { | |
for (y=h-1, i=0; y >= 0; --y, ++i) { | |
for (x=w-1, j=0; x >= 0; --x, j+=d) { | |
memcpy(out + i*w*d + j, in->array + y*w*d + y*ld + x*d, d); | |
} | |
} | |
} | |
} | |
else if (method == FLIP_VERTICAL) { | |
for (y=h-1, i=0, l=w*d; y >= 0; --y, ++i) { | |
memcpy(out + i*l, in->array + y*ld + y*l, l); | |
} | |
} | |
else if (method == FLIP_HORIZONTAL) { | |
for (y=0; y < h; ++y) { | |
for (x=w-1, i=0; x >= 0; --x, i+=d) { | |
memcpy(out + i + y*w*d, in->array + y*w*d + y*ld + x*d, d); | |
} | |
} | |
} else { | |
delete out; | |
return NULL; | |
} | |
} | |
return new Fl_RGB_Image(out, w, h, d); | |
} | |
static Fl_RGB_Image *convert_bitmap_to_rgb(Fl_Bitmap *in) | |
{ | |
int w, h, x, y, i, b; | |
if (!in) { | |
return NULL; | |
} | |
w = in->w(); | |
h = in->h(); | |
uchar *out = new uchar[w*h]; | |
for (y=0, i=0; y < h; ++y) { | |
for (x=0; x < w; x+=8, ++i) { | |
for (b=0; b < 8; ++b) { | |
out[y*w + x + b] = (0 != (in->array[i] & (1 << b))) ? 0x00 : 0xff; | |
} | |
} | |
} | |
return new Fl_RGB_Image(out, w, h, 1); | |
} | |
int main(void) | |
{ | |
Fl_Double_Window *win = new Fl_Double_Window(400, 400, "Rotate Test"); | |
Fl_Box *b1 = new Fl_Box(0, 0, win->w(), win->h()/2); | |
Fl_Box *b2 = new Fl_Box(0, win->h()/2 - 20, win->w(), win->h()/2); | |
//Fl_PNG_Image *rgb = new Fl_PNG_Image("test.png"); // d()==4 | |
//Fl_JPEG_Image *rgb = new Fl_JPEG_Image("test.jpg"); // d()==3 | |
//Fl_RGB_Image *rgb = new Fl_RGB_Image(rgb_array, 10, 8, 1, 15); | |
Fl_XBM_Image *xbm = new Fl_XBM_Image("srs.xbm"); | |
Fl_RGB_Image *rgb = convert_bitmap_to_rgb(xbm); | |
delete xbm; | |
//Fl_GIF_Image *gif = new Fl_GIF_Image("test.gif"); | |
//Fl_RGB_Image *rgb = new Fl_RGB_Image(gif, Fl_Color(0)); | |
//delete gif; | |
Fl_RGB_Image *rot = modify_rgb_image(ROTATE_COUNTER_CLOCKWISE, rgb); | |
int sc = 2; | |
if (rgb) { | |
b1->image(rgb->copy(rgb->w()*sc, rgb->h()*sc)); | |
delete rgb; | |
} | |
if (rot) { | |
b2->image(rot->copy(rot->w()*sc, rot->h()*sc)); | |
delete rot; | |
} | |
win->end(); | |
win->show(); | |
Fl::run(); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment