cs240/hw/hw6/HW6-src-update/task1/intensity.c
2018-10-15 17:20:57 -04:00

83 lines
2.0 KiB
C
Executable File

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <libgen.h>
#include "../lib/imageio.h"
void tackName(char* fileAndPath, char* ending, char* dest);
int main(int argc,char * argv[]){
if (argc != 3) {
printf("Usage: intensity <file path> <intensity>\n");
return -1;
}
IMAGE org = image_open(argv[1]);
if (org == NULL) {
printf("Failed to open file: %s\n", argv[1]);
return -1;
}
float intensity = (float) atof(argv[2]);
IMAGE clone = image_clone(org);
if (clone == NULL) {
printf("Failed to clone image.\n");
return -1;
}
image_close(org);
int width = image_width(clone);
int height = image_height(clone);
int size = width * height;
int* pixels = image_pixels(clone);
unsigned char* pixelChannel; // = pixels[0] + 1;
// 0xAABBGGRR
for (int i = 0; i < size; i++) {
//pixelChannel = (unsigned char*) (&pixels + i);
for (int j = 0; j < 3; j++) { // BB, GG, RR...
pixelChannel = (unsigned char*) (&pixels[i]) + j;
// Skip alpha channel.
//char temp = *pixelChannel;
//*pixelChannel = (char) (*pixelChannel * intensity);
//printf("%d * %f = %f or %d -> 0x%x\n", *pixelChannel, intensity, *pixelChannel * intensity, (unsigned char) (*pixelChannel * intensity), (unsigned char) (*pixelChannel * intensity));
if ( (float) *pixelChannel * intensity > 0xFF) {
*pixelChannel = 0xFF; // Cap it.
} else {
*pixelChannel = (unsigned char) (*pixelChannel * intensity);
}
//printf("{%d/%d}[%p]: %d -> %d\n", i, size, pixelChannel, temp, *pixelChannel);
}
}
char fileStr[strlen(argv[1]) + 8];
tackName(argv[1], "_int.jpg", fileStr);
if (image_save(clone, fileStr)) {
printf("Failure while saving image.\n");
return -1;
}
image_close(clone);
return 0;
}
void tackName(char* fileAndPath, char* ending, char* dest) {
int index;
char* file = basename(fileAndPath);
for (index = strlen(file) - 1; index > 0; index--) {
if (file[index] == '.') {
break;
}
}
strncpy(dest, file, index);
dest[index] = '\0';
strcat(dest, ending);
}