📄 image_out.cpp
字号:
} assert((scan->width-scan->accessed_samples) >= line.get_width()); if (line.get_buf32() != NULL) { if (line.is_absolute()) convert_ints_to_bytes(line.get_buf32(), scan->buf+3*scan->accessed_samples+idx, line.get_width(),precision[idx],3); else convert_floats_to_bytes(line.get_buf32(), scan->buf+3*scan->accessed_samples+idx, line.get_width(),precision[idx],3); } else { if (line.is_absolute()) convert_shorts_to_bytes(line.get_buf16(), scan->buf+3*scan->accessed_samples+idx, line.get_width(),precision[idx],3); else convert_fixpoint_to_bytes(line.get_buf16(), scan->buf+3*scan->accessed_samples+idx, line.get_width(),precision[idx],3); } scan->next_x_tnum++; if (idx == 2) scan->accessed_samples += line.get_width(); if (scan->accessed_samples == scan->width) { // Write completed line and send it to the free list. if (num_unwritten_rows == 0) { kdu_error e; e << "Attempting to write too many lines to image " "file for components " << first_comp_idx << " through " << first_comp_idx+2 << "."; } if (fwrite(scan->buf,1,(size_t)(scan->width*3),out) != (size_t)(scan->width*3)) { kdu_error e; e << "Unable to write to image file for components " << first_comp_idx << " through " << first_comp_idx+2 << ". File may be write protected, or disk may be full."; } num_unwritten_rows--; assert(scan == incomplete_lines); incomplete_lines = scan->next; scan->next = free_lines; free_lines = scan; }}/* ========================================================================= *//* raw_out *//* ========================================================================= *//*****************************************************************************//* raw_out::raw_out *//*****************************************************************************/raw_out::raw_out(char const *fname, kdu_image_dims &dims, int &next_comp_idx){ comp_idx = next_comp_idx++; if (comp_idx >= dims.get_num_components()) { kdu_error e; e << "Output image files require more image components " "(or mapped colour channels) than are available!"; } rows = dims.get_height(comp_idx); cols = dims.get_width(comp_idx); precision = dims.get_bit_depth(comp_idx); is_signed = dims.get_signed(comp_idx); sample_bytes = (precision+7)>>3; incomplete_lines = free_lines = NULL; num_unwritten_rows = rows; if ((out = fopen(fname,"wb")) == NULL) { kdu_error e; e << "Unable to open output image file, \"" << fname <<"\".";}}/*****************************************************************************//* raw_out::~raw_out *//*****************************************************************************/raw_out::~raw_out(){ if ((num_unwritten_rows > 0) || (incomplete_lines != NULL)) { kdu_warning w; w << "Not all rows of image component " << comp_idx << " were produced!"; } 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(out);}/*****************************************************************************//* raw_out::put *//*****************************************************************************/void raw_out::put(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 open a new line buffer. assert(x_tnum == 0); // Must supply samples from left to right. 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; scan->accessed_samples = 0; scan->next_x_tnum = 0; } assert((scan->width-scan->accessed_samples) >= line.get_width()); if (line.get_buf32() != NULL) { if (line.is_absolute()) convert_ints_to_words(line.get_buf32(), scan->buf+sample_bytes*scan->accessed_samples, line.get_width(), precision,is_signed,sample_bytes); else convert_floats_to_words(line.get_buf32(), scan->buf+sample_bytes*scan->accessed_samples, line.get_width(), precision,is_signed,sample_bytes); } else { if (line.is_absolute()) convert_shorts_to_words(line.get_buf16(), scan->buf+sample_bytes*scan->accessed_samples, line.get_width(), precision,is_signed,sample_bytes); else convert_fixpoint_to_words(line.get_buf16(), scan->buf+sample_bytes*scan->accessed_samples, line.get_width(), precision,is_signed,sample_bytes); } scan->next_x_tnum++; scan->accessed_samples += line.get_width(); if (scan->accessed_samples == scan->width) { // Write completed line and send it to the free list. if (num_unwritten_rows == 0) { kdu_error e; e << "Attempting to write too many lines to image " "file for component " << comp_idx << "."; } if (fwrite(scan->buf,1,(size_t)(scan->width*scan->sample_bytes),out) != (size_t)(scan->width*scan->sample_bytes)) { kdu_error e; e << "Unable to write to image file for component " << comp_idx << ". File may be write protected, or disk may be full."; } num_unwritten_rows--; assert(scan == incomplete_lines); incomplete_lines = scan->next; scan->next = free_lines; free_lines = scan; }}/* ========================================================================= *//* bmp_out *//* ========================================================================= *//*****************************************************************************//* bmp_out::bmp_out *//*****************************************************************************/bmp_out::bmp_out(char const *fname, kdu_image_dims &dims, int &next_comp_idx){ bool is_signed; int n; first_comp_idx = next_comp_idx; num_components = dims.get_num_components() - first_comp_idx; if (num_components <= 0) { kdu_error e; e << "Output image files require more image components " "(or mapped colour channels) than are available!"; } if (num_components >= 3) num_components = 3; else num_components = 1; rows = dims.get_height(first_comp_idx); cols = dims.get_width(first_comp_idx); is_signed = dims.get_signed(first_comp_idx); for (n=0; n < num_components; n++, next_comp_idx++) { if ((rows != dims.get_height(next_comp_idx)) || (cols != dims.get_width(next_comp_idx)) || (is_signed != dims.get_signed(next_comp_idx))) { assert(n > 0); num_components = 1; break; } precision[n] = dims.get_bit_depth(next_comp_idx); } next_comp_idx = first_comp_idx + num_components; if (is_signed) { kdu_warning w; w << "Signed sample values will be written to the " "BMP file as unsigned 8-bit quantities, centered about 128."; } kdu_byte magic[14]; bmp_header header; int header_bytes = 14+sizeof(header); assert(header_bytes == 54); if (num_components == 1) header_bytes += 1024; // Need colour LUT. int line_bytes = num_components * cols; alignment_bytes = (4-line_bytes) & 3; line_bytes += alignment_bytes; int file_bytes = line_bytes*rows + header_bytes; magic[0] = 'B'; magic[1] = 'M'; magic[2] = (kdu_byte) file_bytes; magic[3] = (kdu_byte)(file_bytes>>8); magic[4] = (kdu_byte)(file_bytes>>16); magic[5] = (kdu_byte)(file_bytes>>24); magic[6] = magic[7] = magic[8] = magic[9] = 0; magic[10] = (kdu_byte) header_bytes; magic[11] = (kdu_byte)(header_bytes>>8); magic[12] = (kdu_byte)(header_bytes>>16); magic[13] = (kdu_byte)(header_bytes>>24); header.size = 40; header.width = cols; header.height = rows; header.planes = 1; header.bit_count = (num_components==1)?8:24; header.compression = 0; header.image_size = 0; header.xpels_per_metre = header.ypels_per_metre = 0; header.num_colours_used = header.num_colours_important = 0; to_little_endian((kdu_int32 *) &header,10); if ((out = fopen(fname,"wb")) == NULL) { kdu_error e; e << "Unable to open output image file, \"" << fname <<"\".";} fwrite(magic,1,14,out); fwrite(&header,1,40,out); if (num_components == 1) for (n=0; n < 256; n++) { fputc(n,out); fputc(n,out); fputc(n,out); fputc(0,out); } incomplete_lines = NULL; free_lines = NULL; num_unwritten_rows = rows;}/*****************************************************************************//* bmp_out::~bmp_out *//*****************************************************************************/bmp_out::~bmp_out(){ if ((num_unwritten_rows > 0) || (incomplete_lines != NULL)) { kdu_warning w; w << "Not all rows of image components " << first_comp_idx << " through " << first_comp_idx+num_components-1 << " were completed!"; } 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(out);}/*****************************************************************************//* bmp_out::put *//*****************************************************************************/void bmp_out::put(int comp_idx, kdu_line_buf &line, int x_tnum){ int idx = comp_idx - this->first_comp_idx; assert((idx >= 0) && (idx < num_components)); x_tnum = x_tnum*num_components+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 open a new line buffer assert(x_tnum == 0); // Must consume in very specific order. if ((scan = free_lines) == NULL) { scan = new image_line_buf(cols+3,num_components); for (int k=0; k < alignment_bytes; k++) scan->buf[num_components*cols+k] = 0; } free_lines = scan->next; if (prev == NULL) incomplete_lines = scan; else prev->next = scan; scan->accessed_samples = 0; scan->next_x_tnum = 0; } assert((cols-scan->accessed_samples) >= line.get_width()); int comp_offset = (num_components==3)?(2-idx):0; if (line.get_buf32() != NULL) { if (line.is_absolute()) convert_ints_to_bytes(line.get_buf32(), scan->buf+num_components*scan->accessed_samples+comp_offset, line.get_width(),precision[idx],num_components); else convert_floats_to_bytes(line.get_buf32(), scan->buf+num_components*scan->accessed_samples+comp_offset, line.get_width(),precision[idx],num_components); } else { if (line.is_absolute()) convert_shorts_to_bytes(line.get_buf16(), scan->buf+num_components*scan->accessed_samples+comp_offset, line.get_width(),precision[idx],num_components); else convert_fixpoint_to_bytes(line.get_buf16(), scan->buf+num_components*scan->accessed_samples+comp_offset, line.get_width(),precision[idx],num_components); } scan->next_x_tnum++; if (idx == (num_components-1)) scan->accessed_samples += line.get_width(); if (scan->accessed_samples == cols) { // Write completed line and send it to the free list. if (num_unwritten_rows == 0) { kdu_error e; e << "Attempting to write too many lines to image " "file for components " << first_comp_idx << " through " << first_comp_idx+num_components-1 << "."; } if (fwrite(scan->buf,1,(size_t)(cols*num_components+alignment_bytes), out) != (size_t)(cols*num_components+alignment_bytes)) { kdu_error e; e << "Unable to write to image file for components " << first_comp_idx << " through " << first_comp_idx+num_components-1 << ". File may be write protected, or disk may be full."; } num_unwritten_rows--; assert(scan == incomplete_lines); incomplete_lines = scan->next; scan->next = free_lines; free_lines = scan; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -