裁剪图像数组

时间:2014-03-21 18:20:16

标签: c arrays image int crop

我尝试通过创建一个由用户指定的变量然后从图像的宽度和高度的最大值中减去该变量来裁剪图像。  问题是,当我使用减法运算符而不是HEIGHT - input = number时,我得到一个奇怪的数字 - -180000 ...

这是我的代码

#define _CRT_SECURE_NO_DEPRECATE 1
#include<stdio.h>
#include<stdlib.h>
#include <string.h>
//#include "functions.h"
#define _CRT_SECURE_NO_WARNINGS 1
#define HEIGHT 128
#define WIDTH 256
int image02[HEIGHT][WIDTH][3];
int image03[HEIGHT][WIDTH][3];
int x, y;

FILE*RED = NULL;
FILE*BLUE = NULL;
FILE*GREEN = NULL;
FILE*REDRead = NULL;
FILE*BLUERead = NULL;
FILE*GREENRead = NULL;
FILE*Write1 = NULL;
char cropans [30];
char red [30];
char green [30];
char blue [30];
char ppm [30];
char rotans [30];
int xcrop;
int ycrop;
int newheight;
int newwidth;

void main()
{

    printf("Type in filenames of the image files, including extensions\nThe program will exit if it cannot read any files\n");  //this is the UI//
    printf("Red channel data:  ");  
        scanf("%s", &red);
        REDRead = fopen(red, "r");  
        if (REDRead == NULL)
        {
            printf("\nCannot read red image data\n");
            exit(0);
        }
    printf("Green channel data:  ");
        scanf("%s", &green);
        GREENRead = fopen(green, "r");
        if (GREENRead == NULL)
        {
            printf("\nCannot read green image data\n");
            exit(0);
        }
    printf("Blue channel data:  ");
        scanf("%s", &blue); 
        BLUERead = fopen(blue, "r");
        if (BLUERead == NULL)
        {
            printf("\nCannot read blue image data\n");
            exit(0);
        }


    printf("\nNow type in the name of the file you want to create, with extension:\n");
        scanf("%s", &ppm);
    printf("\nWould you like to rotate a channel, or create an image without rotating any channels?(y/n)\n");
        scanf("%s", &rotans);
    printf("\nWould you like to crop the image? (y/n)"); 
        scanf("%s", &cropans); 
        if (strcmp( cropans, "y") == 0)
            {
            printf("\ntype number of pixels to crop from x axis\n");
            scanf("%s", &xcrop);
            printf("\ntype number of pixels to crop from y axis\n");
            scanf("%s", &ycrop);            
            }
        else if(strcmp( cropans, "n") == 0);
        {
            printf("hi");
        }
    printf("got input correctly\n");


    //Zeroing the seperate arrays//
    for (y = 0; y<HEIGHT; y++)                                  
    {
        for (x = 0; x<WIDTH; x++)                               
        {
            image02[y][x][0] = 0;                                   
            image02[y][x][1] = 0;                                   
            image02[y][x][2] = 0;                                   
        }
    }


    for (y = 0; y<HEIGHT; y++)                                      
    {
        for (x = 0; x<WIDTH; x++)                                   
        {
            image03[y][x][0] = 0;                                   
            image03[y][x][1] = 0;                                   
            image03[y][x][2] = 0;                                   
        }
    }

    {
        int newwidth = (WIDTH - xcrop);
        int newheight = (HEIGHT - ycrop);
    }
    //printing to file part//
    //no rotation//

    if(strcmp( rotans, "n") == 0)
        {
            for (y = 0; y<HEIGHT; y++)
            {
                for (x = 0; x<WIDTH; x++)
                    {
                    fscanf(REDRead,"%d", &image03[y][x][0]);
                    fscanf(GREENRead, "%d",&image03[y][x][1]);
                    fscanf(BLUERead,"%d", &image03[y][x][2]);
                    }
            }   

        Write1 = fopen(ppm, "w");
        printf("got here 1");
        fprintf(Write1,"P3\n %d %d \n 255\n", WIDTH, HEIGHT);
        for (y = 0; y<(HEIGHT); y++)
        {
            for (x = 0; x<(WIDTH); x++)
                {
                fprintf(Write1,"%d %d %d ", image03[y][x][0], image03[y][x][1], image03[y][x][2]);
                }
        }
        printf("got here 1");
        }

else if (strcmp( rotans, "y") == 0)
    {
        for (y = 0; y<HEIGHT; y++)
        {
            for (x = 0; x<WIDTH; x++)
            {
                fscanf(REDRead,"%d", &image02[y][x][0]);
                fscanf(BLUERead,"%d", &image02[y][x][2]);
            }
        }

        for (y = HEIGHT; y>0; y--)
        {
            for (x = WIDTH; x>0; x--)
            {
                fscanf(GREENRead,"%d", &image02[y][x][1]);
            }
        }

        Write1 = fopen(ppm, "w");
        printf("got here 1");
        fprintf(Write1,"P3\n %d %d \n 255\n", WIDTH, HEIGHT);
        for (y = 0; y<HEIGHT; y++)
        {
            for (x = 0; x<WIDTH; x++)
            {
            fprintf(Write1,"%d %d %d ", image02[y][x][0], image02[y + 11][x + 11][1], image02[y][x][2]);
            }
        }
        printf("got here 1");
    }

}

我很遗憾,任何帮助都会受到赞赏。 :)

2 个答案:

答案 0 :(得分:0)

您已分配给新创建的newheightnewwidth,它们是在您声明/分配它们的块中创建的。因为它们会在该块结束时消失,所以您将离开原始版本,而这些版本从未被分配过!

答案 1 :(得分:0)

您对%sxcrop使用ycrop。您需要使用%d。换句话说,这一行

scanf("%s", &xcrop);

应该是

scanf("%d", &xcrop);