Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- void putPixel(png_byte *cur_row, int x, int y, int r, int g, int b, int a = 255) {
- png_byte *ptr = &(cur_row[(x*4)]);
- ptr[0] = static_cast<png_byte>(r);
- ptr[1] = static_cast<png_byte>(g);
- ptr[2] = static_cast<png_byte>(b);
- ptr[3] = static_cast<png_byte>(a);
- }
- void drawLine(struct Png *image, int x1, int y1, int x2, int y2, int r, int g, int b) {
- const int deltaX = abs(x2 - x1);
- const int deltaY = abs(y2 - y1);
- const int signX = x1 < x2 ? 1 : -1;
- const int signY = y1 < y2 ? 1 : -1;
- int error = deltaX - deltaY;
- if (x2 >= 0 && x2 < image -> width && y2 >= 0 && y2 < image -> height)
- putPixel(image -> row_pointers[y2], x2, y2, r, g, b);
- while(x1 != x2 || y1 != y2)
- {
- if (x1 >= 0 && x1 < image -> width && y1 >= 0 && y1 < image -> height)
- putPixel(image -> row_pointers[y1], x1, y1, r, g, b);
- const int error2 = error * 2;
- if(error2 > -deltaY)
- {
- error -= deltaY;
- x1 += signX;
- }
- if(error2 < deltaX)
- {
- error += deltaX;
- y1 += signY;
- }
- }
- }
- void drawCircle(struct Png *image, int xc, int yc, int inner, int outer, int R, int G, int B)
- {
- int xo = outer;
- int xi = inner;
- int y = 0;
- int erro = 1 - xo;
- int erri = 1 - xi;
- while(xo >= y) {
- drawLine(image, xc + xi, yc + y, xc + xo, yc + y, R, G, B);
- drawLine(image, xc + y, yc + xi, xc + y, yc + xo, R, G, B);
- drawLine(image, xc - xo, yc + y, xc - xi, yc + y, R, G, B);
- drawLine(image, xc - y, yc + xi, xc - y, yc + xo, R, G, B);
- drawLine(image, xc - xo, yc - y, xc - xi, yc - y, R, G, B);
- drawLine(image, xc - y, yc - xo, xc - y, yc - xi, R, G, B);
- drawLine(image, xc + xi, yc - y, xc + xo, yc - y, R, G, B);
- drawLine(image, xc + y, yc - xo, xc + y, yc - xi, R, G, B);
- y++;
- if (erro < 0) {
- erro += 2 * y + 1;
- } else {
- xo--;
- erro += 2 * (y - xo + 1);
- }
- if (y > inner) {
- xi = y;
- } else {
- if (erri < 0) {
- erri += 2 * y + 1;
- } else {
- xi--;
- erri += 2 * (y - xi + 1);
- }
- }
- }
- }
- void frameType1(struct Png *image, int width, int R, int G, int B) {
- for (int y = image -> height - width;;y -= width) {
- for (int j = y; j >= max(0, y - width); j--) {
- drawLine(image, 0, j, image -> width -1 - j, image -> height - 1, 255 - R, 255 - G, 255 - B);
- }
- y -= width;
- if (y < 0)
- break;
- }
- int ty = image -> height - 1;
- for (int j = ty; j >= max(0, ty - width + (image -> width % width)); j--) {
- drawLine(image, image -> width - 1, j, image -> width -1 - j, 0, 255 - R, 255 - G, 255 - B);
- }
- //ty -= width + (image -> width % width);
- for (int y = image -> width - 1;;y -= width) {
- for (int j = y; j >= max(0, y - width); j--) {
- drawLine(image, image -> width - 1, j, image -> width -1 - j, 0, 255 - R, 255 - G, 255 - B);
- }
- y -= width;
- if (y < 0)
- break;
- }
- }
- void frameType2(struct Png *image, int width, int R, int G, int B) {
- for (int y = 0;; y += width / 2) {
- for (int x = 0;; x += width / 2) {
- drawCircle(image, x, y, width / 2 - width / 20, width / 2, 255 - R, 255 - G, 255 - B);
- if (x >= image -> width)
- break;
- }
- if (y >= image -> height)
- break;
- }
- }
- void frameType3(struct Png *image, int width) {
- int step = width / 7;
- for (int y = 0;;y += step) {
- for (int x = 0;; x += step) {
- int r = rand() % 256;
- int g = rand() % 256;
- int b = rand() % 256;
- for (int i = y; i < min(y + step, image -> height); i++)
- for (int j = x; j < min(x + step, image -> width); j++) {
- putPixel(image->row_pointers[i], j, i, r, g, b);
- }
- if (x >= image -> width)
- break;
- }
- if (y >= image -> height)
- break;
- }
- }
- void drawFrame(struct Png *image, int R, int G, int B, int width, int type) {
- int wid = image -> width;
- int hei = image -> height;
- png_bytep *new_row_pointers =(png_bytep *) malloc(sizeof(png_bytep) * hei);
- for(int y = 0; y < hei; y++)
- {
- new_row_pointers[y] = (png_byte *)malloc(sizeof(png_byte)*4 * wid);
- png_byte *new_row = new_row_pointers[y];
- png_byte *cur_row = image->row_pointers[y];
- for (int x = 0; x < wid; x++)
- {
- png_byte *ptr = &(cur_row[(x*4)]);
- putPixel(new_row, x, y, ptr[0], ptr[1], ptr[2], ptr[3]);
- }
- }
- int lenX = image -> width + 2 * width;
- int lenY = image -> height + 2 * width;
- image ->row_pointers = (png_bytep *) realloc(image -> row_pointers, lenY * sizeof(png_bytep));
- for (int i = 0; i < lenY; i++) {
- image->row_pointers[i] = (png_byte *) realloc(image->row_pointers[i], sizeof(png_byte)*4 * lenX);
- }
- image -> width = lenX;
- image -> height = lenY;
- for (int i = 0; i < image -> height; i++) {
- png_byte *cur_row = image->row_pointers[i];
- for (int j = 0; j < image -> width; j++) {
- putPixel(cur_row, j, i, R, G, B, 255);
- }
- }
- switch (type) {
- case 1:
- frameType1(image, width, R, G, B);
- break;
- case 2:
- frameType2(image, width, R, G, B);
- break;
- case 3:
- frameType3(image, width);
- break;
- }
- for(int y = 0; y < hei; y++) {
- png_byte *new_row = new_row_pointers[y];
- png_byte *ch_row = image->row_pointers[width + y];
- for (int x = 0; x < wid; x++)
- {
- png_byte *toptr = &(new_row[x*4]);
- putPixel(ch_row, width + x, y, toptr[0], toptr[1], toptr[2], toptr[3]);
- }
- }
- for (int y = 0; y < hei; y++)
- free(new_row_pointers[y]);
- free(new_row_pointers);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement