📄 image_in.cpp
字号:
sp = scan->buf + n; kdu_byte val, *dp = scan->buf + (n<<3); for (; n > 0; n--) { val = *(--sp); val = ~val; *(--dp) = (val&1); val >>= 1; *(--dp) = (val&1); val >>= 1; *(--dp) = (val&1); val >>= 1; *(--dp) = (val&1); val >>= 1; *(--dp) = (val&1); val >>= 1; *(--dp) = (val&1); val >>= 1; *(--dp) = (val&1); val >>= 1; *(--dp) = (val&1); } scan->accessed_samples = 0; scan->next_x_tnum = 0; num_unread_rows--; } assert((cols-scan->accessed_samples) >= line.get_width()); sp = scan->buf+scan->accessed_samples; n = line.get_width(); if (line.get_buf32() != NULL) { kdu_sample32 *dp = line.get_buf32(); if (line.is_absolute()) { // 32-bit absolute integers for (; n > 0; n--, sp++, dp++) dp->ival = ((kdu_int32)(*sp)) - 1; } else { // true 32-bit floats for (; n > 0; n--, sp++, dp++) dp->fval = (((float)(*sp)) / 2.0F) - 0.5F; } } else { kdu_sample16 *dp = line.get_buf16(); if (line.is_absolute()) { // 16-bit absolute integers for (; n > 0; n--, sp++, dp++) dp->ival = ((kdu_int16)(*sp)) - 1; } else { // 16-bit normalized representation. for (; n > 0; n--, sp++, dp++) dp->ival = (((kdu_int16)(*sp)) - 1) << (KDU_FIX_POINT-1); } } scan->next_x_tnum++; scan->accessed_samples += line.get_width(); if (scan->accessed_samples == cols) { // Send empty line to free list. assert(scan == incomplete_lines); incomplete_lines = scan->next; scan->next = free_lines; free_lines = scan; } return true;}/* ========================================================================= *//* pgm_in *//* ========================================================================= *//*****************************************************************************//* pgm_in::pgm_in *//*****************************************************************************/pgm_in::pgm_in(char const *fname, siz_params *siz, int &next_comp_idx){ char magic[3]; int max_val; // We don't actually use this. if ((in = fopen(fname,"rb")) == NULL) { kdu_error e; e << "Unable to open input image file, \"" << fname <<"\"."; } magic[0] = magic[1] = magic[2] = '\0'; fread(magic,1,2,in); if (strcmp(magic,"P5") != 0) { kdu_error e; e << "PGM image file must start with the magic string, " "\"P5\"!"; } bool failed = false; eat_white_and_comments(in); if (fscanf(in,"%d",&cols) != 1) failed = true; eat_white_and_comments(in); if (fscanf(in,"%d",&rows) != 1) failed = true; eat_white_and_comments(in); if (fscanf(in,"%d",&max_val) != 1) failed = true; if (failed) {kdu_error e; e << "Image file \"" << fname << "\" does not appear to " "have a valid PGM header."; } int ch; while ((ch = fgetc(in)) != EOF) if ((ch == '\n') || (ch == ' ')) break; comp_idx = next_comp_idx++; siz->set(Sdims,comp_idx,0,rows); siz->set(Sdims,comp_idx,1,cols); siz->set(Ssigned,comp_idx,0,false); siz->set(Sprecision,comp_idx,0,8); incomplete_lines = free_lines = NULL; num_unread_rows = rows;}/*****************************************************************************//* pgm_in::~pgm_in *//*****************************************************************************/pgm_in::~pgm_in(){ if ((num_unread_rows > 0) || (incomplete_lines != NULL)) { kdu_warning w; w << "Not all rows of image component " << comp_idx << " were consumed!"; } image_line_buf *tmp; while ((tmp=incomplete_lines) != NULL) { incomplete_lines = tmp->next; delete tmp; } while ((tmp=free_lines) != NULL) { free_lines = tmp->next; delete tmp; } fclose(in);}/*****************************************************************************//* pgm_in::get *//*****************************************************************************/bool pgm_in::get(int comp_idx, kdu_line_buf &line, int x_tnum){ assert(comp_idx == this->comp_idx); image_line_buf *scan, *prev=NULL; for (scan=incomplete_lines; scan != NULL; prev=scan, scan=scan->next) { assert(scan->next_x_tnum >= x_tnum); if (scan->next_x_tnum == x_tnum) break; } if (scan == NULL) { // Need to read a new image line. assert(x_tnum == 0); // Must consume line from left to right. if (num_unread_rows == 0) return false; if ((scan = free_lines) == NULL) scan = new image_line_buf(cols,1); free_lines = scan->next; if (prev == NULL) incomplete_lines = scan; else prev->next = scan; if (fread(scan->buf,1,(size_t) scan->width,in) != (size_t) scan->width) { kdu_error e; e << "Image file for component " << comp_idx << " terminated prematurely!"; } scan->accessed_samples = 0; scan->next_x_tnum = 0; num_unread_rows--; } assert((scan->width-scan->accessed_samples) >= line.get_width()); kdu_byte *sp = scan->buf+scan->accessed_samples; int n=line.get_width(); if (line.get_buf32() != NULL) { kdu_sample32 *dp = line.get_buf32(); if (line.is_absolute()) { // 32-bit absolute integers for (; n > 0; n--, sp++, dp++) dp->ival = ((kdu_int32)(*sp)) - 128; } else { // true 32-bit floats for (; n > 0; n--, sp++, dp++) dp->fval = (((float)(*sp)) / 256.0F) - 0.5F; } } else { kdu_sample16 *dp = line.get_buf16(); if (line.is_absolute()) { // 16-bit absolute integers for (; n > 0; n--, sp++, dp++) dp->ival = ((kdu_int16)(*sp)) - 128; } else { // 16-bit normalized representation. for (; n > 0; n--, sp++, dp++) dp->ival = (((kdu_int16)(*sp)) - 128) << (KDU_FIX_POINT-8); } } scan->next_x_tnum++; scan->accessed_samples += line.get_width(); if (scan->accessed_samples == scan->width) { // Send empty line to free list. assert(scan == incomplete_lines); incomplete_lines = scan->next; scan->next = free_lines; free_lines = scan; } return true;}/* ========================================================================= *//* ppm_in *//* ========================================================================= *//*****************************************************************************//* ppm_in::ppm_in *//*****************************************************************************/ppm_in::ppm_in(char const *fname, siz_params *siz, int &next_comp_idx){ char magic[3]; int max_val; // We don't actually use this. int n; if ((in = fopen(fname,"rb")) == NULL) { kdu_error e; e << "Unable to open input image file, \"" << fname <<"\"."; } magic[0] = magic[1] = magic[2] = '\0'; fread(magic,1,2,in); if (strcmp(magic,"P6") != 0) { kdu_error e; e << "PPM image file must start with the magic string, " "\"P6\"!"; } bool failed = false; eat_white_and_comments(in); if (fscanf(in,"%d",&cols) != 1) failed = true; eat_white_and_comments(in); if (fscanf(in,"%d",&rows) != 1) failed = true; eat_white_and_comments(in); if (fscanf(in,"%d",&max_val) != 1) failed = true; if (failed) {kdu_error e; e << "Image file \"" << fname << "\" does not appear to " "have a valid PPM header."; } int ch; while ((ch = fgetc(in)) != EOF) if ((ch == '\n') || (ch == ' ')) break; first_comp_idx = next_comp_idx; for (n=0; n < 3; n++) { next_comp_idx++; siz->set(Sdims,first_comp_idx+n,0,rows); siz->set(Sdims,first_comp_idx+n,1,cols); siz->set(Ssigned,first_comp_idx+n,0,false); siz->set(Sprecision,first_comp_idx+n,0,8); } incomplete_lines = NULL; free_lines = NULL; num_unread_rows = rows;}/*****************************************************************************//* ppm_in::~ppm_in *//*****************************************************************************/ppm_in::~ppm_in(){ if ((num_unread_rows > 0) || (incomplete_lines != NULL)) { kdu_warning w; w << "Not all rows of image components " << first_comp_idx << " through " << first_comp_idx+2 << " were consumed!"; } image_line_buf *tmp; while ((tmp=incomplete_lines) != NULL) { incomplete_lines = tmp->next; delete tmp; } while ((tmp=free_lines) != NULL) { free_lines = tmp->next; delete tmp; } fclose(in);}/*****************************************************************************//* ppm_in::get *//*****************************************************************************/bool ppm_in::get(int comp_idx, kdu_line_buf &line, int x_tnum){ int idx = comp_idx - this->first_comp_idx; assert((idx >= 0) && (idx <= 2)); x_tnum = x_tnum*3+idx; // Works so long as components read in order. image_line_buf *scan, *prev=NULL; for (scan=incomplete_lines; scan != NULL; prev=scan, scan=scan->next) { assert(scan->next_x_tnum >= x_tnum); if (scan->next_x_tnum == x_tnum) break; } if (scan == NULL) { // Need to read a new image line. assert(x_tnum == 0); // Must consume in very specific order. if (num_unread_rows == 0) return false; if ((scan = free_lines) == NULL) scan = new image_line_buf(cols,3); free_lines = scan->next; if (prev == NULL) incomplete_lines = scan; else prev->next = scan; if (fread(scan->buf,1,(size_t)(scan->width*3),in) != (size_t)(scan->width*3)) { kdu_error e; e << "Image file for components " << first_comp_idx << " through " << first_comp_idx+2 << " terminated prematurely!"; } num_unread_rows--; scan->accessed_samples = 0; scan->next_x_tnum = 0; } assert((scan->width-scan->accessed_samples) >= line.get_width()); kdu_byte *sp = scan->buf+3*scan->accessed_samples+idx; int n=line.get_width(); if (line.get_buf32() != NULL) { kdu_sample32 *dp = line.get_buf32(); if (line.is_absolute()) { // 32-bit absolute integers for (; n > 0; n--, sp+=3, dp++) dp->ival = ((kdu_int32)(*sp)) - 128; } else { // true 32-bit floats for (; n > 0; n--, sp+=3, dp++) dp->fval = (((float)(*sp)) / 256.0F) - 0.5F; } } else { kdu_sample16 *dp = line.get_buf16(); if (line.is_absolute()) { // 16-bit absolute integers for (; n > 0; n--, sp+=3, dp++) dp->ival = ((kdu_int16)(*sp)) - 128; } else { // 16-bit normalized representation. for (; n > 0; n--, sp+=3, dp++) dp->ival = (((kdu_int16)(*sp)) - 128) << (KDU_FIX_POINT-8); } } scan->next_x_tnum++; if (idx == 2) scan->accessed_samples += line.get_width(); if (scan->accessed_samples == scan->width) { // Send empty line to free list. assert(scan == incomplete_lines); incomplete_lines = scan->next; scan->next = free_lines; free_lines = scan; } return true;}/* ========================================================================= *//* raw_in *//* ========================================================================= *//*****************************************************************************//* raw_in::raw_in *//*****************************************************************************/raw_in::raw_in(char const *fname, siz_params *siz, int &next_comp_idx){ if ((in = fopen(fname,"rb")) == NULL) { kdu_error e; e << "Unable to open input image file, \"" << fname <<"\".";} comp_idx = next_comp_idx++; if (!(siz->get(Sdims,comp_idx,0,rows) && siz->get(Sdims,comp_idx,1,cols) && siz->get(Sprecision,comp_idx,0,precision) && siz->get(Ssigned,comp_idx,0,is_signed))) { kdu_error e; e << "To use the raw image input file format, you must explicitly " "supply image component dimensions, image sample bit-depth and " "signed/unsigned information, using the \"Sdims\", \"Sprecision\" " "and \"Ssigned\" arguments."; } if (precision > 32) { kdu_error e; e << "Current implementation does not support " "image sample bit-depths in excess of 32 bits!"; } num_unread_rows = rows; sample_bytes = (precision+7)>>3; incomplete_lines = free_lines = NULL;}/*****************************************************************************//* raw_in::~raw_in *//*****************************************************************************/raw_in::~raw_in(){ if ((num_unread_rows > 0) || (incomplete_lines != NULL)) { kdu_warning w; w << "Not all rows of image component " << comp_idx << " were consumed!"; } image_line_buf *tmp; while ((tmp=incomplete_lines) != NULL) { incomplete_lines = tmp->next; delete tmp; } while ((tmp=free_lines) != NULL) { free_lines = tmp->next; delete tmp; } fclose(in);}/*****************************************************************************//* raw_in::get *//*****************************************************************************/bool raw_in::get(int comp_idx, kdu_line_buf &line, int x_tnum){ assert(comp_idx == this->comp_idx); image_line_buf *scan, *prev=NULL; for (scan=incomplete_lines; scan != NULL; prev=scan, scan=scan->next) { assert(scan->next_x_tnum >= x_tnum); if (scan->next_x_tnum == x_tnum) break; } if (scan == NULL) { // Need to read a new image line. assert(x_tnum == 0); // Must consume line from left to right. if (num_unread_rows == 0) return false; if ((scan = free_lines) == NULL) scan = new image_line_buf(cols,sample_bytes); free_lines = scan->next; if (prev == NULL) incomplete_lines = scan; else prev->next = scan; if (fread(scan->buf,1,(size_t)(scan->width*scan->sample_bytes),in) != (size_t)(scan->width*scan->sample_bytes)) { kdu_error e; e << "Image file for component " << comp_idx << " terminated prematurely!"; } num_unread_rows--; scan->accessed_samples = 0;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -