Example #1
0
/**
 * Updates the components characteristics of the image from the coding parameters.
 *
 * @param p_image_header	the image header to update.
 * @param p_cp				the coding parameters from which to update the image.
 */
void opj_image_comp_header_update(opj_image_t * p_image_header, const struct opj_cp_v2 * p_cp)
{
	OPJ_UINT32 i, l_width, l_height;
	OPJ_INT32 l_x0, l_y0, l_x1, l_y1;
	OPJ_INT32 l_comp_x0, l_comp_y0, l_comp_x1, l_comp_y1;
	opj_image_comp_t* l_img_comp = NULL;

	l_x0 = int_max(p_cp->tx0 , p_image_header->x0);
	l_y0 = int_max(p_cp->ty0 , p_image_header->y0);
	l_x1 = int_min(p_cp->tx0 + p_cp->tw * p_cp->tdx, p_image_header->x1);
	l_y1 = int_min(p_cp->ty0 + p_cp->th * p_cp->tdy, p_image_header->y1);

	l_img_comp = p_image_header->comps;
	for	(i = 0; i < p_image_header->numcomps; ++i) {
		l_comp_x0 = int_ceildiv(l_x0, l_img_comp->dx);
		l_comp_y0 = int_ceildiv(l_y0, l_img_comp->dy);
		l_comp_x1 = int_ceildiv(l_x1, l_img_comp->dx);
		l_comp_y1 = int_ceildiv(l_y1, l_img_comp->dy);
		l_width = int_ceildivpow2(l_comp_x1 - l_comp_x0, l_img_comp->factor);
		l_height = int_ceildivpow2(l_comp_y1 - l_comp_y0, l_img_comp->factor);
		l_img_comp->w = l_width;
		l_img_comp->h = l_height;
		l_img_comp->x0 = l_comp_x0/*l_x0*/;
		l_img_comp->y0 = l_comp_y0/*l_y0*/;
		++l_img_comp;
	}
}
Example #2
0
void j2kTierOne::tierOneInitContext()
{
    //初始化ZC上下文,有9个
    for(int bandno=0; bandno<4; bandno++)
    {
        for(int ctx=0; ctx<256; ++ctx)
        {
            zcContextNo[(bandno<<8)|ctx]=initCtxZC(ctx,bandno);
        }
    }

    //初始化SC上下文,有5个
    for(int ctx=0; ctx<256; ctx++)
    {
        scContextNo[ctx]=initCtxSC(ctx<<4);//左移为移出低位4位
    }

    //初始化MR中的上下文,有3个
    for(int i=0; i<2; i++)
    {
        for(int ctx=0; ctx<2048; ++ctx)
        {
            int j=(i<<11)+ctx;//(1<<11)为作标记准备
            if(i)
                maContextNo[ctx+(i<<11)]=initCtxMAG(T1_REFINE|ctx);//添加 refine标记
            else
                maContextNo[ctx+(i<<11)]=initCtxMAG(ctx);
        }
    }

    for(int ctx=0; ctx<256; ++ctx)
        spbNo[ctx]=initSPB(ctx<<4);//腾出低4位

    double t,u,v;

    for(int i=0; i<(1<<T1_NMSEDEC_BITS); i++)
    {
        t=i/pow(2.0,T1_NMSEDEC_FRACBITS);
        u=t;
        v=t-1.5;

        double sig=(floor((u * u - v * v) * pow(2.0, T1_NMSEDEC_FRACBITS) + 0.5) / pow(2.0, T1_NMSEDEC_FRACBITS) * 8192.0);
        nmSeDecSig[i]=int_max(0,(int)sig );
        sig=(floor((u * u) * pow(2.0, T1_NMSEDEC_FRACBITS) + 0.5) / pow(2.0, T1_NMSEDEC_FRACBITS) * 8192.0);
        nmSeDecSig0[i]=int_max(0, (int)sig );

        u=t-1.0;

        if(i&(1<<T1_NMSEDEC_BITS-1))
        {
            v=t-1.5;
        } else
        {
            v=t-0.5;
        }

        nmSeDecRef[i]=int_max(0, (int) (floor((u * u - v * v) * pow(2.0, T1_NMSEDEC_FRACBITS) + 0.5) / pow(2.0, T1_NMSEDEC_FRACBITS) * 8192.0));
        nmSeDecRef0[i]=int_max(0,(int) (floor((u * u) * pow(2.0, T1_NMSEDEC_FRACBITS) +0.5) / pow(2.0, T1_NMSEDEC_FRACBITS) * 8192.0));
    }
}
Example #3
0
static inline void
debug_cookie_parser(struct cookie_str *cstr, unsigned char *pos, int ws, int eq)
{
    int namelen = int_max(cstr->nam_end - cstr->str, 0);
    int valuelen = int_max(cstr->val_end - cstr->val_start, 0);

    printf("[%.*s] :: (%.*s) :: %d,%d [%s] %d\n",
           namelen, cstr->str,
           valuelen, cstr->val_start,
           ws, eq, pos, cstr->nam_end - cstr->str);
}
Example #4
0
static int
do_op_left(struct form_state *fs, struct line_info *line, int current, int utf8)
{
	int old_state;
	int new_state;
	unsigned char *new_value;

	if (!utf8) {
		fs->state = int_max(fs->state - 1, 0);
		return 0;
	}

	if (fs->state_cell) {
		fs->state = fs->state_cell;
		fs->state_cell = 0;
		return 0;
	}

	old_state = fs->state;
	new_value = utf8_prevchar(fs->value + fs->state, 1, fs->value);
	new_state = new_value - fs->value;

	if (old_state != new_state) {
		if (old_state == line[current].start && line[current].split_prev)
			fs->state_cell = new_state;
		else
			fs->state = new_state;
	}
	return 0;
}
void test_fx_float_limits_zero(ostream& out)
{
  cerr << "****************** limits fx_float_zero\n";

  sc_fxval zero_min("-0");     SHOW(zero_min);
  sc_fxval zero_plus("+0");    SHOW(zero_plus);
  sc_fxval zero(0);            SHOW(zero);
  
  sc_fxval nan("NaN");         SHOW(nan);
  sc_fxval inf_plus("+Inf");   SHOW(inf_plus);
  sc_fxval inf_min("-Inf");    SHOW(inf_min);
  sc_fxval inf("Inf");         SHOW(inf);

  sc_fxval long_max(LONG_MAX); SHOW(long_max);
  sc_fxval long_min(LONG_MIN); SHOW(long_min);
  sc_fxval int_max(INT_MAX);   SHOW(int_max);
  sc_fxval int_min(INT_MIN);   SHOW(int_min);
  sc_fxval uint_max(UINT_MAX); SHOW(uint_max);
  sc_fxval ulong_max(ULONG_MAX); SHOW(ulong_max);

  sc_fxval double_min(DBL_MIN); SHOW(double_min);
  sc_fxval double_max(DBL_MAX); SHOW(double_max);
  sc_fxval float_min(FLT_MIN);  SHOW(float_min);
  sc_fxval float_max(FLT_MAX);  SHOW(float_max);

  sc_fxval res;


  SHOW_EXPS(zero_min);
  SHOW_EXPS(zero_plus);
  SHOW_EXPS(zero);
}
Example #6
0
static int
do_op_end(struct form_state *fs, struct line_info *line, int current, int utf8)
{
	if (current == -1) {
		fs->state = strlen(fs->value);
		return 0;
	}

	if (!utf8) {
		int wrap = line[current + 1].start == line[current].end;

		/* Don't jump to next line when wrapping. */
		fs->state = int_max(0, line[current].end - wrap);
		return 0;
	}

	current -= !!fs->state_cell;
	fs->state = line[current].end;
	if (line[current].split_next) {
		unsigned char *new_value;

		new_value = utf8_prevchar(fs->value + fs->state, 1, fs->value);
		fs->state_cell = new_value - fs->value;
	} else {
		fs->state_cell = 0;
	}
	return 0;
}
void test_fx_fixed_limits_double(ostream& out)
{
  out << "****************** limits fx_fixed<8, 5>_double\n";

  sc_fixed<8, 5> zero_min("-0");     SHOW(zero_min);
  sc_fixed<8, 5> zero_plus("+0");    SHOW(zero_plus);
  sc_fixed<8, 5> zero(0);            SHOW(zero);
  
  sc_fixed<8, 5> long_max(LONG_MAX); SHOW(long_max);
  sc_fixed<8, 5> long_min(LONG_MIN); SHOW(long_min);
  sc_fixed<8, 5> int_max(INT_MAX);   SHOW(int_max);
  sc_fixed<8, 5> int_min(INT_MIN);   SHOW(int_min);
  sc_fixed<8, 5> uint_max(UINT_MAX); SHOW(uint_max);
  sc_fixed<8, 5> ulong_max(ULONG_MAX); SHOW(ulong_max);

  sc_fixed<8, 5> double_min(DBL_MIN); SHOW(double_min);
  sc_fixed<8, 5> double_max(DBL_MAX); SHOW(double_max);
  sc_fixed<8, 5> float_min(FLT_MIN);  SHOW(float_min);
  sc_fixed<8, 5> float_max(FLT_MAX);  SHOW(float_max);

  // sc_fixed<8, 5> res;


  // SHOW_EXPS(double_min);
  // SHOW_EXPS(double_max);
  // SHOW_EXPS(float_min);
  // SHOW_EXPS(float_max);
}
Example #8
0
void EightDirections::update(float dt)
{
    if (max_speed == 0)
        return;

    bool on = false;
    int dir = get_joystick_direction(1);
    if (dir == 8)
        dir = instance->direction;
    else {
        on = true;
        dir *= 4;
        instance->set_direction(dir, false);
    }
    double mul = instance->frame->timer_mul;

    double change;
    if (on)
        change = get_accelerator(acceleration);
    else
        change = -get_accelerator(deceleration);

    set_speed(int_max(0, int_min(speed + change * mul, max_speed)));

    if (speed == 0)
        return;

    double add_x, add_y;
    get_dir(instance->direction, add_x, add_y);
    double m = get_pixels(speed) * mul;
    move(add_x * m, add_y * m);
    last_move = m;
}
void test_fx_fix_limits_long(ostream& out)
{
  out << "****************** limits fx_fix_long\n";

  sc_fix zero_min("-0");     SHOW(zero_min);
  sc_fix zero_plus("+0");    SHOW(zero_plus);
  sc_fix zero(0);            SHOW(zero);
  
  sc_fix long_max(LONG_MAX); SHOW(long_max);
  sc_fix long_min(LONG_MIN); SHOW(long_min);
  sc_fix int_max(INT_MAX);   SHOW(int_max);
  sc_fix int_min(INT_MIN);   SHOW(int_min);
  sc_fix uint_max(UINT_MAX); SHOW(uint_max);
  sc_fix ulong_max(ULONG_MAX); SHOW(ulong_max);

  sc_fix double_min(DBL_MIN); SHOW(double_min);
  sc_fix double_max(DBL_MAX); SHOW(double_max);
  sc_fix float_min(FLT_MIN);  SHOW(float_min);
  sc_fix float_max(FLT_MAX);  SHOW(float_max);

  // sc_fix res;


  // SHOW_EXPS(long_max);
  // SHOW_EXPS(long_min);
  // SHOW_EXPS(int_max);
  // SHOW_EXPS(int_min);
  // SHOW_EXPS(uint_max);
  // SHOW_EXPS(ulong_max);
}
Example #10
0
/* 
   PRE: size of actual_dist is same as size of hypothesized_dist.
        Any entry in which hypothesized_dist has a value of
        zero must have an actual_dist value of zero (i.e.
          forall i, hy_dist[i]==0 => ac_dist[i] == 0

   Given two distributions represented as histograms 
   (actual_dist and hypothesized_dist), how much evidence is there that they are
   from the same distribution? 
   Note that these things must be counts. Each element of actual_dist must
   be an integer. Each element of hypothesized_dist may be non-integer
   because we're talking expected counts there.

   The prob returned by this function answers that question
   using a standard chi-squared test. If it is low (e.g. < 0.05), then it is
   unlikely that they are the same. 

   The "dof" parameter is the mysterious "Degrees Of Freedom" that haunts
   any use of the word "Chi". 
   
       If it is possible for any entry in the dist
       to take any value, then set dof==size.

       If the sum of values is constrained to a certain value
       then set dof==size-1.

       If there are more constraints than that, then subtract
       more from size.
*/
double chi_squared_prob(dyv *actual_dist,dyv *hypothesized_dist,int dof)
{
  double result = -1.0;
  double min_hyp_dist = dyv_min(hypothesized_dist);
  if ( min_hyp_dist < 0.0 )
    my_error("chi_squared_prob: -ve count in hypothesized_dist");
  else if ( min_hyp_dist > 0.0 )
    result = chi_squared_prob_helper(actual_dist,hypothesized_dist,dof);
  else
  {
    dyv *copy_ad = mk_dyv(0);
    dyv *copy_hd = mk_dyv(0);
    int i;
    for ( i = 0 ; i < dyv_size(actual_dist) ; i++ )
    {
      if ( dyv_ref(hypothesized_dist,i) > 0.0 )
      {
        add_to_dyv(copy_ad,dyv_ref(actual_dist,i));
        add_to_dyv(copy_hd,dyv_ref(hypothesized_dist,i));
        dof -= 1;
      }
      else if ( dyv_ref(actual_dist,i) > 0.0 )
        my_error("chi_squared_prob: actual_dist value must be zero if hyp dist value is zero");
    }
    dof = int_max(2,dof);
    result = chi_squared_prob_helper(copy_ad,copy_hd,dof);
    free_dyv(copy_ad);
    free_dyv(copy_hd);
  }

  return result;
}
Example #11
0
void Active::force_frame(int value)
{
    if (loop_count == 0)
        return;
    int frame_count = direction_data->frame_count;
    forced_frame = int_max(0, int_min(value, frame_count - 1));
    update_frame();
}
Example #12
0
static int
do_op_end(struct form_state *fs, struct line_info *line, int current)
{
	if (current == -1) {
		fs->state = strlen(fs->value);

	} else {
		int wrap = line[current + 1].start == line[current].end;

		/* Don't jump to next line when wrapping. */
		fs->state = int_max(0, line[current].end - wrap);
	}
	return 0;
}
Example #13
0
static void
gpm_mouse_in(struct gpm_mouse_spec *gms)
{
	Gpm_Event gev;
	struct term_event ev;
	struct term_event_mouse mouse;

	if (Gpm_GetEvent(&gev) <= 0) {
		clear_handlers(gms->h);
		return;
	}

	mouse.x = int_max(gev.x - 1, 0);
	mouse.y = int_max(gev.y - 1, 0);

	if (gev.buttons & GPM_B_LEFT)
		mouse.button = B_LEFT;
	else if (gev.buttons & GPM_B_MIDDLE)
		mouse.button = B_MIDDLE;
	else if (gev.buttons & GPM_B_RIGHT)
		mouse.button = B_RIGHT;
	else
		return;

	if (gev.type & GPM_DOWN)
		mouse.button |= B_DOWN;
	else if (gev.type & GPM_UP)
		mouse.button |= B_UP;
	else if (gev.type & GPM_DRAG)
		mouse.button |= B_DRAG;
	else
		return;

	set_mouse_term_event(&ev, mouse.x, mouse.y, mouse.button);
	gms->fn(gms->data, (char *) &ev, sizeof(ev));
}
Example #14
0
void BallMovement::update()
{
    if (stop_speed != 0 || speed == 0) {
        instance->set_animation(STOPPED);
        return;
    } else
        instance->set_animation(WALKING);
    double add_x, add_y;
    get_dir(instance->direction, add_x, add_y);
    double m = get_pixels(speed) * instance->frame->timer_mul;

    if (speed > 100 && has_back_col) {
        double move_x = add_x * m;
        double move_y = add_y * m;

        int steps = 4;

        move_x /= steps;
        move_y /= steps;

        old_x = instance->x;
        old_y = instance->y;

        clear_collisions();

        for (int i = 0; i < steps; ++i) {
            this->add_x += move_x;
            this->add_y += move_y;
            double xx = floor(this->add_x);
            double yy = floor(this->add_y);
            this->add_x -= xx;
            this->add_y -= yy;
            instance->set_position(instance->x + xx,
                                   instance->y + yy);

            if (instance->overlaps_background())
                break;
        }
    } else {
        move(add_x * m, add_y * m);
    }
    if (deceleration != 0)
        speed_change -= get_accelerator(deceleration)
                        * instance->frame->timer_mul;
    int change = (int)speed_change;
    speed_change -= change;
    speed = int_max(0, speed + change);
}
Example #15
0
int t2_decode_packets(opj_t2_t *t2, unsigned char *src, int len, int tileno, opj_tcd_tile_t *tile) {
	unsigned char *c = src;
	opj_pi_iterator_t *pi;
	int pino, e = 0;
	int n = 0,i;

	opj_volume_t *volume = t2->volume;
	opj_cp_t *cp = t2->cp;
	
	/* create a packet iterator */
	pi = pi_create(volume, cp, tileno);
	if(!pi) {
		/* TODO: throw an error */
		return -999;
	}
	
	for (pino = 0; pino <= cp->tcps[tileno].numpocs; pino++) {
		while (pi_next(&pi[pino])) {
			if ((cp->layer==0) || (cp->layer>=((pi[pino].layno)+1))) {
				e = t2_decode_packet(t2, c, src + len - c, tile, &cp->tcps[tileno], &pi[pino]);
			} else {
				e = 0;
			}
			
			/* progression in resolution */
			for (i = 0; i < 3; i++){
                volume->comps[pi[pino].compno].resno_decoded[i] = (e > 0) ? int_max(pi[pino].resno, volume->comps[pi[pino].compno].resno_decoded[i]) : volume->comps[pi[pino].compno].resno_decoded[i];
			}
			n++;
			
			if (e == -999) {		/* ADD */
				break;
			} else {
				opj_event_msg(t2->cinfo, EVT_INFO, "  t2_decode_packet: %d bytes decoded\n",e);
				c += e;
			}
		}
	}

	/* don't forget to release pi */
	pi_destroy(pi, cp, tileno);
	
	if (e == -999) {
		return e;
	}
	
    return (c - src);
}
Example #16
0
int t2_decode_packets(opj_t2_t *t2, unsigned char *src, int len, int tileno, opj_tcd_tile_t *tile) {
	unsigned char *c = src;
	opj_pi_iterator_t *pi;
	int pino, e = 0;
	int n = 0;

	opj_image_t *image = t2->image;
	opj_cp_t *cp = t2->cp;
	
	/* create a packet iterator */
	pi = pi_create(image, cp, tileno);
	if(!pi) {
		/* TODO: throw an error */
		return -999;
	}
	
	for (pino = 0; pino <= cp->tcps[tileno].numpocs; pino++) {
		while (pi_next(&pi[pino])) {
			if ((cp->layer==0) || (cp->layer>=((pi[pino].layno)+1))) {
				e = t2_decode_packet(t2, c, src + len - c, tile, &cp->tcps[tileno], &pi[pino]);
			} else {
				e = 0;
			}
			
			/* progression in resolution */
			image->comps[pi[pino].compno].resno_decoded =	
				(e > 0) ? 
				int_max(pi[pino].resno, image->comps[pi[pino].compno].resno_decoded) 
				: image->comps[pi[pino].compno].resno_decoded;
			n++;
			
			if (e == -999) {		/* ADD */
				break;
			} else {
				c += e;
			}
		}
	}

	/* don't forget to release pi */
	pi_destroy(pi, cp, tileno);
	
	if (e == -999) {
		return e;
	}
	
    return (c - src);
}
Example #17
0
void
set_cursor(struct terminal *term, int x, int y, int blockable)
{
	assert(term && term->screen);
	if_assert_failed return;

	if (blockable && get_opt_bool_tree(term->spec, (const unsigned char *)"block_cursor", NULL)) {
		x = term->width - 1;
		y = term->height - 1;
	}

	if (term->screen->cx != x || term->screen->cy != y) {
		check_range(term, x, y);

		set_screen_dirty(term->screen, int_min(term->screen->cy, y),
					       int_max(term->screen->cy, y));
		term->screen->cx = x;
		term->screen->cy = y;
	}
}
Example #18
0
	bignum::operator int() const
	{
		bignum temp(*this);
		temp.roundToIndex(ONES_PLACE);
		temp.convertBase(10);
		bignum int_max(numeric_limits<int>::max());
		bignum int_min(numeric_limits<int>::min());

		if (temp > int_max || temp < int_min)
			throw error_handler(__FILE__, __LINE__, "attempted conversion failed: bignum is too large to be converted to an int");

		int return_int = 0;
		for (int i = 0; i < digitRange; i++)
		{
			int power = (pow((double)10, i));
			return_int += (power * digits[ONES_PLACE + i]);
		}
		
		return return_int;
	}
