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; }
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 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); }
int decodeJpegData(char* jpegData, int jpegSize, int maxPixels, Bitmap* bitmap) { int returnCode; int maxWidth; int maxHeight; // Decode red channel returnCode = decodeJpegChannel(jpegData, jpegSize, 0, &(*bitmap).red, &(*bitmap).redWidth, &(*bitmap).redHeight); if (returnCode != MEMORY_OK) { LOGE("Failed to decode red channel"); njDone(); freeUnsignedCharArray(&(*bitmap).red); return returnCode; } doTransforms(bitmap, 1, 0, 0); // Resize red channel getScaledSize((*bitmap).redWidth, (*bitmap).redHeight, maxPixels, &maxWidth, &maxHeight); //We only need to do this once as r, g, b should be the same sizes returnCode = resizeChannel(&(*bitmap).red, (*bitmap).redWidth, (*bitmap).redHeight, maxWidth, maxHeight); if (returnCode != MEMORY_OK) { njDone(); freeUnsignedCharArray(&(*bitmap).red); return returnCode; } // Set red channel dimensions if ((*bitmap).redWidth >= maxWidth && (*bitmap).redHeight >= maxHeight) { (*bitmap).redWidth = maxWidth; (*bitmap).redHeight = maxHeight; } // Decode green channel returnCode = decodeJpegChannel(jpegData, jpegSize, 1, &(*bitmap).green, &(*bitmap).greenWidth, &(*bitmap).greenHeight); if (returnCode != MEMORY_OK) { LOGE("Failed to decode green channel"); njDone(); freeUnsignedCharArray(&(*bitmap).red); freeUnsignedCharArray(&(*bitmap).green); return returnCode; } doTransforms(bitmap, 0, 1, 0); // Resize green channel returnCode = resizeChannel(&(*bitmap).green, (*bitmap).greenWidth, (*bitmap).greenHeight, maxWidth, maxHeight); if (returnCode != MEMORY_OK) { njDone(); freeUnsignedCharArray(&(*bitmap).red); freeUnsignedCharArray(&(*bitmap).green); return returnCode; } // Set green channel dimensions if ((*bitmap).greenWidth >= maxWidth && (*bitmap).greenHeight >= maxHeight) { (*bitmap).greenWidth = maxWidth; (*bitmap).greenHeight = maxHeight; } // Decode blue channel returnCode = decodeJpegChannel(jpegData, jpegSize, 2, &(*bitmap).blue, &(*bitmap).blueWidth, &(*bitmap).blueHeight); if (returnCode != MEMORY_OK) { LOGE("Failed to decode blue channel"); njDone(); freeUnsignedCharArray(&(*bitmap).red); freeUnsignedCharArray(&(*bitmap).green); freeUnsignedCharArray(&(*bitmap).blue); return returnCode; } doTransforms(bitmap, 0, 0, 1); // Resize blue channel returnCode = resizeChannel(&(*bitmap).blue, (*bitmap).blueWidth, (*bitmap).blueHeight, maxWidth, maxHeight); if (returnCode != MEMORY_OK) { njDone(); freeUnsignedCharArray(&(*bitmap).red); freeUnsignedCharArray(&(*bitmap).green); freeUnsignedCharArray(&(*bitmap).blue); return returnCode; } // Set blue channel dimensions if ((*bitmap).blueWidth >= maxWidth && (*bitmap).blueHeight >= maxHeight) { (*bitmap).blueWidth = maxWidth; (*bitmap).blueHeight = maxHeight; } // Set the final bitmap dimensions if ((*bitmap).redWidth == (*bitmap).greenWidth && (*bitmap).redWidth == (*bitmap).blueWidth && (*bitmap).redHeight == (*bitmap).greenHeight && (*bitmap).redHeight == (*bitmap).blueHeight) { (*bitmap).width = (*bitmap).redWidth; (*bitmap).height = (*bitmap).redHeight; } else { njDone(); freeUnsignedCharArray(&(*bitmap).red); freeUnsignedCharArray(&(*bitmap).green); freeUnsignedCharArray(&(*bitmap).blue); return INCONSISTENT_BITMAP_ERROR; } njDoneLeaveRGBData(); return MEMORY_OK; }