Skip to content

Commit

Permalink
first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
pp-Innovate committed Jan 12, 2017
0 parents commit a824934
Show file tree
Hide file tree
Showing 14 changed files with 2,409 additions and 0 deletions.
24 changes: 24 additions & 0 deletions jpeg/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
CC=gcc
LD=gcc

CFLAGS=-g -Wall -O2

#下面两行分别是jpeglib.h和libjpeg.so所在的路径,请根据实际修改
INCDIR=/usr/include/
LIBDIR=/usr/lib/arm-linux-gnueabihf/
LIBS=-ljpeg

demo:main.o imgFile.o imgProcess.o
$(LD) -o $@ $^ $(CFLAGS) -L$(LIBDIR) $(LIBS)

main.o:main.c imgFile.h imgProcess.h
$(CC) -c -o $@ main.c $(CFLAGS) -I$(INCDIR)

imgFile.o:imgFile.c imgFile.h
$(CC) -c -o $@ imgFile.c $(CFLAGS) -I$(INCDIR)

imgProcess.o:imgProcess.c imgProcess.h
$(CC) -c -o $@ imgProcess.c $(CFLAGS) -I$(INCDIR)

clean:
rm -rf *.o demo
99 changes: 99 additions & 0 deletions jpeg/imgFile.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
#include "imgFile.h"

int imread(char *fileName,ImageTypeDef *outImg)
{
FILE *fp;

if(!(fp = fopen(fileName,"rb")))
return -1;

struct jpeg_decompress_struct cinfo;
struct jpeg_error_mgr jerr;

cinfo.err = jpeg_std_error(&jerr);
jpeg_create_decompress(&cinfo);
jpeg_stdio_src(&cinfo,fp);
jpeg_read_header(&cinfo,true);

cinfo.out_color_space = JCS_YCbCr;

jpeg_start_decompress(&cinfo);

outImg->width = cinfo.output_width;
outImg->height = cinfo.output_height;
outImg->maxIntensity = 255;

long int pixels = outImg->width*outImg->height;

outImg->data = (unsigned char *)malloc(pixels);

unsigned char *buffer = (unsigned char *)malloc(cinfo.output_width*cinfo.output_components);

unsigned char *ptr = (unsigned char *)outImg->data;
while(cinfo.output_scanline < cinfo.output_height)
{
jpeg_read_scanlines(&cinfo,(JSAMPARRAY)&buffer,1);

int i;
for(i = 0;i < cinfo.output_width;i++)
{
*ptr = *(buffer + i*cinfo.output_components);
ptr++;
}
}

free(buffer);
jpeg_finish_decompress(&cinfo);
jpeg_destroy_decompress(&cinfo);

fclose(fp);

return 0;
}

int imwrite(char *fileName,ImageTypeDef *inImg)
{
FILE *fp;

if(!(fp = fopen(fileName,"wb")))
return -1;

struct jpeg_compress_struct cinfo;
struct jpeg_error_mgr jerr;

cinfo.err = jpeg_std_error(&jerr);
jpeg_create_compress(&cinfo);
jpeg_stdio_dest(&cinfo,fp);

cinfo.image_width = inImg->width;
cinfo.image_height = inImg->height;
cinfo.input_components = 3;
cinfo.in_color_space = JCS_YCbCr;

jpeg_set_defaults(&cinfo);
jpeg_start_compress(&cinfo,true);

unsigned char *buffer = (unsigned char *)malloc(cinfo.image_width*cinfo.input_components);
memset(buffer,127,cinfo.image_width*cinfo.input_components);

unsigned char *ptr = (unsigned char *)inImg->data;
while(cinfo.next_scanline < cinfo.image_height)
{
int i;
for(i = 0;i < cinfo.image_width;i++)
{
*(buffer + i*cinfo.input_components) = *ptr;
ptr++;
}

jpeg_write_scanlines(&cinfo,(JSAMPARRAY)&buffer,1);
}

free(buffer);
jpeg_finish_compress(&cinfo);
jpeg_destroy_compress(&cinfo);

fclose(fp);

return 0;
}
22 changes: 22 additions & 0 deletions jpeg/imgFile.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#ifndef __IMGFILE_H
#define __IMGFILE_H

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#include <jpeglib.h>

typedef struct
{
int width;
int height;
int maxIntensity;
void *data;
}ImageTypeDef;

int imread(char *fileName,ImageTypeDef *outImg);
int imwrite(char *fileName,ImageTypeDef *inImg);

#endif /* __IMGFILE_H */

29 changes: 29 additions & 0 deletions jpeg/imgProcess.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#include "imgProcess.h"

int thresholding(ImageTypeDef *inImg,ImageTypeDef *outImg,int threshold)
{
int pixels = inImg->width*inImg->height;

outImg->width = inImg->width;
outImg->height = inImg->height;
outImg->maxIntensity = 255;
outImg->data = (unsigned char *)malloc(pixels);

int i;

if(inImg->maxIntensity <= 255)
for(i = 0;i < pixels;i++)
if(*((unsigned char *)inImg->data + i) < threshold)
*((unsigned char *)outImg->data + i) = 0;
else
*((unsigned char *)outImg->data + i) = 255;
else
for(i = 0;i < pixels;i++)
if(*((unsigned short *)inImg->data + i) < threshold)
*((unsigned char *)outImg->data + i) = 0;
else
*((unsigned char *)outImg->data + i) = 255;

return 0;
}

10 changes: 10 additions & 0 deletions jpeg/imgProcess.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#ifndef __IMGPROCESS_H
#define __IMGPROCESS_H