Example #19
0
int main(int argc, char *argv[])
{
    int x = 2;
    int *p = &x;

    printf("X is %d\n", x);
    printf("X^2 is %d\n", powpow(x));
    power(p);
    printf("X^2 pointer is %d\n", x);

    int r[] = {7, 6, 5, 3};
    printf("p1 is %d\n", r[0]);
    printf("p2 is %d\n", *(r + 1));

    printf("%d\n", int_max(1, 3));


    #if DEBUG
    printf("Compiled on %s, %s by %d\n", __DATE__, __TIME__, __STDC__);
    printf("DEBUGGER MODE\n");
    #endif

    return 0;
}
Example #20
0
opj_pi_iterator_t *pi_create_decode(opj_image_t *image, opj_cp_t *cp, int tileno) {
	int p, q;
	int compno, resno, pino;
	opj_pi_iterator_t *pi = NULL;
	opj_tcp_t *tcp = NULL;
	opj_tccp_t *tccp = NULL;
	size_t array_size;
	
	tcp = &cp->tcps[tileno];

	array_size = (tcp->numpocs + 1) * sizeof(opj_pi_iterator_t);
	pi = (opj_pi_iterator_t *) opj_malloc(array_size);
	if(!pi) {
		/* TODO: throw an error */
		return NULL;
	}
	
	for (pino = 0; pino < tcp->numpocs + 1; pino++) {	/* change */
		int maxres = 0;
		int maxprec = 0;
		p = tileno % cp->tw;
		q = tileno / cp->tw;

		pi[pino].tx0 = int_max(cp->tx0 + p * cp->tdx, image->x0);
		pi[pino].ty0 = int_max(cp->ty0 + q * cp->tdy, image->y0);
		pi[pino].tx1 = int_min(cp->tx0 + (p + 1) * cp->tdx, image->x1);
		pi[pino].ty1 = int_min(cp->ty0 + (q + 1) * cp->tdy, image->y1);
		pi[pino].numcomps = image->numcomps;

		array_size = image->numcomps * sizeof(opj_pi_comp_t);
		pi[pino].comps = (opj_pi_comp_t *) opj_malloc(array_size);
		if(!pi[pino].comps) {
			/* TODO: throw an error */
			pi_destroy(pi, cp, tileno);
			return NULL;
		}
		memset(pi[pino].comps, 0, array_size);
		
		for (compno = 0; compno < pi->numcomps; compno++) {
			int tcx0, tcy0, tcx1, tcy1;
			opj_pi_comp_t *comp = &pi[pino].comps[compno];
			tccp = &tcp->tccps[compno];
			comp->dx = image->comps[compno].dx;
			comp->dy = image->comps[compno].dy;
			comp->numresolutions = tccp->numresolutions;

			array_size = comp->numresolutions * sizeof(opj_pi_resolution_t);
			comp->resolutions =	(opj_pi_resolution_t *) opj_malloc(array_size);
			if(!comp->resolutions) {
				/* TODO: throw an error */
				pi_destroy(pi, cp, tileno);
				return NULL;
			}

			tcx0 = int_ceildiv(pi->tx0, comp->dx);
			tcy0 = int_ceildiv(pi->ty0, comp->dy);
			tcx1 = int_ceildiv(pi->tx1, comp->dx);
			tcy1 = int_ceildiv(pi->ty1, comp->dy);
			if (comp->numresolutions > maxres) {
				maxres = comp->numresolutions;
			}

			for (resno = 0; resno < comp->numresolutions; resno++) {
				int levelno;
				int rx0, ry0, rx1, ry1;
				int px0, py0, px1, py1;
				opj_pi_resolution_t *res = &comp->resolutions[resno];
				if (tccp->csty & J2K_CCP_CSTY_PRT) {
					res->pdx = tccp->prcw[resno];
					res->pdy = tccp->prch[resno];
				} else {
					res->pdx = 15;
					res->pdy = 15;
				}
				levelno = comp->numresolutions - 1 - resno;
				rx0 = int_ceildivpow2(tcx0, levelno);
				ry0 = int_ceildivpow2(tcy0, levelno);
				rx1 = int_ceildivpow2(tcx1, levelno);
				ry1 = int_ceildivpow2(tcy1, levelno);
				px0 = int_floordivpow2(rx0, res->pdx) << res->pdx;
				py0 = int_floordivpow2(ry0, res->pdy) << res->pdy;
				px1 = int_ceildivpow2(rx1, res->pdx) << res->pdx;
				py1 = int_ceildivpow2(ry1, res->pdy) << res->pdy;
				res->pw = (rx0==rx1)?0:((px1 - px0) >> res->pdx);
				res->ph = (ry0==ry1)?0:((py1 - py0) >> res->pdy);
				
				if (res->pw*res->ph > maxprec) {
					maxprec = res->pw*res->ph;
				}
				
			}
		}
		
		tccp = &tcp->tccps[0];
		pi[pino].step_p = 1;
		pi[pino].step_c = maxprec * pi[pino].step_p;
		pi[pino].step_r = image->numcomps * pi[pino].step_c;
		pi[pino].step_l = maxres * pi[pino].step_r;
		
		if (pino == 0) {
			array_size = image->numcomps * maxres * tcp->numlayers * maxprec * sizeof(short int);
			pi[pino].include = (short int *) opj_malloc(array_size);
			if(!pi[pino].include) {
				/* TODO: throw an error */
				pi_destroy(pi, cp, tileno);
				return NULL;
			}
		}
		else {
			pi[pino].include = pi[pino - 1].include;
		}
		
		if (tcp->POC == 0) {
			pi[pino].first = 1;
			pi[pino].poc.resno0 = 0;
			pi[pino].poc.compno0 = 0;
			pi[pino].poc.layno1 = tcp->numlayers;
			pi[pino].poc.resno1 = maxres;
			pi[pino].poc.compno1 = image->numcomps;
			pi[pino].poc.prg = tcp->prg;
		} else {
			pi[pino].first = 1;
			pi[pino].poc.resno0 = tcp->pocs[pino].resno0;
			pi[pino].poc.compno0 = tcp->pocs[pino].compno0;
			pi[pino].poc.layno1 = tcp->pocs[pino].layno1;
			pi[pino].poc.resno1 = tcp->pocs[pino].resno1;
			pi[pino].poc.compno1 = tcp->pocs[pino].compno1;
			pi[pino].poc.prg = tcp->pocs[pino].prg;
		}
		pi[pino].poc.layno0  = 0;
		pi[pino].poc.precno0 = 0; 
		pi[pino].poc.precno1 = maxprec;
			
	}
	
	return pi;
}
Example #21
0
static bool t2_encode_packet(
               OPJ_UINT32 tileno,
               opj_tcd_tile_t * tile,
               opj_tcp_t * tcp,
               opj_pi_iterator_t *pi,
               OPJ_BYTE *dest,
               OPJ_UINT32 * p_data_written,
               OPJ_UINT32 length,
               opj_codestream_info_t *cstr_info)
{
  OPJ_UINT32 bandno, cblkno;
  OPJ_BYTE *c = dest;
  OPJ_UINT32 l_nb_bytes;
  OPJ_UINT32 compno = pi->compno;  /* component value */
  OPJ_UINT32 resno  = pi->resno;    /* resolution level value */
  OPJ_UINT32 precno = pi->precno;  /* precinct value */
  OPJ_UINT32 layno  = pi->layno;    /* quality layer value */
  OPJ_UINT32 l_nb_blocks;
  opj_tcd_band_t *band = 00;
  opj_tcd_cblk_enc_t* cblk = 00;
  opj_tcd_pass_t *pass = 00;

  opj_tcd_tilecomp_t *tilec = &tile->comps[compno];
  opj_tcd_resolution_t *res = &tilec->resolutions[resno];

  opj_bio_t *bio = 00;  /* BIO component */

  /* <SOP 0xff91> */
  if (tcp->csty & J2K_CP_CSTY_SOP) {
    c[0] = 255;
    c[1] = 145;
    c[2] = 0;
    c[3] = 4;
    c[4] = (tile->packno % 65536) / 256;
    c[5] = (tile->packno % 65536) % 256;
    c += 6;
    length -= 6;
  }
  /* </SOP> */

  if (!layno) {
    band = res->bands;
    for
      (bandno = 0; bandno < res->numbands; ++bandno)
    {
      opj_tcd_precinct_t *prc = &band->precincts[precno];
      tgt_reset(prc->incltree);
      tgt_reset(prc->imsbtree);
      l_nb_blocks = prc->cw * prc->ch;
      for
        (cblkno = 0; cblkno < l_nb_blocks; ++cblkno)
      {
        opj_tcd_cblk_enc_t* cblk_v = &prc->cblks.enc[cblkno];
        cblk_v->numpasses = 0;
        tgt_setvalue(prc->imsbtree, cblkno, band->numbps - cblk_v->numbps);
      }
      ++band;
    }
  }

  bio = bio_create();
  bio_init_enc(bio, c, length);
  bio_write(bio, 1, 1);    /* Empty header bit */

  /* Writing Packet header */
  band = res->bands;
  for
    (bandno = 0; bandno < res->numbands; ++bandno)
  {
    opj_tcd_precinct_t *prc = &band->precincts[precno];
    l_nb_blocks = prc->cw * prc->ch;
    cblk = prc->cblks.enc;
    for (cblkno = 0; cblkno < l_nb_blocks; ++cblkno)
    {
      opj_tcd_layer_t *layer = &cblk->layers[layno];
      if
        (!cblk->numpasses && layer->numpasses)
      {
        tgt_setvalue(prc->incltree, cblkno, layno);
      }
      ++cblk;
    }
    cblk = prc->cblks.enc;
    for
      (cblkno = 0; cblkno < l_nb_blocks; cblkno++)
    {
      opj_tcd_layer_t *layer = &cblk->layers[layno];
      OPJ_UINT32 increment = 0;
      OPJ_UINT32 nump = 0;
      OPJ_UINT32 len = 0, passno;
      OPJ_UINT32 l_nb_passes;
      /* cblk inclusion bits */
      if (!cblk->numpasses) {
        tgt_encode(bio, prc->incltree, cblkno, layno + 1);
      } else {
        bio_write(bio, layer->numpasses != 0, 1);
      }
      /* if cblk not included, go to the next cblk  */
      if
        (!layer->numpasses)
      {
        ++cblk;
        continue;
      }
      /* if first instance of cblk --> zero bit-planes information */
      if
        (!cblk->numpasses)
      {
        cblk->numlenbits = 3;
        tgt_encode(bio, prc->imsbtree, cblkno, 999);
      }
      /* number of coding passes included */
      t2_putnumpasses(bio, layer->numpasses);
      l_nb_passes = cblk->numpasses + layer->numpasses;
      pass = cblk->passes +  cblk->numpasses;
      /* computation of the increase of the length indicator and insertion in the header     */
      for
        (passno = cblk->numpasses; passno < l_nb_passes; ++passno)
      {
        ++nump;
        len += pass->len;
        if
          (pass->term || passno == (cblk->numpasses + layer->numpasses) - 1)
        {
          increment = int_max(increment, int_floorlog2(len) + 1 - (cblk->numlenbits + int_floorlog2(nump)));
          len = 0;
          nump = 0;
        }
        ++pass;
      }
      t2_putcommacode(bio, increment);

      /* computation of the new Length indicator */
      cblk->numlenbits += increment;

      pass = cblk->passes +  cblk->numpasses;
      /* insertion of the codeword segment length */
      for
        (passno = cblk->numpasses; passno < l_nb_passes; ++passno)
      {
        nump++;
        len += pass->len;
        if
          (pass->term || passno == (cblk->numpasses + layer->numpasses) - 1)
        {
          bio_write(bio, len, cblk->numlenbits + int_floorlog2(nump));
          len = 0;
          nump = 0;
        }
        ++pass;
      }
      ++cblk;
    }
    ++band;
  }

  if
    (bio_flush(bio))
  {
    bio_destroy(bio);
    return false;    /* modified to eliminate longjmp !! */
  }
  l_nb_bytes = bio_numbytes(bio);
  c += l_nb_bytes;
  length -= l_nb_bytes;
  bio_destroy(bio);

  /* <EPH 0xff92> */
  if (tcp->csty & J2K_CP_CSTY_EPH) {
    c[0] = 255;
    c[1] = 146;
    c += 2;
    length -= 2;
  }
  /* </EPH> */

  /* << INDEX */
  // End of packet header position. Currently only represents the distance to start of packet
  // Will be updated later by incrementing with packet start value
  if(cstr_info && cstr_info->index_write) {
    opj_packet_info_t *info_PK = &cstr_info->tile[tileno].packet[cstr_info->packno];
    info_PK->end_ph_pos = (OPJ_INT32)(c - dest);
  }
  /* INDEX >> */

  /* Writing the packet body */
  band = res->bands;
  for
    (bandno = 0; bandno < res->numbands; bandno++)
  {
    opj_tcd_precinct_t *prc = &band->precincts[precno];
    l_nb_blocks = prc->cw * prc->ch;
    cblk = prc->cblks.enc;
    for
      (cblkno = 0; cblkno < l_nb_blocks; ++cblkno)
    {
      opj_tcd_layer_t *layer = &cblk->layers[layno];
      if
        (!layer->numpasses)
      {
        ++cblk;
        continue;
      }
      if
        (layer->len > length)
      {
        return false;
      }
      memcpy(c, layer->data, layer->len);
      cblk->numpasses += layer->numpasses;
      c += layer->len;
      length -= layer->len;
      /* << INDEX */
      if(cstr_info && cstr_info->index_write) {
        opj_packet_info_t *info_PK = &cstr_info->tile[tileno].packet[cstr_info->packno];
        info_PK->disto += layer->disto;
        if (cstr_info->D_max < info_PK->disto) {
          cstr_info->D_max = info_PK->disto;
        }
      }
      ++cblk;
      /* INDEX >> */
    }
    ++band;
  }
  * p_data_written += (c - dest);
  return true;
}
Example #22
0
/* Takes care about rendering of each listbox item. */
static int
display_listbox_item(struct listbox_item *item, void *data_, int *offset)
{
	struct listbox_context *data = (struct listbox_context *)data_;
	int len; /* Length of the current text field. */
	struct color_pair *tree_color, *text_color;
	int depth = item->depth + 1;
	int d;
	int x, y;

	tree_color = get_bfu_color(data->term, (const unsigned char *)"menu.normal");
	if (item == data->box->sel) {
		text_color = get_bfu_color(data->term, (const unsigned char *)"menu.selected");

	} else if (item->marked) {
		text_color = get_bfu_color(data->term, (const unsigned char *)"menu.marked");

	} else {
		text_color = tree_color;
	}

	y = data->widget_data->box.y + data->offset;
	for (d = 0; d < depth - 1; d++) {
		struct listbox_item *root = item;
		struct listbox_item *child = item;
		int i, x;

		for (i = depth - d; i; i--) {
			child = root;
			if (root) root = data->box->ops->get_root(root);
		}

		/* XXX */
		x = data->widget_data->box.x + d * 5;
		draw_text(data->term, x, y, (const unsigned char *)"     ", 5, 0, tree_color);

		if (root ? root->child.prev == child
			 : data->box->items->prev == child)
			continue; /* We were the last branch. */

		draw_border_char(data->term, x + 1, y, BORDER_SVLINE, tree_color);
	}

	if (depth) {
		unsigned char str[5] =
			{ 32, BORDER_SRTEE, BORDER_SHLINE, BORDER_SHLINE, 32 };
		int i;

		switch (item->type) {
		case BI_LEAF:
		case BI_SEPARATOR:
		{
			const struct listbox_item *prev;

			prev = traverse_listbox_items_list(item, data->box,
			                                   -1, 1, NULL, NULL);

			if (item == prev) {
				/* There is no visible item before @item, so it
				 * must be the first item in the listbox. */
				str[1] = BORDER_SULCORNER;
			} else {
				const struct listbox_item *next;

				next = traverse_listbox_items_list(item,
				                  data->box, 1, 1, NULL, NULL);

				if (item == next
				    || item->depth != next->depth) {
					/* There is no visible item after @item
					 * at the same depth, so it must be the
					 * last in its folder. */
					str[1] = BORDER_SDLCORNER;
				}
			}
			break;
		}
		case BI_FOLDER:
			str[0] = '[';
			str[1] = (item->expanded) ? '-' : '+';
			str[2] = ']';
			break;
		default:
			INTERNAL("Unknown item type");
			break;
		}

		if (item->marked) str[4] = '*';

		x = data->widget_data->box.x + (depth - 1) * 5;
		for (i = 0; i < 5; i++) {
			draw_border_char(data->term, x + i, y, str[i], tree_color);
		}
	}

	x = data->widget_data->box.x + depth * 5;

	if (item->type == BI_SEPARATOR) {
		int i;
		int width = data->widget_data->box.width - depth * 5;

		for (i = 0; i < width; i++) {
			draw_border_char(data->term, x + i, y, BORDER_SHLINE, text_color);
		}

	} else if (data->box->ops && data->box->ops->draw) {
		int width = data->widget_data->box.width - depth * 5;

		data->box->ops->draw(item, data, x, y, width);

	} else {
		unsigned char *text;
		const struct listbox_ops *ops = data->box->ops;
		int len_bytes;

		assert(ops && ops->get_info);

		text = ops->get_text(item, data->term);
		if (!text) return 0;

		len = strlen((const char *)text);
		int_upper_bound(&len, int_max(0, data->widget_data->box.width - depth * 5));
#ifdef CONFIG_UTF8
		if (data->term->utf8_cp)
			len_bytes = utf8_cells2bytes(text, len, NULL);
		else
#endif /* CONFIG_UTF8 */
			len_bytes = len;

		draw_text(data->term, x, y, text, len_bytes, 0, text_color);

		mem_free(text);
	}

	if (item == data->box->sel) {
		/* For blind users: */
		x = data->widget_data->box.x + 5 + item->depth * 5;
		set_cursor(data->term, x, y, 1);
		set_window_ptr(data->dlg_data->win, x, y);
	}

	data->offset++;

	return 0;
}
Example #23
0
void init_params_R1 (BNTree *bntree) {
  int i, j;
  int dimer_pair_num = 0;
  int param_idx = 0;
  int types[] = {0,   1,  2,  3, 16, 17,
		 4,   5,  6,  7, 16, 17,
		 8,   9, 10, 11, 16, 17,
		 12, 13, 14, 15, 16, 17,
		 18, 18, 18, 18, 19, 20,
		 21, 21, 21, 21, 22, 23};

  for (i=0; i<36; i++) {
    for (j=0; j<36; j++) {
      int type1 = types[i];
      int type2 = types[j];
      int char1 = i / 6;
      int char2 = i % 6;
      int char3 = j / 6;
      int char4 = j % 6;

      if (i == j) continue;

      if (type1 < 16 && type2 < 16) { /* base dimer to base dimer substitution */
	if (i > j) continue;  /* already took care of these */

	param_idx = dimer_pair_num++;
	bntree->param_map[i][j] = param_idx;
	bntree->param_map[j][i] = param_idx;
	
	bntree->weight_idx[i][j] = j;
	bntree->weight_idx[j][i] = i;
      }

      else if (type1 == type2 && (char1 != char3 || char2 != char4)) {
	/* mutation in a gap pattern base with no change in pattern */
	if (type1 == 16) {
	  if (is_transition (char1, char3))
	    param_idx = 192;
	  else
	    param_idx = 193;
	}
	else if (type1 == 17) {
	  if (is_transition (char1, char3))
	    param_idx = 194;
	  else
	    param_idx = 195;

	}
	else if (type1 == 18) {
	  if (is_transition (char2, char4))
	    param_idx = 196;
	  else
	    param_idx = 197;
	}
	else if (type1 == 21) {
	  if (is_transition (char2, char4))
	    param_idx = 198;
	  else
	    param_idx = 199;
	}
	bntree->param_map[i][j] = param_idx;	
	bntree->weight_idx[i][j] = -1;
      }

      else { /* gap pattern to different gap pattern */
	param_idx = 120 + 8 * int_max(type1 - 15, 0) + int_max(type2 - 15, 0); 
	if (type1 < type2)
	  param_idx--;

	bntree->param_map[i][j] = param_idx;	

	if (type2 < 16)
	  bntree->weight_idx[i][j] = j;
	else
	  bntree->weight_idx[i][j] = -1;
      }
    }
  }

  /* self substitutions */
  for (i=0; i<36; i++) {
    bntree->param_map[i][i] = 200 + i;
    bntree->weight_idx[i][i] = -1;
  }

  assert (dimer_pair_num == 120);
}
Example #24
0
void
draw_text(struct terminal *term, int x, int y,
	  const unsigned char *text, int length,
	  int attr, struct color_pair *color)
{
	int end_pos;
	struct screen_char *pos, *end;

	assert(text && length >= 0);
	if_assert_failed return;

	if (x >= term->width || y >= term->height) return;

#ifdef CONFIG_UTF8
	if (term->utf8_cp) {
		draw_text_utf8(term, x, y, text, length, attr, color);
		return;
	}
#endif /* CONFIG_UTF8 */

	if (length <= 0) return;
	pos = get_char(term, x, y);
	if (!pos) return;

	end_pos = int_min(length, term->width - x) - 1;

#ifdef CONFIG_DEBUG
	/* Detect attempt to set @end to a point outside @text,
	 * it may occur in case of bad calculations. --Zas */
	if (end_pos < 0) {
		INTERNAL("end_pos < 0 !!");
		end_pos = 0;
	} else {
		int textlen = strlen((const char *)text);

		if (end_pos >= textlen) {
			INTERNAL("end_pos (%d) >= text length (%d) !!", end_pos, textlen);
			end_pos = textlen - 1;
		}
	}
#endif

	end = &pos[int_max(0, end_pos)];

	if (color) {
		/* Use the last char as template. */
		end->attr = attr;
		set_term_color(end, color, 0,
			       get_opt_int_tree(term->spec, (const unsigned char *)"colors", NULL));

		for (; pos < end && *text; text++, pos++) {
			end->data = *text;
			copy_screen_chars(pos, end, 1);
		}

		end->data = *text;

	} else {
		for (; pos <= end && *text; text++, pos++) {
			pos->data = *text;
		}
	}

	set_screen_dirty(term->screen, y, y);
}
Example #25
0
int t2_decode_packets(opj_t2_t *t2, unsigned char *src, int len, int tileno, opj_tcd_tile_t *tile, opj_codestream_info_t *cstr_info) {
	unsigned char *c = src;
	opj_pi_iterator_t *pi;
	int pino, e = 0;
	int n = 0, curtp = 0;
	int tp_start_packno;

	opj_image_t *image = t2->image;
	opj_cp_t *cp = t2->cp;
	
	/* create a packet iterator */
	pi = pi_create_decode(image, cp, tileno);
	if(!pi) {
		/* TODO: throw an error */
		return -999;
	}

	tp_start_packno = 0;
	
	for (pino = 0; pino <= cp->tcps[tileno].numpocs; pino++) {
		while (pi_next(&pi[pino])) {
			if ((cp->layer==0) || (cp->layer>=((pi[pino].layno)+1))) {
				opj_packet_info_t *pack_info;
				if (cstr_info)
					pack_info = &cstr_info->tile[tileno].packet[cstr_info->packno];
				else
					pack_info = NULL;
				e = t2_decode_packet(t2, c, src + len - c, tile, &cp->tcps[tileno], &pi[pino], pack_info);
			} else {
				e = 0;
			}
			if(e == -999) return -999;
			/* progression in resolution */
			image->comps[pi[pino].compno].resno_decoded =	
				(e > 0) ? 
				int_max(pi[pino].resno, image->comps[pi[pino].compno].resno_decoded) 
				: image->comps[pi[pino].compno].resno_decoded;
			n++;

			/* INDEX >> */
			if(cstr_info) {
				opj_tile_info_t *info_TL = &cstr_info->tile[tileno];
				opj_packet_info_t *info_PK = &info_TL->packet[cstr_info->packno];
				if (!cstr_info->packno) {
					info_PK->start_pos = info_TL->end_header + 1;
				} else if (info_TL->packet[cstr_info->packno-1].end_pos >= (int)cstr_info->tile[tileno].tp[curtp].tp_end_pos){ /* New tile part*/
					info_TL->tp[curtp].tp_numpacks = cstr_info->packno - tp_start_packno; /* Number of packets in previous tile-part*/
          info_TL->tp[curtp].tp_start_pack = tp_start_packno;
					tp_start_packno = cstr_info->packno;
					curtp++;
					info_PK->start_pos = cstr_info->tile[tileno].tp[curtp].tp_end_header+1;
				} else {
					info_PK->start_pos = (cp->tp_on && info_PK->start_pos) ? info_PK->start_pos : info_TL->packet[cstr_info->packno - 1].end_pos + 1;
				}
				info_PK->end_pos = info_PK->start_pos + e - 1;
				info_PK->end_ph_pos += info_PK->start_pos - 1;	/* End of packet header which now only represents the distance 
																												// to start of packet is incremented by value of start of packet*/
				cstr_info->packno++;
			}
			/* << INDEX */
			
			if (e == -999) {		/* ADD */
				break;
			} else {
				c += e;
			}			
		}
	}
	/* INDEX >> */
	if(cstr_info) {
		cstr_info->tile[tileno].tp[curtp].tp_numpacks = cstr_info->packno - tp_start_packno; /* Number of packets in last tile-part*/
    cstr_info->tile[tileno].tp[curtp].tp_start_pack = tp_start_packno;
	}
	/* << INDEX */

	/* don't forget to release pi */
	pi_destroy(pi, cp, tileno);
	
	if (e == -999) {
		return e;
	}
	
	return (c - src);
}
Example #26
0
static inline void
scan_css_token(struct scanner *scanner, struct scanner_token *token)
{
	const unsigned char *string = scanner->position;
	unsigned char first_char = *string;
	enum css_token_type type = CSS_TOKEN_GARBAGE;
	int real_length = -1;

	assert(first_char);
	token->string = string++;

	if (is_css_char_token(first_char)) {
		type = first_char;

	} else if (is_css_digit(first_char) || first_char == '.') {
		scan_css(scanner, string, CSS_CHAR_DIGIT);

		/* First scan the full number token */
		if (*string == '.') {
			string++;

			if (is_css_digit(*string)) {
				type = CSS_TOKEN_NUMBER;
				scan_css(scanner, string, CSS_CHAR_DIGIT);
			}
		}

		/* Check what kind of number we have */
		if (*string == '%') {
			if (first_char != '.')
				type = CSS_TOKEN_PERCENTAGE;
			string++;

		} else if (!is_css_ident_start(*string)) {
			type = CSS_TOKEN_NUMBER;

		} else {
			const unsigned char *ident = string;

			scan_css(scanner, string, CSS_CHAR_IDENT);
			type = map_scanner_string(scanner, ident, string,
						  CSS_TOKEN_DIMENSION);
		}

	} else if (is_css_ident_start(first_char)) {
		scan_css(scanner, string, CSS_CHAR_IDENT);

		if (*string == '(') {
			const unsigned char *function_end = string + 1;

			/* Make sure that we have an ending ')' */
			skip_css(scanner, function_end, ')');
			if (*function_end == ')') {
				type = map_scanner_string(scanner, token->string,
						string, CSS_TOKEN_FUNCTION);

				/* If it is not a known function just skip the
				 * how arg stuff so we don't end up generating
				 * a lot of useless tokens. */
				if (type == CSS_TOKEN_FUNCTION) {
					string = function_end;

				} else if (type == CSS_TOKEN_URL) {
					/* Extracting the URL first removes any
					 * leading or ending whitespace and
					 * then see if the url is given in a
					 * string. If that is the case the
					 * string delimiters are also trimmed.
					 * This is not totally correct because
					 * we should of course handle escape
					 * sequences .. but that will have to
					 * be fixed later.  */
					const unsigned char *from = string + 1;
					const unsigned char *to = function_end - 1;

					scan_css(scanner, from, CSS_CHAR_WHITESPACE);
					scan_back_css(scanner, to, CSS_CHAR_WHITESPACE);

					if (isquote(*from)) from++;
					if (isquote(*to)) to--;

					token->string = from;
					/* Given "url( )", @to and @from will
					 * cross when they scan forwards and
					 * backwards, respectively, for a non-
					 * whitespace character, and @to - @from
					 * will be negative.  If there is
					 * anything between the parentheses,
					 * @to and @from will not cross and @to
					 * - @from will not become negative. */
					real_length = int_max(0, to - from + 1);
					string = function_end;
				}

				assert(type != CSS_TOKEN_RGB || *string == '(');
				assert(type != CSS_TOKEN_URL || *string == ')');
				assert(type != CSS_TOKEN_FUNCTION || *string == ')');
			}

			string++;

		} else {
			type = CSS_TOKEN_IDENT;
		}

	} else if (!is_css_token_start(first_char)) {
		/* TODO: Better composing of error tokens. For now we just
		 * split them down into char tokens */

	} else if (first_char == '#') {
		/* Check whether it is hexcolor or hash token */
		if (is_css_hexdigit(*string)) {
			int hexdigits;

			scan_css(scanner, string, CSS_CHAR_HEX_DIGIT);

			/* Check that the hexdigit sequence is either 3 or 6
			 * chars and it isn't just start of some non-hex ident
			 * string. */
			hexdigits = string - token->string - 1;
			if ((hexdigits == 3 || hexdigits == 6)
			    && !is_css_ident(*string)) {
				type = CSS_TOKEN_HEX_COLOR;
			} else {
				scan_css(scanner, string, CSS_CHAR_IDENT);
				type = CSS_TOKEN_HASH;
			}

		} else if (is_css_ident(*string)) {
			/* Not *_ident_start() because hashes are #<name>. */
			scan_css(scanner, string, CSS_CHAR_IDENT);
			type = CSS_TOKEN_HASH;
		}

	} else if (first_char == '@') {
		/* Compose token containing @<ident> */
		if (is_css_ident_start(*string)) {
			const unsigned char *ident = string;

			/* Scan both ident start and ident */
			scan_css(scanner, string, CSS_CHAR_IDENT);
			type = map_scanner_string(scanner, ident, string,
						  CSS_TOKEN_AT_KEYWORD);
		}

	} else if (first_char == '*') {
		if (*string == '=') {
			type = CSS_TOKEN_SELECT_CONTAINS;
			string++;
		} else {
			type = CSS_TOKEN_IDENT;
		}

	} else if (first_char == '^') {
		if (*string == '=') {
			type = CSS_TOKEN_SELECT_BEGIN;
			string++;
		}

	} else if (first_char == '$') {
		if (*string == '=') {
			type = CSS_TOKEN_SELECT_END;
			string++;
		}

	} else if (first_char == '|') {
		if (*string == '=') {
			type = CSS_TOKEN_SELECT_HYPHEN_LIST;
			string++;
		}

	} else if (first_char == '!') {
		scan_css(scanner, string, CSS_CHAR_WHITESPACE);
		if (!c_strncasecmp(string, "important", 9)) {
			type = CSS_TOKEN_IMPORTANT;
			string += 9;
		}

	} else if (isquote(first_char)) {
		/* TODO: Escaped delimiters --jonas */
		int size = scanner->end - string;
		unsigned char *string_end = memchr(string, first_char, size);

		if (string_end) {
			/* We don't want the delimiters in the token */
			token->string++;
			real_length = string_end - token->string;
			string = string_end + 1;
			type = CSS_TOKEN_STRING;
		}

	} else if (first_char == '<' || first_char == '-') {
		/* Try to navigate SGML tagsoup */

		if (*string == '/') {
			/* Some kind of SGML tag end ... better bail out screaming */
			type = CSS_TOKEN_NONE;

		} else {
			const unsigned char *sgml = string;

			/* Skip anything looking like SGML "<!--" and "-->"
			 * comments + <![CDATA[ and ]]> notations. */
			scan_css(scanner, sgml, CSS_CHAR_SGML_MARKUP);

			if (sgml - string >= 2
			    && ((first_char == '<' && *string == '!')
				|| (first_char == '-' && sgml[-1] == '>'))) {
				type = CSS_TOKEN_SKIP;
				string = sgml;
			}
		}

	} else if (first_char == '/') {
		/* Comments */
		if (*string == '*') {
			type = CSS_TOKEN_SKIP;

			for (string++; string < scanner->end; string++)
				if (*string == '*' && string[1] == '/') {
					string += 2;
					break;
				}
		}

	} else {
		INTERNAL("Someone forgot to put code for recognizing tokens "
			 "which start with '%c'.", first_char);
	}

	token->type = type;
	token->length = real_length > 0 ? real_length : string - token->string;
	token->precedence = get_css_precedence(type);
	scanner->position = string;
}
Example #27
0
static int t2_encode_packet(opj_tcd_tile_t * tile, opj_tcp_t * tcp, opj_pi_iterator_t *pi, unsigned char *dest, int length, opj_codestream_info_t *cstr_info, int tileno) {
	int bandno, cblkno;
	unsigned char *c = dest;

	int compno = pi->compno;	/* component value */
	int resno  = pi->resno;		/* resolution level value */
	int precno = pi->precno;	/* precinct value */
	int layno  = pi->layno;		/* quality layer value */

	opj_tcd_tilecomp_t *tilec = &tile->comps[compno];
	opj_tcd_resolution_t *res = &tilec->resolutions[resno];
	
	opj_bio_t *bio = NULL;	/* BIO component */
	
	/* <SOP 0xff91> */
	if (tcp->csty & J2K_CP_CSTY_SOP) {
		c[0] = 255;
		c[1] = 145;
		c[2] = 0;
		c[3] = 4;
		c[4] = (unsigned char)((tile->packno % 65536) / 256);
		c[5] = (unsigned char)((tile->packno % 65536) % 256);
		c += 6;
	}
	/* </SOP> */
	
	if (!layno) {
		for (bandno = 0; bandno < res->numbands; bandno++) {
			opj_tcd_band_t *band = &res->bands[bandno];
			opj_tcd_precinct_t *prc = &band->precincts[precno];
			tgt_reset(prc->incltree);
			tgt_reset(prc->imsbtree);
			for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
				opj_tcd_cblk_enc_t* cblk = &prc->cblks.enc[cblkno];
				cblk->numpasses = 0;
				tgt_setvalue(prc->imsbtree, cblkno, band->numbps - cblk->numbps);
			}
		}
	}
	
	bio = bio_create();
	bio_init_enc(bio, c, length);
	bio_write(bio, 1, 1);		/* Empty header bit */
	
	/* Writing Packet header */
	for (bandno = 0; bandno < res->numbands; bandno++) {
		opj_tcd_band_t *band = &res->bands[bandno];
		opj_tcd_precinct_t *prc = &band->precincts[precno];
		for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
			opj_tcd_cblk_enc_t* cblk = &prc->cblks.enc[cblkno];
			opj_tcd_layer_t *layer = &cblk->layers[layno];
			if (!cblk->numpasses && layer->numpasses) {
				tgt_setvalue(prc->incltree, cblkno, layno);
			}
		}
		for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
			opj_tcd_cblk_enc_t* cblk = &prc->cblks.enc[cblkno];
			opj_tcd_layer_t *layer = &cblk->layers[layno];
			int increment = 0;
			int nump = 0;
			int len = 0, passno;
			/* cblk inclusion bits */
			if (!cblk->numpasses) {
				tgt_encode(bio, prc->incltree, cblkno, layno + 1);
			} else {
				bio_write(bio, layer->numpasses != 0, 1);
			}
			/* if cblk not included, go to the next cblk  */
			if (!layer->numpasses) {
				continue;
			}
			/* if first instance of cblk --> zero bit-planes information */
			if (!cblk->numpasses) {
				cblk->numlenbits = 3;
				tgt_encode(bio, prc->imsbtree, cblkno, 999);
			}
			/* number of coding passes included */
			t2_putnumpasses(bio, layer->numpasses);
			
			/* computation of the increase of the length indicator and insertion in the header     */
			for (passno = cblk->numpasses; passno < cblk->numpasses + layer->numpasses; passno++) {
				opj_tcd_pass_t *pass = &cblk->passes[passno];
				nump++;
				len += pass->len;
				if (pass->term || passno == (cblk->numpasses + layer->numpasses) - 1) {
					increment = int_max(increment, int_floorlog2(len) + 1 - (cblk->numlenbits + int_floorlog2(nump)));
					len = 0;
					nump = 0;
				}
			}
			t2_putcommacode(bio, increment);

			/* computation of the new Length indicator */
			cblk->numlenbits += increment;

			/* insertion of the codeword segment length */
			for (passno = cblk->numpasses; passno < cblk->numpasses + layer->numpasses; passno++) {
				opj_tcd_pass_t *pass = &cblk->passes[passno];
				nump++;
				len += pass->len;
				if (pass->term || passno == (cblk->numpasses + layer->numpasses) - 1) {
					bio_write(bio, len, cblk->numlenbits + int_floorlog2(nump));
					len = 0;
					nump = 0;
				}
			}
		}
	}

	if (bio_flush(bio)) {
		bio_destroy(bio);
		return -999;		/* modified to eliminate longjmp !! */
	}

	c += bio_numbytes(bio);
	bio_destroy(bio);
	
	/* <EPH 0xff92> */
	if (tcp->csty & J2K_CP_CSTY_EPH) {
		c[0] = 255;
		c[1] = 146;
		c += 2;
	}
	/* </EPH> */

	/* << INDEX */
	/* End of packet header position. Currently only represents the distance to start of packet
	// Will be updated later by incrementing with packet start value */
	if(cstr_info && cstr_info->index_write) {
		opj_packet_info_t *info_PK = &cstr_info->tile[tileno].packet[cstr_info->packno];
		info_PK->end_ph_pos = (int)(c - dest);
	}
	/* INDEX >> */
	
	/* Writing the packet body */
	
	for (bandno = 0; bandno < res->numbands; bandno++) {
		opj_tcd_band_t *band = &res->bands[bandno];
		opj_tcd_precinct_t *prc = &band->precincts[precno];
		for (cblkno = 0; cblkno < prc->cw * prc->ch; cblkno++) {
			opj_tcd_cblk_enc_t* cblk = &prc->cblks.enc[cblkno];
			opj_tcd_layer_t *layer = &cblk->layers[layno];
			if (!layer->numpasses) {
				continue;
			}
			if (c + layer->len > dest + length) {
				return -999;
			}
			
			memcpy(c, layer->data, layer->len);
			cblk->numpasses += layer->numpasses;
			c += layer->len;
			/* << INDEX */ 
			if(cstr_info && cstr_info->index_write) {
				opj_packet_info_t *info_PK = &cstr_info->tile[tileno].packet[cstr_info->packno];
				info_PK->disto += layer->disto;
				if (cstr_info->D_max < info_PK->disto) {
					cstr_info->D_max = info_PK->disto;
				}
			}
			/* INDEX >> */
		}
	}
	
	return (c - dest);
}
Example #28
0
opj_pi_iterator_t *pi_initialise_encode(opj_image_t *image, opj_cp_t *cp, int tileno, J2K_T2_MODE t2_mode){ 
	int p, q, pino;
	int compno, resno;
	int maxres = 0;
	int maxprec = 0;
	opj_pi_iterator_t *pi = NULL;
	opj_tcp_t *tcp = NULL;
	opj_tccp_t *tccp = NULL;
	size_t array_size;
	
	tcp = &cp->tcps[tileno];

	array_size = (tcp->numpocs + 1) * sizeof(opj_pi_iterator_t);
	pi = (opj_pi_iterator_t *) opj_malloc(array_size);
	if(!pi) {	return NULL;}
	pi->tp_on = cp->tp_on;
	
	for(pino = 0;pino < tcp->numpocs+1 ; pino ++){
		p = tileno % cp->tw;
		q = tileno / cp->tw;

		pi[pino].tx0 = int_max(cp->tx0 + p * cp->tdx, image->x0);
		pi[pino].ty0 = int_max(cp->ty0 + q * cp->tdy, image->y0);
		pi[pino].tx1 = int_min(cp->tx0 + (p + 1) * cp->tdx, image->x1);
		pi[pino].ty1 = int_min(cp->ty0 + (q + 1) * cp->tdy, image->y1);
		pi[pino].numcomps = image->numcomps;
		
		array_size = image->numcomps * sizeof(opj_pi_comp_t);
		pi[pino].comps = (opj_pi_comp_t *) opj_malloc(array_size);
		if(!pi[pino].comps) {
			pi_destroy(pi, cp, tileno);
			return NULL;
		}
		memset(pi[pino].comps, 0, array_size);
		
		for (compno = 0; compno < pi[pino].numcomps; compno++) {
			int tcx0, tcy0, tcx1, tcy1;
			opj_pi_comp_t *comp = &pi[pino].comps[compno];
			tccp = &tcp->tccps[compno];
			comp->dx = image->comps[compno].dx;
			comp->dy = image->comps[compno].dy;
			comp->numresolutions = tccp->numresolutions;

			array_size = comp->numresolutions * sizeof(opj_pi_resolution_t);
			comp->resolutions =	(opj_pi_resolution_t *) opj_malloc(array_size);
			if(!comp->resolutions) {
				pi_destroy(pi, cp, tileno);
				return NULL;
			}

			tcx0 = int_ceildiv(pi[pino].tx0, comp->dx);
			tcy0 = int_ceildiv(pi[pino].ty0, comp->dy);
			tcx1 = int_ceildiv(pi[pino].tx1, comp->dx);
			tcy1 = int_ceildiv(pi[pino].ty1, comp->dy);
			if (comp->numresolutions > maxres) {
				maxres = comp->numresolutions;
			}

			for (resno = 0; resno < comp->numresolutions; resno++) {
				int levelno;
				int rx0, ry0, rx1, ry1;
				int px0, py0, px1, py1;
				opj_pi_resolution_t *res = &comp->resolutions[resno];
				if (tccp->csty & J2K_CCP_CSTY_PRT) {
					res->pdx = tccp->prcw[resno];
					res->pdy = tccp->prch[resno];
				} else {
					res->pdx = 15;
					res->pdy = 15;
				}
				levelno = comp->numresolutions - 1 - resno;
				rx0 = int_ceildivpow2(tcx0, levelno);
				ry0 = int_ceildivpow2(tcy0, levelno);
				rx1 = int_ceildivpow2(tcx1, levelno);
				ry1 = int_ceildivpow2(tcy1, levelno);
				px0 = int_floordivpow2(rx0, res->pdx) << res->pdx;
				py0 = int_floordivpow2(ry0, res->pdy) << res->pdy;
				px1 = int_ceildivpow2(rx1, res->pdx) << res->pdx;
				py1 = int_ceildivpow2(ry1, res->pdy) << res->pdy;
				res->pw = (rx0==rx1)?0:((px1 - px0) >> res->pdx);
				res->ph = (ry0==ry1)?0:((py1 - py0) >> res->pdy);

				if (res->pw*res->ph > maxprec) {
					maxprec = res->pw * res->ph;
				}
			}
		}
		
		tccp = &tcp->tccps[0];
		pi[pino].step_p = 1;
		pi[pino].step_c = maxprec * pi[pino].step_p;
		pi[pino].step_r = image->numcomps * pi[pino].step_c;
		pi[pino].step_l = maxres * pi[pino].step_r;
		
		for (compno = 0; compno < pi->numcomps; compno++) {
			opj_pi_comp_t *comp = &pi->comps[compno];
			for (resno = 0; resno < comp->numresolutions; resno++) {
				int dx, dy;
				opj_pi_resolution_t *res = &comp->resolutions[resno];
				dx = comp->dx * (1 << (res->pdx + comp->numresolutions - 1 - resno));
				dy = comp->dy * (1 << (res->pdy + comp->numresolutions - 1 - resno));
				pi[pino].dx = !pi->dx ? dx : int_min(pi->dx, dx);
				pi[pino].dy = !pi->dy ? dy : int_min(pi->dy, dy);
			}
		}

		if (pino == 0) {
			array_size = tcp->numlayers * pi[pino].step_l * sizeof(short int);
			pi[pino].include = (short int *) opj_malloc(array_size);
			if(!pi[pino].include) {
				pi_destroy(pi, cp, tileno);
				return NULL;
			}
		}
		else {
			pi[pino].include = pi[pino - 1].include;
		}
		
		/* Generation of boundaries for each prog flag*/
			if(tcp->POC & (t2_mode == FINAL_PASS)){
				tcp->pocs[pino].compS= tcp->pocs[pino].compno0;
				tcp->pocs[pino].compE= tcp->pocs[pino].compno1;
				tcp->pocs[pino].resS = tcp->pocs[pino].resno0;
				tcp->pocs[pino].resE = tcp->pocs[pino].resno1;
				tcp->pocs[pino].layE = tcp->pocs[pino].layno1;
				tcp->pocs[pino].prg  = tcp->pocs[pino].prg1;
				if (pino > 0)
					tcp->pocs[pino].layS = (tcp->pocs[pino].layE > tcp->pocs[pino - 1].layE) ? tcp->pocs[pino - 1].layE : 0;
			}else {
				tcp->pocs[pino].compS= 0;
				tcp->pocs[pino].compE= image->numcomps;
				tcp->pocs[pino].resS = 0;
				tcp->pocs[pino].resE = maxres;
				tcp->pocs[pino].layS = 0;
				tcp->pocs[pino].layE = tcp->numlayers;
				tcp->pocs[pino].prg  = tcp->prg;
			}
			tcp->pocs[pino].prcS = 0;
			tcp->pocs[pino].prcE = maxprec;;
			tcp->pocs[pino].txS = pi[pino].tx0;
			tcp->pocs[pino].txE = pi[pino].tx1;
			tcp->pocs[pino].tyS = pi[pino].ty0;
			tcp->pocs[pino].tyE = pi[pino].ty1;
			tcp->pocs[pino].dx = pi[pino].dx;
			tcp->pocs[pino].dy = pi[pino].dy;
		}
			return pi;
	}
