int main(){ unsigned char buffer[BUF_SIZE] = {0}; unsigned int picSize=0; unsigned int packNum=0; unsigned int packSize; unsigned short int checksum; unsigned int width, height; unsigned int i,j; unsigned char bytes[2]; unsigned char image[160][120]; int fd = serialOpen("/dev/ttyUSB1", 115200); FILE* filefd = fopen("./image.ppm", "w"); unsigned char* bufptr; sendShot(fd, RES_160X128); readImageDetails(fd, &picSize, &packNum); printf("size: %d\npackets count: %d\n", picSize, packNum); unsigned char* imgBuffer = (unsigned char*) malloc((picSize+1)*sizeof(unsigned char)); readImage(fd, imgBuffer, picSize, packNum); serialClose(fd); njInit(); if (njDecode(imgBuffer, picSize)) { printf("Error decoding the input file.\n"); return 1; } printf("size:\n%d %d\n", njGetWidth(), njGetHeight()); fprintf(filefd, "P%d\n%d %d\n255\n", njIsColor() ? 6 : 5, njGetWidth(), njGetHeight()); width = njGetWidth(); height = njGetHeight(); fwrite(njGetImage(), 1, njGetImageSize(), filefd); bufptr = njGetImage(); for(i=0;i<width;i++){ for(j=0;j<height;j++){ image[i][j] = (char)((0.21 * (double)bufptr[(i+j*width)*3]) + 0.72 * (double)bufptr[(i+j*width)*3 + 1] + 0.07 * (double)bufptr[(i+j*width)*3 + 2]); } } for(j=0;j<height;j++){ for(i=0;i<width;i++){ if(image[i][j]<63) printf("@"); else if(image[i][j]<128) printf("*"); else if(image[i][j]<191) printf("+"); else printf("-"); } printf("\n"); } fclose(filefd); njDone(); }
int decodeJpegChannel(char* jpegData, int jpegSize, int channel, unsigned char** channelPixels, int* srcWidth, int* srcHeight) { int returnCode; if (channel == 0) { // Decode the red channel njInit(); returnCode = njDecode(jpegData, jpegSize, 1, 0, 0); } else if (channel == 1) { // Decode the green channel njInit(); returnCode = njDecode(jpegData, jpegSize, 0, 1, 0); } else if (channel == 2) { // Decode the blue channel njInit(); returnCode = njDecode(jpegData, jpegSize, 0, 0, 1); } if (returnCode != 0) { LOGE("Failed to njDecode()"); njDone(); return returnCode; } *srcWidth = njGetWidth(); *srcHeight = njGetHeight(); if (channel == 0) { // Get the red channel pixels *channelPixels = (unsigned char*)njGetRedImage(); } else if (channel == 1) { // Get the green channel pixels *channelPixels = (unsigned char*)njGetGreenImage(); } else if (channel == 2) { // Get the blue channel pixels *channelPixels = (unsigned char*)njGetBlueImage(); } return MEMORY_OK; }
uint8 *image_decode_jpg(uint8 *content,int32 bytes,int32 *result,int32 *x,int32 *y){ //Result:bit 1=Success,bit 2=32bit[BGRA] *result=0; static int32 init=0; if (!init){init=1; njInit();} if (njDecode(content,bytes)) return NULL; static uint8 *si; si=(uint8*)njGetImage(); static int32 w,h; w=njGetWidth(); h=njGetHeight(); static uint8 *di; if (njIsColor){//RGB //Create a buffer large enough to store image if (w*h*3!=njGetImageSize()) return NULL; di=(uint8*)malloc(w*h*4); //RGB->BGRA static int32 c; c=w*h; while(c--){ di[c*4+2]=si[c*3 ];//red di[c*4+1]=si[c*3+1];//green di[c*4 ]=si[c*3+2];//blue di[c*4+3]=255; //alpha } }else{//Greyscale //Create a buffer large enough to store image if (w*h!=njGetImageSize()) return NULL; di=(uint8*)malloc(w*h*4); //Greyscale->BGRA static int32 c; c=w*h; while(c--){ di[c*4+2]=si[c];//red di[c*4+1]=si[c];//green di[c*4 ]=si[c];//blue di[c*4+3]=255; //alpha } } *result=1+2; *x=w; *y=h; return di; }
int main(int argc, char* argv[]) { int size; char *buf; FILE *f; if (argc < 2) { printf("Usage: %s <input.jpg> [<output.ppm>]\n", argv[0]); return 2; } f = fopen(argv[1], "rb"); if (!f) { printf("Error opening the input file.\n"); return 1; } fseek(f, 0, SEEK_END); size = (int) ftell(f); buf = malloc(size); fseek(f, 0, SEEK_SET); size = (int) fread(buf, 1, size, f); fclose(f); njInit(); if (njDecode(buf, size)) { printf("Error decoding the input file.\n"); return 1; } f = fopen((argc > 2) ? argv[2] : (njIsColor() ? "nanojpeg_out.ppm" : "nanojpeg_out.pgm"), "wb"); if (!f) { printf("Error opening the output file.\n"); return 1; } fprintf(f, "P%d\n%d %d\n255\n", njIsColor() ? 6 : 5, njGetWidth(), njGetHeight()); fwrite(njGetImage(), 1, njGetImageSize(), f); fclose(f); njDone(); return 0; }
void mainCRTStartup(void) { char *argv[3], *buf = GetCommandLine(); enum { S_WHITESPACE, S_PARAM, S_QUOTE } state = S_WHITESPACE; int argc, size, esize; HANDLE f; for (argc = 0; (argc < 1022) && buf[argc]; ++argc) cmdline[argc] = buf[argc]; cmdline[argc] = 0; for (buf = cmdline, argc = 0; *buf; ++buf) { switch (state) { case S_WHITESPACE: if (buf[0] == 32) buf[0] = 0; else { if (argc >= 3) ExitProcess(2); if (buf[0] == 34) { argv[argc++] = buf + 1; state = S_QUOTE; } else { argv[argc++] = buf; state = S_PARAM; } } break; case S_PARAM: if (buf[0] == 32) { buf[0] = 0; state = S_WHITESPACE; } else if (buf[0] == 34) { argv[argc - 1] = buf + 1; state = S_QUOTE; } break; case S_QUOTE: if (buf[0] == 34) { buf[0] = 0; state = S_WHITESPACE; } break; } } if (argc < 2) ExitProcess(2); f = CreateFile(argv[1], GENERIC_READ, 0, NULL, OPEN_EXISTING, 0, NULL); if (f == INVALID_HANDLE_VALUE) ExitProcess(1); size = (int) SetFilePointer(f, 0, NULL, FILE_END); buf = LocalAlloc(LMEM_FIXED, size); if (!buf) ExitProcess(1); SetFilePointer(f, 0, NULL, FILE_BEGIN); ReadFile(f, buf, size, (LPDWORD) &esize, NULL); CloseHandle(f); njInit(); if (njDecode(buf, esize)) ExitProcess(1); f = CreateFile((argc > 2) ? argv[2] : (njIsColor() ? "nanojpeg_out.ppm" : "nanojpeg_out.pgm"), GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, 0, NULL); if (f == INVALID_HANDLE_VALUE) ExitProcess(1); if (njIsColor()) header[1] = '6'; putnum(&header[7], njGetWidth()); putnum(&header[13], njGetHeight()); WriteFile(f, header, 19, (LPDWORD) &esize, NULL); WriteFile(f, njGetImage(), njGetImageSize(), (LPDWORD) &esize, NULL); njDone(); ExitProcess(0); }