#include <stdio.h>
#include "imgFile.h"

int thresholding(ImageTypeDef *inImg,ImageTypeDef *outImg,int threshold);

#endif /* __IMGPROCESS_H */

101 changes: 101 additions & 0 deletions jpeg/main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
#include <stdio.h>
#include <stdbool.h>
#include <string.h>
#include <unistd.h>
#include <sys/time.h>
#include "imgFile.h"
#include "imgProcess.h"

void printHelpMessage(void);
bool checkFileName(char *fileName);

char inputFileName[256];
char outputFileName[256];
int threshold = 128;

int main(int argc, char *argv[])
{
int opt;

if(argc == 1)
{
printHelpMessage();
return 0;
}

while((opt = getopt(argc,argv,"hi:o:t:")) != -1)
{
switch(opt)
{
case 'h':printHelpMessage();return 0;
case 'i':strcpy(inputFileName,optarg);break;
case 'o':strcpy(outputFileName,optarg);break;
case 't':threshold = atoi(optarg);break;
default:printf("Invalid argument %c\n",opt);return -1;
}
}

if(checkFileName(inputFileName) != true)
{
printf("Invalid input file %s\n",inputFileName);
return -1;
}

if(checkFileName(outputFileName) != true)
{
printf("Invalid output file %s\n",outputFileName);
return -1;
}

ImageTypeDef img,img_thr;
struct timeval before,after;

if(imread(inputFileName,&img))
{
printf("Can not read file: %s\n",inputFileName);
return -1;
}

gettimeofday(&before,NULL);
thresholding(&img,&img_thr,threshold);
gettimeofday(&after,NULL);

if(imwrite(outputFileName,&img_thr))
{
printf("Can not save file: %s\n",outputFileName);
return -1;
}

free(img.data);
free(img_thr.data);

printf("Thresholding completed, using %ldus\n",(after.tv_sec - before.tv_sec)*1000000 + (after.tv_usec - before.tv_usec));

return 0;
}

bool checkFileName(char *fileName)
{
char *ptr;

ptr = strrchr(fileName,'.');
if(!ptr)
return false;
else if(strcmp(ptr,".jpg") != 0)
return false;
else
return true;
}

void printHelpMessage(void)
{
printf("This is a image processing demo.\n");
printf("Author: 彭澎,[email protected]\n\n");
printf("Usage:\n");
printf("\t demo [options] ...\n\n");
printf("Options:\n");
printf("\t-h\tprint this help message.\n");
printf("\t-i\tinput image file, it should be a .jpg file.\n");
printf("\t-o\toutput image file, it should be a .jpg file.\n");
printf("\t-t\tset threshold, default 128.\n");
}
Binary file added jpeg/test.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
23 changes: 23 additions & 0 deletions pgm/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
CC=gcc
LD=gcc

CFLAGS=-g -Wall -O2

INCDIR=
LIBDIR=
LIBS=

demo:main.o imgFile.o imgProcess.o
$(LD) -o $@ $^ $(CFLAGS)

main.o:main.c imgFile.h imgProcess.h
$(CC) -c -o $@ main.c $(CFLAGS)

imgFile.o:imgFile.c imgFile.h
$(CC) -c -o $@ imgFile.c $(CFLAGS)

imgProcess.o:imgProcess.c imgProcess.h
$(CC) -c -o $@ imgProcess.c $(CFLAGS)

clean:
rm -rf *.o demo
72 changes: 72 additions & 0 deletions pgm/imgFile.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
#include "imgFile.h"

int imread(char *fileName,ImageTypeDef *outImg)
{
FILE *fp;

/* 打开文件 */
if(!(fp = fopen(fileName,"r")))
return -1;

char type[3];

/* 格式化读取pgm图像文件的信息头 */
fscanf(fp,"%s\n%d %d\n%d\n",type,&outImg->width,&outImg->height,&outImg->maxIntensity);

/* 只针对P5类型的pgm文件 */
if(strcmp(type,"P5"))
{
if(!strcmp(type,"P2"))
printf("Do not support P2 file type!\n");
else
printf("Unknown file type!\n");

fclose(fp);

return -1;
}

long int pixels = outImg->width*outImg->height;

/* 为图像数据分配内存,并从文件中块读取图像数据 */
if(outImg->maxIntensity <= 255) //每个像素用单字节存储
{
outImg->data = (unsigned char *)malloc(pixels);
fread((unsigned char *)outImg->data,1,pixels,fp);
}
else //每个像素用两个字节存储
{
outImg->data = (unsigned short *)malloc(pixels);
fread((unsigned short *)outImg->data,2,pixels,fp);
}

/* 关闭文件流 */
fclose(fp);

return 0;
}

int imwrite(char *fileName,ImageTypeDef *inImg)
{
FILE *fp;

/* 打开文件 */
if(!(fp = fopen(fileName,"w")))
return -1;

/* 格式化输出pgm图像文件的信息头 */
fprintf(fp,"P5\n%d %d\n%d\n",inImg->width,inImg->height,inImg->maxIntensity);

long int pixels = inImg->width*inImg->height;

/* 块写入图像数据 */
if(inImg->maxIntensity <= 255)
fwrite((unsigned char *)inImg->data,1,pixels,fp);
else
fwrite((unsigned short *)inImg->data,2,pixels,fp);

/* 关闭文件流 */
fclose(fp);

return 0;
}
Loading

0 comments on commit a824934

Please sign in to comment.