8000 Crop feature added by vidojesevic · Pull Request #9 · vidojesevic/imgprocesslib · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Crop feature added #9

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading 8000
Diff view
Diff view
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# See LICENSE file for copyright and license details.

CC = gcc
SRC = ipl.c prompt.c cli.c resize.c
SRC = ipl.c prompt.c cli.c resize.c crop.c rotate.c
OUT = ipl
INSTALL_DIR = /usr/local/bin

Expand Down
44 changes: 37 additions & 7 deletions cli.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,15 @@
#include <getopt.h>
#include "cli.h"
#include "resize.h"
#include "crop.h"
#include "rotate.h"

unsigned char *imgData;
char *fileSize;
Dime dime;
Flip flip;

void parseArguments(int argc, char *argv[], Pics *img, Input *input) {
void parseArguments(int argc, char *argv[], Pics *img, Input *input, Crop *crop) {
if (argc == 2) {
int i = argc - 1;
if (strcmp(argv[i], "-h") != 0 && strcmp(argv[i], "--help") != 0 && strcmp(argv[i], "-v") != 0 && strcmp(argv[i], "--version") != 0) {
Expand Down Expand Up @@ -71,17 +74,44 @@ void parseArguments(int argc, char *argv[], Pics *img, Input *input) {

if (strcmp(cli_option, "-r") == 0) {
resizeCLI(img, &dime, argc, argv);
}
if (strcmp(cli_option, "-c") == 0) {
printf("Crop\n");
}
if (strcmp(cli_option, "-f") == 0) {
printf("Rotate\n");
} else if (strcmp(cli_option, "-c") == 0) {
cropCLI(img, argc, argv, crop);
} else if (strcmp(cli_option, "-f") == 0) {
rotateCLI(img, argc, argv, &flip);
}

}
free(fileSize);
}
void rotateCLI(Pics *img, int argc, char *argv[], Flip *flip) {
printf("Candy flip\n");
rotate(img, flip);
}

void cropCLI(Pics *img, int argc, char *argv[], Crop *crop) {
char *cropXarg = argv[3];
char *cropYarg = argv[5];

if (strcmp(cropXarg, "-x") == 0 && strcmp(cropYarg, "-y") == 0) {
strcpy(img->name, argv[7]);
printf("Crop madafaka\n");
crop->x = atoi(argv[4]);
crop->y = atoi(argv[6]);
} else if (strcmp(cropXarg, "-b") == 0) {
strcpy(img->name, argv[5]);
crop->x = crop->y = atoi(argv[4]);
} else {
perror("Error: Input argument failed! Use -x and -y");
exit(EXIT_FAILURE);
}
findOutExtension(img->name, img->ext);

if (crop->x >= img->width || crop->y >= img->height) {
perror("Error: Values of X and Y must be less than original width and height!");
exit(EXIT_FAILURE);
}
cropImage(img, crop);
}

void resizeCLI(Pics *img, Dime *dime, int argc, char *argv[]) {
char *resizeOption = argv[3];
< 8000 /a> Expand Down
4 changes: 3 additions & 1 deletion cli.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,10 @@

#define NUM_ARGC 11

void parseArguments(int argc, char *argv[], Pics *img, Input *input);
void parseArguments(int argc, char *argv[], Pics *img, Input *input, Crop *crop);
void resizeCLI(Pics *img, Dime *dime, int argc, char *argv[]);
void cropCLI(Pics *img, int argc, char *argv[], Crop *crop);
void rotateCLI(Pics *img, int argc, char *argv[], Flip *flip);
// this was bad idea xD
// int checkArgv(char *argv[NUM_ARGC], char *option);
// Action performAction(const Action *action);
Expand Down
51 changes: 48 additions & 3 deletions crop.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,55 @@
#include <string.h>
#include <sys/stat.h>
#include "prompt.h"
#include "cli.h"

stbi_load(filename, &x, &y, &n, 0);
unsigned char *stbi_load(const char* filename, int *x, int *y, int *n, int desiredChannels);

void cropImage(Pics *img, Crop crop) {
printf("CROP\n");
void cropImage(Pics *img, Crop *crop) {

printf("X: %d, Y: %d, border: %d\n", crop->x, crop->y, crop->border);
int newWidth = img->width - 2 * crop->x;
int newHeight = img->height - 2 * crop->y;

if (newWidth <= 0 || newHeight <= 0) {
perror("Error: invalid crop dimensions!\n");
exit(EXIT_FAILURE);
}
// printf("New width: %d,new height: %d\n", newWidth, newHeight);
unsigned char *croppedData = (unsigned char*)malloc(newWidth * newHeight * img->channel);

if (croppedData == NULL) {
perror("Error: Memory alocation failed!");
exit(EXIT_FAILURE);
}

char outputFilename[PATH_SIZE]; // Define a buffer for the output filename
snprintf(outputFilename, sizeof(outputFilename), "cropped_%s", img->name);

for (int y = crop->y; y < crop->y + newHeight; y++) {
for (int x = crop->x; x < crop->x + newWidth; x++) {
for (int c = 0; c < img->channel; c++) {
croppedData[(y - crop->y) * newWidth * img->channel + (x - crop->x) * img->channel + c] =
img->data[y * img->width * img->channel + x * img->channel + c];
}
}
}

printf("Jel ovde problem?");
saveCroppedImage(croppedData, newWidth, newHeight, img->channel, outputFilename, img->ext, img->quality);

free(img->data);
img->data = croppedData;
img->width = newWidth;
img->height = newHeight;
}

// for (int y = crop->y; y < img->height; y++) {
// for (int x = crop->x; x < img->width; x++) {
// for (int c = 0; c < img->channel; c++) {
// croppedData[(y - crop->y) * newWidth * img->channel + (x - crop->x) * img->channel + c] =
// img->data[y * img->width * img->channel + x * img->channel + c];
// }
// }
// }

4 changes: 2 additions & 2 deletions crop.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@
* along with this program. If not, see <https://www.gnu.org/licenses/gpl-3.0.txt>
* */
#ifndef CROP_H
#define CROP
#define CROP_H

//dependencies
#include "ipl.h"
#include "prompt.h"

// Functions
void cropImage(Pics *img, Crop crop);
void cropImage(Pics *img, Crop *crop);

#endif

86 changes: 86 additions & 0 deletions ipl.1
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,93 @@ want to process an image located at assets/img/logo.png, you can simply enter
assets/img/logo.png as the image name. Similarly, you can enter relative paths
using the ../ notation. For example, if the image is located two directories
above the current directory, you can enter ../../directory/image.png.
.SH CROP
.TP
.BR -c
Crop a specific region from an image. This option allows you to select a rectangular
region within the image and create a new image containing only that region. When prompted,
you can specify the X and Y coordinates of the top-left corner of the region, as well as
the width and height of the region.
.TP
.BR -x
The X coordinate of the top-left corner for cropping or resizing.

.TP
.BR -y
The Y coordinate of the top-left corner for cropping or resizing.

.TP
.BR -b
Use with cropping to specify an equal border for cropping from all sides.
Just specify border without 'x' and 'y'.

.SH RESIZE
.TP
.BR -r
Resize an image to a custom size.

.TP
.BR --background
Resize an image for use as a background. This option resizes the image to fit the screen,
considering common aspect ratios for desktop backgrounds.

.TP
.BR --hero
Resize an image to be used as a hero image on a website. This option resizes the image to
common hero image dimensions, suitable for banners and headers.

.TP
.BR --web-banner
Resize an image for use as a website banner. The recommended dimensions are 250 x 250 with
an aspect ratio of 1:1.

.TP
.BR --blog
Resize an image for use in a blog post. The recommended dimensions are 1200 x 630 with
an aspect ratio of 3:2.

.TP
.BR --logo-rec
Resize an image for use as a rectangular logo. The recommended dimensions are 250 x 100 with
an aspect ratio of 2:3.

.TP
.BR --logo-sq
Resize an image for use as a square logo. The recommended dimensions are 100 x 100 with
an aspect ratio of 1:1.

.TP
.BR --favicon
Resize an image for use as a favicon. The recommended dimensions are 16 x 16 with an
aspect ratio of 1:1.

.TP
.BR --social
Resize an image for use as a social media icon. The recommended dimensions are 32 x 32 with
an aspect ratio of 1:1.

.TP
.BR --lightbox
Resize an image for use in a lightbox (full-screen view). The recommended dimensions are
1600 x 500 with an aspect ratio of 16:9.

.TP
.BR --thumb
Resize an image for use as a thumbnail. The recommended dimensions are 150 x 150 with
an aspect ratio of 1:1.

.TP
.BR --custom
Resize an image to a custom size. Use this option if you want to specify your own width and
height for resizing.

.TP
.BR -w
Specify the width for resizing.

.TP
.BR -h
Specify the height for resizing.
.SH EXAMPLES
Resize an image to a custom size:
.nf
Expand Down
Loading
0