Example #29
0
File: pi.c Project: kerido/koapch
/// <summary>
/// Create a packet iterator.  
/// </summary>
pi_iterator_t *pi_create(j2k_image_t *img, j2k_cp_t *cp, int tileno) {
    int p, q;
    int compno, resno;
    int maxres=0;
    pi_iterator_t *pi;
    j2k_tcp_t *tcp;
    j2k_tccp_t *tccp;
    tcp=&cp->tcps[tileno];
    pi=(pi_iterator_t*)malloc(sizeof(pi_iterator_t));
    p=tileno%cp->tw;
    q=tileno/cp->tw;
    pi->tx0=int_max(cp->tx0+p*cp->tdx, img->x0);
    pi->ty0=int_max(cp->ty0+q*cp->tdy, img->y0);
    pi->tx1=int_min(cp->tx0+(p+1)*cp->tdx, img->x1);
    pi->ty1=int_min(cp->ty0+(q+1)*cp->tdy, img->y1);
    pi->numcomps=img->numcomps;
    pi->comps=(pi_comp_t*)malloc(img->numcomps*sizeof(pi_comp_t));
    for (compno=0; compno<pi->numcomps; compno++) {
        int tcx0, tcy0, tcx1, tcy1;
        pi_comp_t *comp=&pi->comps[compno];
        tccp=&tcp->tccps[compno];
        comp->dx=img->comps[compno].dx;
        comp->dy=img->comps[compno].dy;
        comp->numresolutions=tccp->numresolutions;
        comp->resolutions=(pi_resolution_t*)malloc(comp->numresolutions*sizeof(pi_resolution_t));
        tcx0=int_ceildiv(pi->tx0, comp->dx);
        tcy0=int_ceildiv(pi->ty0, comp->dy);
        tcx1=int_ceildiv(pi->tx1, comp->dx);
        tcy1=int_ceildiv(pi->ty1, comp->dy);
        if (comp->numresolutions>maxres) {
            maxres=comp->numresolutions;
        }
        for (resno=0; resno<comp->numresolutions; resno++) {
            int levelno;
            int rx0, ry0, rx1, ry1;
            int px0, py0, px1, py1;
            pi_resolution_t *res=&comp->resolutions[resno];
            if (tccp->csty&J2K_CCP_CSTY_PRT) {
                res->pdx=tccp->prcw[resno];
                res->pdy=tccp->prch[resno];
            } else {
                res->pdx=15;
                res->pdy=15;
            }
            levelno=comp->numresolutions-1-resno;
            rx0=int_ceildivpow2(tcx0, levelno);
            ry0=int_ceildivpow2(tcy0, levelno);
            rx1=int_ceildivpow2(tcx1, levelno);
            ry1=int_ceildivpow2(tcy1, levelno);
            px0=int_floordivpow2(rx0, res->pdx)<<res->pdx;
            py0=int_floordivpow2(ry0, res->pdy)<<res->pdy;
            px1=int_ceildivpow2(rx1, res->pdx)<<res->pdx;
            py1=int_ceildivpow2(ry1, res->pdy)<<res->pdy;
            res->pw=(px1-px0)>>res->pdx;
            res->ph=(py1-py0)>>res->pdy;
        }
    }
    pi->first=1;
    pi->poc.resno0=0;
    pi->poc.compno0=0;
    pi->poc.layno1=tcp->numlayers;
    pi->poc.resno1=maxres;
    pi->poc.compno1=img->numcomps;
    pi->poc.prg=tcp->prg;
    return pi;
}
Example #30
0
/* Many execution paths may lead to this code so it needs to take appropriate
 * precausions to stuff like doc_view and doc_view->vs being NULL. */
enum frame_event_status
do_action(struct session *ses, enum main_action action_id, int verbose)
{
	enum frame_event_status status = FRAME_EVENT_OK;
	struct terminal *term = ses->tab->term;
	struct document_view *doc_view = current_frame(ses);
	struct link *link = NULL;

	if (action_id == -1) goto unknown_action;

	if (doc_view && doc_view->vs) {
		if (action_prefix_is_link_number(KEYMAP_MAIN, action_id)
		    && !try_jump_to_link_number(ses, doc_view))
			goto ignore_action;

		link = get_current_link(doc_view);

	} else if (action_requires_view_state(KEYMAP_MAIN, action_id)) {
		goto ignore_action;
	}

	if (action_requires_location(KEYMAP_MAIN, action_id)
	    && !have_location(ses))
		return FRAME_EVENT_OK;

	if (action_requires_link(KEYMAP_MAIN, action_id)
	    && !link)
		goto ignore_action;

	if (action_requires_form(KEYMAP_MAIN, action_id)
	    && (!link || !link_is_form(link)))
		goto ignore_action;

	if (!action_is_anonymous_safe(KEYMAP_MAIN, action_id)
	    && get_cmd_opt_bool("anonymous"))
		goto ignore_action;

	/* Please keep in alphabetical order for now. Later we can sort by most
	 * used or something. */
	switch (action_id) {
		case ACT_MAIN_ABORT_CONNECTION:
			abort_loading(ses, 1);
			print_screen_status(ses);
			break;

		case ACT_MAIN_ADD_BOOKMARK:
#ifdef CONFIG_BOOKMARKS
			launch_bm_add_doc_dialog(term, NULL, ses);
#endif
			break;
		case ACT_MAIN_ADD_BOOKMARK_LINK:
#ifdef CONFIG_BOOKMARKS
			launch_bm_add_link_dialog(term, NULL, ses);
#endif
			break;
		case ACT_MAIN_ADD_BOOKMARK_TABS:
#ifdef CONFIG_BOOKMARKS
			bookmark_terminal_tabs_dialog(term);
#endif
			break;

		case ACT_MAIN_AUTH_MANAGER:
			auth_manager(ses);
			break;

		case ACT_MAIN_BACKSPACE_PREFIX:

			if (!ses->kbdprefix.repeat_count) break;

			set_kbd_repeat_count(ses,
			                     ses->kbdprefix.repeat_count / 10);

			/* Keep send_event from resetting repeat_count. */
			status = FRAME_EVENT_SESSION_DESTROYED;

			break;

		case ACT_MAIN_BOOKMARK_MANAGER:
#ifdef CONFIG_BOOKMARKS
			bookmark_manager(ses);
#endif
			break;

		case ACT_MAIN_CACHE_MANAGER:
			cache_manager(ses);
			break;

		case ACT_MAIN_CACHE_MINIMIZE:
			shrink_memory(1);
			break;

		case ACT_MAIN_COOKIES_LOAD:
#ifdef CONFIG_COOKIES
			if (!get_opt_bool("cookies.save", NULL)) break;
			load_cookies();
#endif
			break;

		case ACT_MAIN_COOKIE_MANAGER:
#ifdef CONFIG_COOKIES
			cookie_manager(ses);
#endif
			break;

		case ACT_MAIN_COPY_CLIPBOARD:
			status = copy_current_link_to_clipboard(ses, doc_view, 0);
			break;

		case ACT_MAIN_DOCUMENT_INFO:
			document_info_dialog(ses);
			break;

		case ACT_MAIN_DOWNLOAD_MANAGER:
			download_manager(ses);
			break;

		case ACT_MAIN_EXMODE:
#ifdef CONFIG_EXMODE
			exmode_start(ses);
#endif
			break;

		case ACT_MAIN_FILE_MENU:
			activate_bfu_technology(ses, 0);
			break;

		case ACT_MAIN_FIND_NEXT:
			status = find_next(ses, doc_view, 1);
			break;

		case ACT_MAIN_FIND_NEXT_BACK:
			status = find_next(ses, doc_view, -1);
			break;

		case ACT_MAIN_FORGET_CREDENTIALS:
			free_auth();
			shrink_memory(1); /* flush caches */
			break;

		case ACT_MAIN_FORMHIST_MANAGER:
#ifdef CONFIG_FORMHIST
			formhist_manager(ses);
#endif
			break;

		case ACT_MAIN_FRAME_EXTERNAL_COMMAND:
			status = pass_uri_to_command(ses, doc_view,
			                             PASS_URI_FRAME);
			break;

		case ACT_MAIN_FRAME_NEXT:
			next_frame(ses, 1);
			draw_formatted(ses, 0);
			break;

		case ACT_MAIN_FRAME_MAXIMIZE:
			status = set_frame(ses, doc_view, 0);
			break;

		case ACT_MAIN_FRAME_PREV:
			next_frame(ses, -1);
			draw_formatted(ses, 0);
			break;

		case ACT_MAIN_GOTO_URL:
			goto_url_action(ses, NULL);
			break;

		case ACT_MAIN_GOTO_URL_CURRENT:
			goto_url_action(ses, get_current_url);
			break;

		case ACT_MAIN_GOTO_URL_CURRENT_LINK:
			goto_url_action(ses, get_current_link_url);
			break;

		case ACT_MAIN_GOTO_URL_HOME:
			goto_url_home(ses);
			break;

		case ACT_MAIN_HEADER_INFO:
			protocol_header_dialog(ses);
			break;

		case ACT_MAIN_HISTORY_MANAGER:
#ifdef CONFIG_GLOBHIST
			history_manager(ses);
#endif
			break;

		case ACT_MAIN_HISTORY_MOVE_BACK:
		{
			int count = int_max(1, eat_kbd_repeat_count(ses));

			go_history_by_n(ses, -count);
			break;
		}
		case ACT_MAIN_HISTORY_MOVE_FORWARD:
		{
			int count = int_max(1, eat_kbd_repeat_count(ses));

			go_history_by_n(ses, count);
			break;
		}
		case ACT_MAIN_JUMP_TO_LINK:
			break;

		case ACT_MAIN_KEYBINDING_MANAGER:
			keybinding_manager(ses);
			break;

		case ACT_MAIN_KILL_BACKGROUNDED_CONNECTIONS:
			abort_background_connections();
			break;

		case ACT_MAIN_LINK_DIALOG:
			open_link_dialog(ses);
			break;

		case ACT_MAIN_LINK_DOWNLOAD:
		case ACT_MAIN_LINK_DOWNLOAD_IMAGE:
		case ACT_MAIN_LINK_DOWNLOAD_RESUME:
			status = download_link(ses, doc_view, action_id);
			break;

		case ACT_MAIN_LINK_EXTERNAL_COMMAND:
			status = pass_uri_to_command(ses, doc_view,
			                             PASS_URI_LINK);
			break;

		case ACT_MAIN_LINK_FOLLOW:
			status = enter(ses, doc_view, 0);
			break;

		case ACT_MAIN_LINK_FOLLOW_RELOAD:
			status = enter(ses, doc_view, 1);
			break;

		case ACT_MAIN_LINK_INFO:
			link_info_dialog(ses);
			break;
			
		case ACT_MAIN_LINK_MENU:
			link_menu(term, NULL, ses);
			break;

		case ACT_MAIN_LINK_FORM_MENU:
			link_form_menu(ses);
			break;

		case ACT_MAIN_LUA_CONSOLE:
#ifdef CONFIG_SCRIPTING_LUA
			trigger_event_name("dialog-lua-console", ses);
#endif
			break;

		case ACT_MAIN_MARK_SET:
#ifdef CONFIG_MARKS
			ses->kbdprefix.mark = KP_MARK_SET;
			status = FRAME_EVENT_REFRESH;
#endif
			break;

		case ACT_MAIN_MARK_GOTO:
#ifdef CONFIG_MARKS
			/* TODO: Show promptly a menu (or even listbox?)
			 * with all the marks. But the next letter must
			 * still choose a mark directly! --pasky */
			ses->kbdprefix.mark = KP_MARK_GOTO;
			status = FRAME_EVENT_REFRESH;
#endif
			break;

		case ACT_MAIN_MENU:
			activate_bfu_technology(ses, -1);
			break;

		case ACT_MAIN_MOVE_CURRENT_TOP:
			status = move_current_top(ses, doc_view);
			break;

		case ACT_MAIN_MOVE_CURSOR_UP:
			status = move_cursor_up(ses, doc_view);
			break;

		case ACT_MAIN_MOVE_CURSOR_DOWN:
			status = move_cursor_down(ses, doc_view);
			break;

		case ACT_MAIN_MOVE_CURSOR_LEFT:
			status = move_cursor_left(ses, doc_view);
			break;

		case ACT_MAIN_MOVE_CURSOR_RIGHT:
			status = move_cursor_right(ses, doc_view);
			break;

		case ACT_MAIN_MOVE_CURSOR_LINE_START:
			status = move_cursor_line_start(ses, doc_view);
			break;

		case ACT_MAIN_MOVE_HALF_PAGE_DOWN:
			status = move_half_page_down(ses, doc_view);
			break;

		case ACT_MAIN_MOVE_HALF_PAGE_UP:
			status = move_half_page_up(ses, doc_view);
			break;

		case ACT_MAIN_MOVE_LINK_DOWN:
			status = move_link_down(ses, doc_view);
			break;

		case ACT_MAIN_MOVE_LINK_DOWN_LINE:
			status = move_link_down_line(ses, doc_view);
			break;

		case ACT_MAIN_MOVE_LINK_LEFT:
			status = move_link_left(ses, doc_view);
			break;

		case ACT_MAIN_MOVE_LINK_LEFT_LINE:
			status = move_link_prev_line(ses, doc_view);
			break;

		case ACT_MAIN_MOVE_LINK_NEXT:
			status = move_link_next(ses, doc_view);
			break;

		case ACT_MAIN_MOVE_LINK_PREV:
			status = move_link_prev(ses, doc_view);
			break;

		case ACT_MAIN_MOVE_LINK_RIGHT:
			status = move_link_right(ses, doc_view);
			break;

		case ACT_MAIN_MOVE_LINK_RIGHT_LINE:
			status = move_link_next_line(ses, doc_view);
			break;

		case ACT_MAIN_MOVE_LINK_UP:
			status = move_link_up(ses, doc_view);
			break;

		case ACT_MAIN_MOVE_LINK_UP_LINE:
			status = move_link_up_line(ses, doc_view);
			break;

		case ACT_MAIN_MOVE_PAGE_DOWN:
			status = move_page_down(ses, doc_view);
			break;

		case ACT_MAIN_MOVE_PAGE_UP:
			status = move_page_up(ses, doc_view);
			break;

		case ACT_MAIN_MOVE_DOCUMENT_START:
			status = move_document_start(ses, doc_view);
			break;

		case ACT_MAIN_MOVE_DOCUMENT_END:
			status = move_document_end(ses, doc_view);
			break;

		case ACT_MAIN_OPEN_LINK_IN_NEW_TAB:
			open_current_link_in_new_tab(ses, 0);
			break;

		case ACT_MAIN_OPEN_LINK_IN_NEW_TAB_IN_BACKGROUND:
			open_current_link_in_new_tab(ses, 1);
			break;

		case ACT_MAIN_OPEN_LINK_IN_NEW_WINDOW:
			open_in_new_window(term, send_open_in_new_window, ses);
			break;

		case ACT_MAIN_OPEN_NEW_TAB:
			open_uri_in_new_tab(ses, NULL, 0, 1);
			break;

		case ACT_MAIN_OPEN_NEW_TAB_IN_BACKGROUND:
			open_uri_in_new_tab(ses, NULL, 1, 1);
			break;

		case ACT_MAIN_OPEN_NEW_WINDOW:
			open_in_new_window(term, send_open_new_window, ses);
			break;

		case ACT_MAIN_OPEN_OS_SHELL:
			exec_shell(term);
			break;

		case ACT_MAIN_OPTIONS_MANAGER:
			options_manager(ses);
			break;

		case ACT_MAIN_QUIT:
			exit_prog(ses, 1);
			break;

		case ACT_MAIN_REALLY_QUIT:
			exit_prog(ses, 0);
			break;

		case ACT_MAIN_REDRAW:
			redraw_terminal_cls(term);
			break;

		case ACT_MAIN_RELOAD:
			reload(ses, CACHE_MODE_INCREMENT);
			break;

		case ACT_MAIN_RERENDER:
			draw_formatted(ses, 2);
			break;

		case ACT_MAIN_RESET_FORM:
			status = reset_form(ses, doc_view, 0);
			break;

		case ACT_MAIN_RESOURCE_INFO:
			resource_info(term);
			break;

		case ACT_MAIN_SAVE_AS:
			status = save_as(ses, doc_view, 0);
			break;

		case ACT_MAIN_SAVE_FORMATTED:
			status = save_formatted_dlg(ses, doc_view, 0);
			break;

		case ACT_MAIN_SAVE_OPTIONS:
			write_config(term);
			break;

		case ACT_MAIN_SAVE_URL_AS:
			save_url_as(ses);
			break;

		case ACT_MAIN_SCROLL_DOWN:
			status = scroll_down(ses, doc_view);
			break;

		case ACT_MAIN_SCROLL_LEFT:
			status = scroll_left(ses, doc_view);
			break;

		case ACT_MAIN_SCROLL_RIGHT:
			status = scroll_right(ses, doc_view);
			break;

		case ACT_MAIN_SCROLL_UP:
			status = scroll_up(ses, doc_view);
			break;

		case ACT_MAIN_SEARCH:
			status = search_dlg(ses, doc_view, 1);
			break;

		case ACT_MAIN_SEARCH_BACK:
			status = search_dlg(ses, doc_view, -1);
			break;

		case ACT_MAIN_SEARCH_TYPEAHEAD:
		case ACT_MAIN_SEARCH_TYPEAHEAD_LINK:
		case ACT_MAIN_SEARCH_TYPEAHEAD_TEXT:
		case ACT_MAIN_SEARCH_TYPEAHEAD_TEXT_BACK:
			status = search_typeahead(ses, doc_view, action_id);
			break;

		case ACT_MAIN_SHOW_TERM_OPTIONS:
			terminal_options(term, NULL, ses);
			break;

		case ACT_MAIN_SUBMIT_FORM:
			status = submit_form(ses, doc_view, 0);
			break;

		case ACT_MAIN_SUBMIT_FORM_RELOAD:
			status = submit_form(ses, doc_view, 1);
			break;

		case ACT_MAIN_TAB_CLOSE:
			close_tab(term, ses);
			status = FRAME_EVENT_SESSION_DESTROYED;
			break;

		case ACT_MAIN_TAB_CLOSE_ALL_BUT_CURRENT:
			close_all_tabs_but_current(ses);
			break;

		case ACT_MAIN_TAB_EXTERNAL_COMMAND:
			status = pass_uri_to_command(ses, doc_view,
			                             PASS_URI_TAB);
			break;

		case ACT_MAIN_TAB_MOVE_LEFT:
			move_current_tab(ses, -1);
			break;

		case ACT_MAIN_TAB_MOVE_RIGHT:
			move_current_tab(ses, 1);
			break;

		case ACT_MAIN_TAB_MENU:
			assert(ses->tab == get_current_tab(term));

			if (ses->status.show_tabs_bar)
				tab_menu(ses, ses->tab->xpos,
					 term->height - 1
					  - ses->status.show_status_bar,
					 1);
			else
				tab_menu(ses, 0, 0, 0);

			break;

		case ACT_MAIN_TAB_NEXT:
			switch_current_tab(ses, 1);
			break;

		case ACT_MAIN_TAB_PREV:
			switch_current_tab(ses, -1);
			break;

		case ACT_MAIN_TERMINAL_RESIZE:
			resize_terminal_dialog(term);
			break;

		case ACT_MAIN_TOGGLE_CSS:
#ifdef CONFIG_CSS
			toggle_document_option(ses, "document.css.enable");
#endif
			break;

		case ACT_MAIN_TOGGLE_DISPLAY_IMAGES:
			toggle_document_option(ses, "document.browse.images.show_as_links");
			break;

		case ACT_MAIN_TOGGLE_DISPLAY_TABLES:
			toggle_document_option(ses, "document.html.display_tables");
			break;

		case ACT_MAIN_TOGGLE_DOCUMENT_COLORS:
			toggle_document_option(ses, "document.colors.use_document_colors");
			break;

		case ACT_MAIN_TOGGLE_HTML_PLAIN:
			toggle_plain_html(ses, ses->doc_view, 0);
			break;

		case ACT_MAIN_TOGGLE_MOUSE:
#ifdef CONFIG_MOUSE
			toggle_mouse();
#endif
			break;

		case ACT_MAIN_TOGGLE_NUMBERED_LINKS:
			toggle_document_option(ses, "document.browse.links.numbering");
			break;

		case ACT_MAIN_TOGGLE_PLAIN_COMPRESS_EMPTY_LINES:
			toggle_document_option(ses, "document.plain.compress_empty_lines");
			break;

		case ACT_MAIN_TOGGLE_WRAP_TEXT:
			toggle_wrap_text(ses, ses->doc_view, 0);
			break;

		case ACT_MAIN_VIEW_IMAGE:
			status = view_image(ses, doc_view, 0);
			break;

		case ACT_MAIN_SCRIPTING_FUNCTION:
		case ACT_MAIN_NONE:
		case MAIN_ACTIONS:
		default:
unknown_action:
			if (verbose) {
				INTERNAL("No action handling defined for '%s'.",
					 get_action_name(KEYMAP_MAIN, action_id));
			}

			status = FRAME_EVENT_IGNORED;
	}

ignore_action:
	/* XXX: At this point the session may have been destroyed */

	if (status != FRAME_EVENT_SESSION_DESTROYED
	    && ses->insert_mode == INSERT_MODE_ON
	    && link != get_current_link(doc_view))
		ses->insert_mode = INSERT_MODE_OFF;

	if (status == FRAME_EVENT_REFRESH && doc_view)
		refresh_view(ses, doc_view, 0);

	return status;
}