Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h.>
- #include <stdbool.h>
- #include "transformations.h"
- /*
- // Online C compiler to run C program online
- #include <stdio.h>
- int main() {
- int arr[] = {1,2,3,4,5,6,7,8,9};
- int w = 3;
- int h = 3;
- for(int i = 0; i < w*h; i++){
- printf("%d", arr[i]);
- }
- printf("\n");
- int new_arr[w*h];
- int idx = 0;
- for(int i = 0; i < w; i++){
- for(int j = 0; j < h; j++){
- new_arr[idx] = arr[ ((j+1)*w)-i-1 ];
- idx++;
- }
- }
- for(int i = 0; i < w*h; i++){
- printf("%d", new_arr[i]);
- }
- printf("\n");
- return 0;
- }
- */
- struct bmp_image* rotate_left(const struct bmp_image* image){
- if(image == NULL || image->header == NULL || image->data == NULL){
- return NULL;
- }
- struct bmp_image* new_image = malloc(sizeof (struct bmp_image*));
- new_image = rotate_right(image);
- struct bmp_image* new_image2 = rotate_right(new_image);
- struct bmp_image* new_image3 = rotate_right(new_image2);
- return new_image3;
- // if(image == NULL || image->header == NULL || image->data == NULL){
- // return NULL;
- // }
- //
- // struct bmp_image* new_image = malloc(sizeof (struct bmp_image*));
- //
- // new_image->header = image->header;
- // new_image->data = malloc(sizeof (struct pixel*)*image->header->width*image->header->height);
- //
- // uint32_t old_width = image->header->width;
- // uint32_t old_height = image->header->height;
- //
- // new_image->header->width = old_height;
- // new_image->header->height = old_width;
- //
- // uint32_t idx = 0;
- //
- // for(uint32_t i = 0; i < old_width; i++){
- // for(uint32_t j = 0; j < old_height; j++){
- // new_image->data[idx] = image->data[ ((j)*old_width)+i ];
- // idx++;
- // }
- // }
- //
- // return new_image;
- }
- struct bmp_image* rotate_right(const struct bmp_image* image){
- if(image == NULL || image->header == NULL || image->data == NULL){
- return NULL;
- }
- struct bmp_image* new_image = malloc(sizeof (struct bmp_image*));
- new_image->header = image->header;
- new_image->data = malloc(sizeof (struct pixel*)*image->header->width*image->header->height);
- uint32_t old_width = image->header->width;
- uint32_t old_height = image->header->height;
- new_image->header->width = old_height;
- new_image->header->height = old_width;
- uint32_t idx = 0;
- for(uint32_t i = 0; i < old_width; i++){
- for(uint32_t j = 0; j < old_height; j++){
- new_image->data[idx] = image->data[ ((j+1)*old_width)-i-1 ];
- idx++;
- }
- }
- return new_image;
- }
- /*
- * for(int i = 0; i < w*h; i++){
- if( i % w == 0 ){
- int temp = i;
- for(int j = 0; j < w; j++){
- mirek[temp+j] = obr[i-j+w-1];
- }
- }
- }
- * */
- struct bmp_image* flip_horizontally(const struct bmp_image* image){
- if(image == NULL || image->header == NULL || image->data == NULL){
- return NULL;
- }
- struct bmp_image* new_image = malloc(sizeof (struct bmp_image*));
- uint32_t size = image->header->width*image->header->height;
- uint32_t w = image->header->width;
- new_image->header = image->header;
- new_image->data = malloc(sizeof (struct pixel*)*size);
- for(uint32_t i = 0; i < size; i++){
- if( i % w == 0){
- uint32_t temp = i;
- for(uint32_t j = 0; j < image->header->width; j++){
- new_image->data[temp+j] = image->data[i-j+w-1];
- }
- }
- }
- return new_image;
- }
- struct bmp_image* flip_vertically(const struct bmp_image* image){
- if(image == NULL || image->header == NULL || image->data == NULL){
- return NULL;
- }
- struct bmp_image* new_image = malloc(sizeof (struct bmp_image*));
- new_image = rotate_right(image);
- struct bmp_image* new_image2 = rotate_right(new_image);
- struct bmp_image* new_image3 = flip_horizontally(new_image2);
- // return new_image2;
- return new_image3;
- }
- struct bmp_image* extract(const struct bmp_image* image, const char* colors_to_keep){
- if(image == NULL || image->header == NULL || image->data == NULL || colors_to_keep == NULL || strlen(colors_to_keep) == 0){
- return NULL;
- }
- struct bmp_image* new_image = malloc(sizeof (struct bmp_image*));
- uint32_t size = image->header->width*image->header->height;
- new_image->header = image->header;
- new_image->data = malloc(sizeof (struct pixel*)*size);
- bool r = true;
- bool g = true;
- bool b = true;
- for(int i = 0; i < strlen(colors_to_keep); i++){
- if(colors_to_keep[i] == 'r'){
- r = false;
- break;
- }
- }
- for(int i = 0; i < strlen(colors_to_keep); i++){
- if(colors_to_keep[i] == 'g'){
- g = false;
- break;
- }
- }
- for(int i = 0; i < strlen(colors_to_keep); i++){
- if(colors_to_keep[i] == 'b'){
- b = false;
- break;
- }
- }
- for(uint32_t i = 0; i < size; i++){
- if(r){
- new_image->data[i].red = 0;
- } else {
- new_image->data[i].red = image->data[i].red;
- }
- if(g){
- new_image->data[i].green = 0;
- } else {
- new_image->data[i].green = image->data[i].green;
- }
- if(b){
- new_image->data[i].blue = 0;
- } else {
- new_image->data[i].blue = image->data[i].blue;
- }
- }
- return new_image;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement